From 98e0f1f650b910a30ddcd12989393af8943d561c Mon Sep 17 00:00:00 2001 From: Razvan Cojocaru Date: Tue, 6 Aug 2024 16:56:40 +0300 Subject: [PATCH] logger: De-duplicate Logger::log_{trace, info, ...} logic Add a single template function implementing the logging logic, parametrized by log level, and have the log_{trace, info, ...} functions call that. While at it, const-ify a couple of member functions. Signed-off-by: Razvan Cojocaru --- openvpn/log/logger.hpp | 52 ++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/openvpn/log/logger.hpp b/openvpn/log/logger.hpp index 801770f9..278a7fa7 100644 --- a/openvpn/log/logger.hpp +++ b/openvpn/log/logger.hpp @@ -67,7 +67,7 @@ class Logger //! return the current logging level for all logging - int log_level() + int log_level() const { return current_log_level; } @@ -78,9 +78,8 @@ class Logger current_log_level = level; } - //! return the current prefix all logging - std::string log_prefix() + std::string log_prefix() const { return prefix_; } @@ -91,6 +90,21 @@ class Logger prefix_ = prefix; } + /** + * Prints a log message for tracing if the log level + * is at least LEVEL. + * @param msg the message to print + */ + template + void log(T &&msg) + { + /* this ensures that the function is empty if MAX_LEVEL excludes this level */ + if constexpr (max_log_level >= LEVEL) + { + if (current_log_level >= LEVEL) + OPENVPN_LOG(prefix_ << std::forward(msg)); + } + } /** * Prints a log message for tracing if the log level @@ -100,12 +114,7 @@ class Logger template void log_trace(T &&msg) { - /* this ensures that the function is empty if MAX_LEVEL excludes this level */ - if constexpr (max_log_level >= LOG_LEVEL_TRACE) - { - if (current_log_level >= LOG_LEVEL_TRACE) - OPENVPN_LOG(prefix_ << msg); - } + log(std::forward(msg)); } /** @@ -116,15 +125,9 @@ class Logger template void log_debug(T &&msg) { - /* this ensures that the function is empty if MAX_LEVEL excludes this level */ - if constexpr (max_log_level >= LOG_LEVEL_DEBUG) - { - if (current_log_level >= LOG_LEVEL_DEBUG) - OPENVPN_LOG(prefix_ << msg); - } + log(std::forward(msg)); } - /** * Prints a log message for general info if the log level * is at least INFO (=1) @@ -133,12 +136,7 @@ class Logger template void log_info(T &&msg) { - /* this ensures that the function is empty if MAX_LEVEL excludes this level */ - if constexpr (max_log_level >= LOG_LEVEL_INFO) - { - if (current_log_level >= LOG_LEVEL_INFO) - OPENVPN_LOG(prefix_ << msg); - } + log(std::forward(msg)); } /** @@ -148,12 +146,7 @@ class Logger template void log_verbose(T &&msg) { - /* this ensures that the function is empty if MAX_LEVEL excludes this level */ - if constexpr (max_log_level >= LOG_LEVEL_VERB) - { - if (current_log_level >= LOG_LEVEL_VERB) - OPENVPN_LOG(prefix_ << msg); - } + log(std::forward(msg)); } /** @@ -163,8 +156,7 @@ class Logger template void log_error(T &&msg) { - if (current_log_level >= LOG_LEVEL_ERROR) - OPENVPN_LOG(prefix_ << msg); + log(std::forward(msg)); } protected: