From fa0b243ab01896e9e552bf78298c25a827b8ba5a Mon Sep 17 00:00:00 2001 From: Leonid Stryzhevskyi Date: Wed, 15 May 2024 02:37:06 +0300 Subject: [PATCH] base logging use templates instead of macro --- src/oatpp/base/Log.cpp | 4 ++-- src/oatpp/base/Log.hpp | 52 ++++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/oatpp/base/Log.cpp b/src/oatpp/base/Log.cpp index 2a1f6326..1e98ccff 100644 --- a/src/oatpp/base/Log.cpp +++ b/src/oatpp/base/Log.cpp @@ -288,11 +288,11 @@ LogMessage& LogMessage::operator << (const Float64& value) { return *this; } -void LogMessage::log(v_uint32 priority, const std::string& tag, const LogMessage& message) { +void Log::log(v_uint32 priority, const std::string& tag, const LogMessage& message) { oatpp::Environment::log(priority, tag, message.toStdString()); } -void LogMessage::log(v_uint32 priority, const LogCategory& category, const LogMessage& message) { +void Log::log(v_uint32 priority, const LogCategory& category, const LogMessage& message) { if (category.categoryEnabled && (category.enabledPriorities & (1U << priority))) { log(priority, category.tag, message); } diff --git a/src/oatpp/base/Log.hpp b/src/oatpp/base/Log.hpp index ea830c01..1a4f3cce 100644 --- a/src/oatpp/base/Log.hpp +++ b/src/oatpp/base/Log.hpp @@ -28,7 +28,7 @@ #include "oatpp/data/stream/BufferStream.hpp" #include "oatpp/macro/basic.hpp" -namespace oatpp { namespace base{ +namespace oatpp { namespace base { class LogMessage { private: @@ -77,7 +77,27 @@ public: LogMessage& operator << (const Float32& value); LogMessage& operator << (const Float64& value); -public: +}; + +struct Log { + + static void ignore(std::initializer_list list) { + (void) list; + } + + template + static void stream(v_uint32 priority, const std::string& tag, const oatpp::String& message, Types... args) { + oatpp::base::LogMessage msg(message); + ignore({std::addressof(msg << args)...}); + log(priority, tag, msg); + } + + template + static void stream(v_uint32 priority, const LogCategory& category, const oatpp::String& message, Types... args) { + oatpp::base::LogMessage msg(message); + ignore({std::addressof(msg << args)...}); + log(priority, category, msg); + } static void log(v_uint32 priority, const std::string& tag, const LogMessage& message); static void log(v_uint32 priority, const LogCategory& category, const LogMessage& message); @@ -86,24 +106,6 @@ public: }} -#define OATPP_LOG_PARAMS(IDX, CNT, P) \ -<< P - -#define OATPP_LOG_MACRO_0(PRIORITY, TAG, MSG) \ -oatpp::base::LogMessage::log(PRIORITY, TAG, oatpp::base::LogMessage(MSG)); - -#define OATPP_LOG_MACRO_1(PRIORITY, TAG, MSG, ...) \ -oatpp::base::LogMessage::log(PRIORITY, TAG, oatpp::base::LogMessage(MSG) OATPP_MACRO_FOREACH_OR_EMPTY(OATPP_LOG_PARAMS, __VA_ARGS__)); - -/** - * General LOG macro - * @param PRIORITY - Http method ("GET", "POST", "PUT", etc.) - * @param TAG - Path to endpoint (without host) - * @param MSG - Name of the generated method - */ -#define OATPP_LOG(PRIORITY, TAG, ...) \ -OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_BINARY_SELECTOR(OATPP_LOG_MACRO_, (__VA_ARGS__)) (PRIORITY, TAG, __VA_ARGS__)) - //////////////////////////// //////////////////////////// //////////////////////////// @@ -118,7 +120,7 @@ OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_BINARY_SELECTOR(OATPP_LOG_MACRO_, (__VA_ARG * @param ... - optional format parameter. */ #define OATPP_LOGv(TAG, ...) \ - OATPP_LOG(oatpp::Logger::PRIORITY_V, TAG, __VA_ARGS__) + oatpp::base::Log::stream(oatpp::Logger::PRIORITY_V, TAG, __VA_ARGS__); #else #define OATPP_LOGv(TAG, ...) @@ -134,7 +136,7 @@ OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_BINARY_SELECTOR(OATPP_LOG_MACRO_, (__VA_ARG * @param ... - optional format parameter. */ #define OATPP_LOGd(TAG, ...) \ - OATPP_LOG(oatpp::Logger::PRIORITY_D, TAG, __VA_ARGS__) + oatpp::base::Log::stream(oatpp::Logger::PRIORITY_D, TAG, __VA_ARGS__); #else #define OATPP_LOGd(TAG, ...) @@ -150,7 +152,7 @@ OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_BINARY_SELECTOR(OATPP_LOG_MACRO_, (__VA_ARG * @param ... - optional format parameter. */ #define OATPP_LOGi(TAG, ...) \ - OATPP_LOG(oatpp::Logger::PRIORITY_I, TAG, __VA_ARGS__) + oatpp::base::Log::stream(oatpp::Logger::PRIORITY_I, TAG, __VA_ARGS__); #else #define OATPP_LOGi(TAG, ...) @@ -166,7 +168,7 @@ OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_BINARY_SELECTOR(OATPP_LOG_MACRO_, (__VA_ARG * @param ... - optional format parameter. */ #define OATPP_LOGw(TAG, ...) \ - OATPP_LOG(oatpp::Logger::PRIORITY_W, TAG, __VA_ARGS__) + oatpp::base::Log::stream(oatpp::Logger::PRIORITY_W, TAG, __VA_ARGS__); #else #define OATPP_LOGw(TAG, ...) @@ -182,7 +184,7 @@ OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_BINARY_SELECTOR(OATPP_LOG_MACRO_, (__VA_ARG * @param ... - optional format parameter. */ #define OATPP_LOGe(TAG, ...) \ - OATPP_LOG(oatpp::Logger::PRIORITY_E, TAG, __VA_ARGS__) + oatpp::base::Log::stream(oatpp::Logger::PRIORITY_E, TAG, __VA_ARGS__); #else #define OATPP_LOGe(TAG, ...)