File: fake_log.cpp

package info (click to toggle)
repowerd 2025.09-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,204 kB
  • sloc: cpp: 24,530; ansic: 397; xml: 219; makefile: 14
file content (80 lines) | stat: -rw-r--r-- 2,009 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright © 2016 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 */

#include "fake_log.h"

#include <cstdarg>
#include <cstdlib>

namespace rt = repowerd::test;

namespace
{

bool get_log_to_console()
{
    auto const test_log_cstr = getenv("REPOWERD_TEST_LOG");
    auto const test_log = test_log_cstr ? std::string{test_log_cstr} : "";

    return test_log == "console";
}

}

rt::FakeLog::FakeLog(repowerd::LogLevel threshold)
    : repowerd::Log::Log(threshold), log_to_console{get_log_to_console()}
{
}

void rt::FakeLog::logOutput(repowerd::LogLevel, char const* tag, char const* format, va_list ap)
{
    std::string const format_str = std::string{tag} + ": " + format + "\n";

    char output[1024];
    vsnprintf(output, 1024, format_str.c_str(), ap);

    std::lock_guard<std::mutex> lock{contents_mutex};
    contents.push_back(output);

    if (log_to_console)
        printf("%s", output);
}

bool rt::FakeLog::contains_line(std::vector<std::string> const& words)
{
    std::lock_guard<std::mutex> lock{contents_mutex};

    for (auto const& line : contents)
    {
        bool found = true;

        for (auto const& word : words)
        {
            if (line.find(word) == std::string::npos)
            {
                found = false;
                break;
            }
        }

        if (found)
            return true;
    }

    return false;
}