fix(logging): add logging namespace and create logging::init method (#2336)
This commit is contained in:
parent
a1edc246f5
commit
2da6fb050a
9 changed files with 304 additions and 158 deletions
|
|
@ -5,16 +5,16 @@
|
|||
#include <boost/log/expressions.hpp>
|
||||
#include <boost/log/sinks/sync_frontend.hpp>
|
||||
#include <boost/log/sinks/text_ostream_backend.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include <src/globals.h>
|
||||
#include <src/logging.h>
|
||||
#include <src/platform/common.h>
|
||||
|
||||
#include <tests/utils.h>
|
||||
|
||||
namespace logging = boost::log;
|
||||
namespace sinks = logging::sinks;
|
||||
namespace boost_logging = boost::log;
|
||||
namespace sinks = boost_logging::sinks;
|
||||
|
||||
// Undefine the original TEST macro
|
||||
#undef TEST
|
||||
|
|
@ -40,6 +40,9 @@ protected:
|
|||
// we can possibly use some internal googletest functions to capture stdout and stderr, but I have not tested this
|
||||
// https://stackoverflow.com/a/33186201/11214013
|
||||
|
||||
// Add a member variable for deinit_guard
|
||||
std::unique_ptr<logging::deinit_t> deinit_guard;
|
||||
|
||||
// Add a member variable to store the sink
|
||||
boost::shared_ptr<sinks::synchronous_sink<sinks::text_ostream_backend>> test_sink;
|
||||
|
||||
|
|
@ -79,7 +82,7 @@ protected:
|
|||
test_sink->locked_backend()->add_stream(stream);
|
||||
|
||||
// Register the sink in the logging core (BOOST_LOG)
|
||||
logging::core::get()->add_sink(test_sink);
|
||||
boost_logging::core::get()->add_sink(test_sink);
|
||||
|
||||
sbuf = std::cout.rdbuf(); // save cout buffer (std::cout)
|
||||
std::cout.rdbuf(cout_buffer.rdbuf()); // redirect cout to buffer (std::cout)
|
||||
|
|
@ -87,6 +90,11 @@ protected:
|
|||
// todo: do this only once
|
||||
// setup a mail object
|
||||
mail::man = std::make_shared<safe::mail_raw_t>();
|
||||
|
||||
deinit_guard = logging::init(0, "test.log");
|
||||
if (!deinit_guard) {
|
||||
FAIL() << "Logging failed to initialize";
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
|
@ -121,7 +129,7 @@ protected:
|
|||
}
|
||||
|
||||
// Remove the sink from the logging core (BOOST_LOG)
|
||||
logging::core::get()->remove_sink(test_sink);
|
||||
boost_logging::core::get()->remove_sink(test_sink);
|
||||
test_sink.reset();
|
||||
}
|
||||
|
||||
|
|
|
|||
75
tests/unit/test_logging.cpp
Normal file
75
tests/unit/test_logging.cpp
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
/**
|
||||
* @file tests/test_logging.cpp
|
||||
* @brief Test src/logging.*.
|
||||
*/
|
||||
#include <fstream>
|
||||
|
||||
#include <src/logging.h>
|
||||
|
||||
#include <tests/conftest.cpp>
|
||||
|
||||
class LoggerInitTest: public virtual BaseTest, public ::testing::WithParamInterface<int> {
|
||||
protected:
|
||||
void
|
||||
SetUp() override {
|
||||
BaseTest::SetUp();
|
||||
}
|
||||
|
||||
void
|
||||
TearDown() override {
|
||||
BaseTest::TearDown();
|
||||
}
|
||||
};
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
LogLevel,
|
||||
LoggerInitTest,
|
||||
::testing::Values(
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5));
|
||||
TEST_P(LoggerInitTest, InitLogging) {
|
||||
int logLevel = GetParam();
|
||||
std::string logFilePath = "test_log_" + std::to_string(logLevel) + ".log";
|
||||
|
||||
// deinit the BaseTest logger
|
||||
BaseTest::deinit_guard.reset();
|
||||
|
||||
auto log_deinit = logging::init(logLevel, logFilePath);
|
||||
if (!log_deinit) {
|
||||
FAIL() << "Failed to initialize logging";
|
||||
}
|
||||
}
|
||||
|
||||
TEST(LogFlushTest, CheckLogFile) {
|
||||
// Write a log message
|
||||
BOOST_LOG(info) << "Test message";
|
||||
|
||||
// Call log_flush
|
||||
logging::log_flush();
|
||||
|
||||
// Check the contents of the log file
|
||||
std::ifstream log_file("test.log");
|
||||
std::string line;
|
||||
bool found = false;
|
||||
while (std::getline(log_file, line)) {
|
||||
if (line.find("Test message") != std::string::npos) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(found);
|
||||
}
|
||||
|
||||
TEST(PrintHelpTest, CheckOutput) {
|
||||
std::string name = "test";
|
||||
logging::print_help(name.c_str());
|
||||
|
||||
std::string output = cout_buffer.str();
|
||||
|
||||
EXPECT_NE(output.find("Usage: " + name), std::string::npos);
|
||||
EXPECT_NE(output.find("--help"), std::string::npos);
|
||||
}
|
||||
|
|
@ -21,10 +21,6 @@ protected:
|
|||
bool isEncoderValid;
|
||||
isEncoderValid = video::validate_encoder(*encoder, false);
|
||||
|
||||
// todo: av logging is not redirected to boost so it will be visible whether the test passes or fails
|
||||
// move this code to logging
|
||||
// https://github.com/LizardByte/Sunshine/blob/5606840c8983b714a0e442c42d887a49807715e1/src/main.cpp#L118
|
||||
|
||||
if (!isEncoderValid) {
|
||||
// if encoder is software fail, otherwise skip
|
||||
if (encoder == &video::software && std::string(TESTS_SOFTWARE_ENCODER_UNAVAILABLE) == "fail") {
|
||||
|
|
@ -49,7 +45,6 @@ INSTANTIATE_TEST_SUITE_P(
|
|||
EncoderVariants,
|
||||
EncoderTest,
|
||||
::testing::Values(
|
||||
// todo: all encoders crash on windows, probably due to platf not being initialized (which also crashes)
|
||||
#if !defined(__APPLE__)
|
||||
std::make_tuple(video::nvenc.name, &video::nvenc),
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue