summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-03-13 14:54:09 +0100
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-03-19 10:31:33 +0100
commit70bf05ffeb9ef01a72a150abc3e3a4b89f451117 (patch)
tree38b794d90075d687e35ece05707e13becc010c7b
parent435e473e75f5892b69cf4ad904c3ccf19f8f79dd (diff)
downloadjustbuild-70bf05ffeb9ef01a72a150abc3e3a4b89f451117.tar.gz
logger: Add common methods for global and named loggers
This allows to be explicit and thus have better control on where messages get logged.
-rw-r--r--src/buildtool/logging/logger.hpp38
-rw-r--r--test/buildtool/logging/logger.test.cpp72
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");
+ }
+ }
+ }
+ }
+}