From 70bf05ffeb9ef01a72a150abc3e3a4b89f451117 Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Wed, 13 Mar 2024 14:54:09 +0100 Subject: logger: Add common methods for global and named loggers This allows to be explicit and thus have better control on where messages get logged. --- src/buildtool/logging/logger.hpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/buildtool/logging/logger.hpp') 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 + static void Log(Logger const* logger, + LogLevel level, + std::string const& msg, + T_Args&&... args) noexcept { + if (static_cast(level) <= + static_cast(logger != nullptr ? logger->log_limit_ + : LogConfig::LogLimit())) { + FormatAndForward( + logger, + logger != nullptr ? logger->sinks_ : LogConfig::Sinks(), + level, + msg, + std::forward(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(level) <= + static_cast(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_{}; -- cgit v1.2.3