sunshine-sdk/tests/unit/test_logging.cpp

65 lines
1.6 KiB
C++

/**
* @file tests/unit/test_logging.cpp
* @brief Test src/logging.*.
*/
#include <src/logging.h>
#include "../tests_common.h"
#include <fstream>
#include <random>
namespace {
std::array log_levels = {
std::tuple("verbose", &verbose),
std::tuple("debug", &debug),
std::tuple("info", &info),
std::tuple("warning", &warning),
std::tuple("error", &error),
std::tuple("fatal", &fatal),
};
constexpr auto log_file = "test_sunshine.log";
} // namespace
struct LogLevelsTest: testing::TestWithParam<decltype(log_levels)::value_type> {};
INSTANTIATE_TEST_SUITE_P(
Logging,
LogLevelsTest,
testing::ValuesIn(log_levels),
[](const auto &info) { return std::string(std::get<0>(info.param)); });
TEST_P(LogLevelsTest, PutMessage) {
auto [label, plogger] = GetParam();
ASSERT_TRUE(plogger);
auto &logger = *plogger;
std::random_device rand_dev;
std::mt19937_64 rand_gen(rand_dev());
auto test_message = std::to_string(rand_gen()) + std::to_string(rand_gen());
BOOST_LOG(logger) << test_message;
// Flush logger and search for the message in the log file
logging::log_flush();
std::ifstream input(log_file);
ASSERT_TRUE(input.is_open());
bool found = false;
for (std::string line; std::getline(input, line);) {
if (line.find(test_message) != std::string::npos) {
// Assume that logger may change the case of log level label
std::transform(line.begin(), line.end(), line.begin(),
[](char c) { return std::tolower(c); });
if (line.find(label) != std::string::npos) {
found = true;
break;
}
}
}
ASSERT_TRUE(found);
}