diff options
-rw-r--r-- | src/buildtool/logging/logger.hpp | 38 | ||||
-rw-r--r-- | test/buildtool/logging/logger.test.cpp | 72 |
2 files changed, 110 insertions, 0 deletions
diff --git a/src/buildtool/logging/logger.hpp b/src/buildtool/logging/logger.hpp index 6d7fd66d..d8e20f6d 100644 --- a/src/buildtool/logging/logger.hpp +++ b/src/buildtool/logging/logger.hpp @@ -107,6 +107,44 @@ class Logger { } } + /// \brief Generic logging method. Provides a common interface between the + /// global logger and named instances, hidden from the outside caller. + /// For named instances no global configuration is used. + template <class... T_Args> + static void Log(Logger const* logger, + LogLevel level, + std::string const& msg, + T_Args&&... args) noexcept { + if (static_cast<int>(level) <= + static_cast<int>(logger != nullptr ? logger->log_limit_ + : LogConfig::LogLimit())) { + FormatAndForward( + logger, + logger != nullptr ? logger->sinks_ : LogConfig::Sinks(), + level, + msg, + std::forward<T_Args>(args)...); + } + } + + /// \brief Generic logging method with provided message creator. Provides a + /// common interface between the global logger and named instances, hidden + /// from the outside caller. + /// For named instances no global configuration is used. + static void Log(Logger const* logger, + LogLevel level, + MessageCreateFunc const& msg_creator) noexcept { + if (static_cast<int>(level) <= + static_cast<int>(logger != nullptr ? logger->log_limit_ + : LogConfig::LogLimit())) { + FormatAndForward( + logger, + logger != nullptr ? logger->sinks_ : LogConfig::Sinks(), + level, + msg_creator()); + } + } + private: std::string name_{}; LogLevel log_limit_{}; diff --git a/test/buildtool/logging/logger.test.cpp b/test/buildtool/logging/logger.test.cpp index 7a265323..eac80424 100644 --- a/test/buildtool/logging/logger.test.cpp +++ b/test/buildtool/logging/logger.test.cpp @@ -342,3 +342,75 @@ TEST_CASE_METHOD(TwoGlobalSinksFixture, } } } + +TEST_CASE_METHOD(OneGlobalSinkFixture, + "Common interface for global and local named loggers" + "[logger]") { + // global logs will be forwarded to instance: 0 + int global_instance = 0; + + // create local logger with separate sink instance + Logger logger("OwnSinkLogger", {LogSinkTest::CreateFactory()}); + // local logs should be forwarded to new sink instance: 1 + int local_instance = 1; + + SECTION("global instance") { + // create log outside of log limit + Logger::Log(nullptr, LogLevel::Trace, "first"); + CHECK(TestPrints::Read(global_instance).empty()); + + SECTION("create log within log limit") { + Logger::Log(nullptr, LogLevel::Info, "second"); + auto prints = TestPrints::Read(global_instance); + REQUIRE(prints.size() == 1); + CHECK(prints[0] == "INFO: second"); + + SECTION("increase log limit create log within log limit") { + LogConfig::SetLogLimit(LogLevel::Trace); + Logger::Log(nullptr, LogLevel::Trace, "third"); + auto prints = TestPrints::Read(global_instance); + REQUIRE(prints.size() == 2); + CHECK(prints[1] == "TRACE: third"); + + SECTION("log via lambda function") { + Logger::Log(nullptr, LogLevel::Trace, [] { + return std::string{"forth"}; + }); + auto prints = TestPrints::Read(global_instance); + REQUIRE(prints.size() == 3); + CHECK(prints[2] == "TRACE: forth"); + } + } + } + } + + SECTION("named instance") { + // create log outside of log limit + Logger::Log(&logger, LogLevel::Trace, "first"); + CHECK(TestPrints::Read(local_instance).empty()); + + SECTION("create log within log limit") { + Logger::Log(&logger, LogLevel::Info, "second"); + auto prints = TestPrints::Read(local_instance); + REQUIRE(prints.size() == 1); + CHECK(prints[0] == "INFO (OwnSinkLogger): second"); + + SECTION("increase log limit create log within log limit") { + logger.SetLogLimit(LogLevel::Trace); + Logger::Log(&logger, LogLevel::Trace, "third"); + auto prints = TestPrints::Read(local_instance); + REQUIRE(prints.size() == 2); + CHECK(prints[1] == "TRACE (OwnSinkLogger): third"); + + SECTION("log via lambda function") { + Logger::Log(&logger, LogLevel::Trace, [] { + return std::string{"forth"}; + }); + auto prints = TestPrints::Read(local_instance); + REQUIRE(prints.size() == 3); + CHECK(prints[2] == "TRACE (OwnSinkLogger): forth"); + } + } + } + } +} |