From 1d0f778ef961f45b2d4940722cf359d8cd1bf16c Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Mon, 15 Sep 2025 17:14:09 +0300 Subject: [PATCH] test: cover fmt macros? --- CMakeLists.txt | 15 ++ include/logit_cpp/logit/LogMacros.hpp | 207 ++++++++++++++++++ .../formatter/compiler/PatternCompiler.hpp | 12 + include/logit_cpp/logit/utils/LogRecord.hpp | 7 +- .../logit_cpp/logit/utils/VariableValue.hpp | 41 ++++ tests/CMakeLists.txt | 3 + tests/compiled_level_test.cpp | 12 + tests/fmt_macros_test.cpp | 22 ++ 8 files changed, 317 insertions(+), 2 deletions(-) create mode 100644 tests/fmt_macros_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 16dbb5e..7533b11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ option(LOGIT_CPP_BUILD_TESTS "Build log-it-cpp tests" ${PROJECT_IS_TOP_LEVEL}) option(LOGIT_CPP_BUILD_EXAMPLES "Build log-it-cpp examples" OFF) option(LOGIT_WITH_GZIP "Enable gzip via zlib" OFF) option(LOGIT_WITH_ZSTD "Enable zstd" OFF) +option(LOGIT_WITH_FMT "Enable fmt support" OFF) option(LOGIT_USE_SUBMODULES "Allow bundled third_party fallback" OFF) option(LOGIT_WITH_SYSLOG "Enable POSIX syslog backend" ON) option(LOGIT_WITH_WIN_EVENT_LOG "Enable Windows Event Log backend" ON) @@ -62,6 +63,20 @@ if(LOGIT_WITH_WIN_EVENT_LOG AND WIN32) target_link_libraries(log-it-cpp INTERFACE advapi32) endif() +if(LOGIT_WITH_FMT) + if(NOT TARGET fmt::fmt) + find_package(fmt QUIET CONFIG) + endif() + if(NOT TARGET fmt::fmt AND LOGIT_USE_SUBMODULES) + add_subdirectory(libs/fmt EXCLUDE_FROM_ALL) + endif() + if(NOT TARGET fmt::fmt) + message(FATAL_ERROR "fmt not found. Enable LOGIT_USE_SUBMODULES or install fmt.") + endif() + target_compile_definitions(log-it-cpp INTERFACE LOGIT_WITH_FMT=1) + target_link_libraries(log-it-cpp INTERFACE fmt::fmt) +endif() + # ---------- GZIP (zlib) ---------- if(LOGIT_WITH_GZIP) if(NOT TARGET ZLIB::ZLIB) diff --git a/include/logit_cpp/logit/LogMacros.hpp b/include/logit_cpp/logit/LogMacros.hpp index 36e215b..1b4f92a 100644 --- a/include/logit_cpp/logit/LogMacros.hpp +++ b/include/logit_cpp/logit/LogMacros.hpp @@ -2,6 +2,10 @@ #ifndef _LOGIT_LOG_MACROS_HPP_INCLUDED #define _LOGIT_LOG_MACROS_HPP_INCLUDED +#ifdef LOGIT_WITH_FMT +#include +#endif + /// \file LogMacros.hpp /// \brief Provides various logging macros for different log levels and options. @@ -260,6 +264,47 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT } while (0) #endif +//------------------------------------------------------------------------------ +// Macros for logging with fmt formatting + +#if __cplusplus >= 201703L +#define LOGIT_LOG_AND_RETURN_FMT(level, format, arg_names, ...) \ + do { \ + LOGIT_IF_COMPILED_LEVEL(level) \ + logit::Logger::get_instance().log_and_return( \ + logit::LogRecord{level, LOGIT_CURRENT_TIMESTAMP_MS(), \ + logit::make_relative(__FILE__, LOGIT_BASE_PATH), __LINE__, \ + LOGIT_FUNCTION, format, arg_names, -1, false, true}, __VA_ARGS__); \ + } while (0) +#else +#define LOGIT_LOG_AND_RETURN_FMT(level, format, arg_names, ...) \ + do { \ + logit::Logger::get_instance().log_and_return( \ + logit::LogRecord{level, LOGIT_CURRENT_TIMESTAMP_MS(), \ + logit::make_relative(__FILE__, LOGIT_BASE_PATH), __LINE__, \ + LOGIT_FUNCTION, format, arg_names, -1, false, true}, __VA_ARGS__); \ + } while (0) +#endif + +#if __cplusplus >= 201703L +#define LOGIT_LOG_AND_RETURN_FMT_WITH_INDEX(level, index, format, arg_names, ...) \ + do { \ + LOGIT_IF_COMPILED_LEVEL(level) \ + logit::Logger::get_instance().log_and_return( \ + logit::LogRecord{level, LOGIT_CURRENT_TIMESTAMP_MS(), \ + logit::make_relative(__FILE__, LOGIT_BASE_PATH), __LINE__, \ + LOGIT_FUNCTION, format, arg_names, index, false, true}, __VA_ARGS__); \ + } while (0) +#else +#define LOGIT_LOG_AND_RETURN_FMT_WITH_INDEX(level, index, format, arg_names, ...) \ + do { \ + logit::Logger::get_instance().log_and_return( \ + logit::LogRecord{level, LOGIT_CURRENT_TIMESTAMP_MS(), \ + logit::make_relative(__FILE__, LOGIT_BASE_PATH), __LINE__, \ + LOGIT_FUNCTION, format, arg_names, index, false, true}, __VA_ARGS__); \ + } while (0) +#endif + //------------------------------------------------------------------------------ // Macros for each log level @@ -273,6 +318,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_TRACE(fmt, ...) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_TRACE, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_TRACE(...) LOGIT_LOG_AND_RETURN_PRINT(logit::LogLevel::LOG_LVL_TRACE, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_TRACE(fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_TRACE, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_TRACE(fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_TRACE, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_TRACE(fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_TRACE, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_TRACE(fmt_str, ...) LOGIT_PRINTF_TRACE(fmt_str, __VA_ARGS__) +#define LOGIT_FMT_TRACE(fmt_str, ...) LOGIT_FORMAT_TRACE(fmt_str, __VA_ARGS__) +#endif // TRACE macros with index #define LOGIT_TRACE_TO(index, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_TRACE, index, {}, #__VA_ARGS__, __VA_ARGS__) @@ -283,6 +335,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_TRACE_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_TRACE, index, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_TRACE_TO(index, ...) LOGIT_LOG_AND_RETURN_PRINT_WITH_INDEX(logit::LogLevel::LOG_LVL_TRACE, index, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_TRACE_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_TRACE, index, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_TRACE_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_TRACE, index, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_TRACE_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT_WITH_INDEX(logit::LogLevel::LOG_LVL_TRACE, index, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_TRACE_TO(index, fmt_str, ...) LOGIT_PRINTF_TRACE_TO(index, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_TRACE_TO(index, fmt_str, ...) LOGIT_FORMAT_TRACE_TO(index, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_TRACE(...) do { } while (0) #define LOGIT_TRACE0() do { } while (0) @@ -312,6 +371,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_INFO(fmt, ...) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_INFO, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_INFO(...) LOGIT_LOG_AND_RETURN_PRINT(logit::LogLevel::LOG_LVL_INFO, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_INFO(fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_INFO, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_INFO(fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_INFO, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_INFO(fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_INFO, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_INFO(fmt_str, ...) LOGIT_PRINTF_INFO(fmt_str, __VA_ARGS__) +#define LOGIT_FMT_INFO(fmt_str, ...) LOGIT_FORMAT_INFO(fmt_str, __VA_ARGS__) +#endif // INFO macros with index #define LOGIT_INFO_TO(index, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_INFO, index, {}, #__VA_ARGS__, __VA_ARGS__) @@ -322,6 +388,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_INFO_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_INFO, index, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_INFO_TO(index, ...) LOGIT_LOG_AND_RETURN_PRINT_WITH_INDEX(logit::LogLevel::LOG_LVL_INFO, index, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_INFO_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_INFO, index, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_INFO_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_INFO, index, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_INFO_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT_WITH_INDEX(logit::LogLevel::LOG_LVL_INFO, index, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_INFO_TO(index, fmt_str, ...) LOGIT_PRINTF_INFO_TO(index, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_INFO_TO(index, fmt_str, ...) LOGIT_FORMAT_INFO_TO(index, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_INFO(...) do { } while (0) #define LOGIT_INFO0() do { } while (0) @@ -351,6 +424,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_DEBUG(fmt, ...) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_DEBUG, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_DEBUG(...) LOGIT_LOG_AND_RETURN_PRINT(logit::LogLevel::LOG_LVL_DEBUG, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_DEBUG(fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_DEBUG, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_DEBUG(fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_DEBUG, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_DEBUG(fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_DEBUG, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_DEBUG(fmt_str, ...) LOGIT_PRINTF_DEBUG(fmt_str, __VA_ARGS__) +#define LOGIT_FMT_DEBUG(fmt_str, ...) LOGIT_FORMAT_DEBUG(fmt_str, __VA_ARGS__) +#endif // DEBUG macros with index #define LOGIT_DEBUG_TO(index, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_DEBUG, index, {}, #__VA_ARGS__, __VA_ARGS__) @@ -361,6 +441,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_DEBUG_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_DEBUG, index, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_DEBUG_TO(index, ...) LOGIT_LOG_AND_RETURN_PRINT_WITH_INDEX(logit::LogLevel::LOG_LVL_DEBUG, index, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_DEBUG_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_DEBUG, index, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_DEBUG_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_DEBUG, index, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_DEBUG_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT_WITH_INDEX(logit::LogLevel::LOG_LVL_DEBUG, index, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_DEBUG_TO(index, fmt_str, ...) LOGIT_PRINTF_DEBUG_TO(index, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_DEBUG_TO(index, fmt_str, ...) LOGIT_FORMAT_DEBUG_TO(index, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_DEBUG(...) do { } while (0) #define LOGIT_DEBUG0() do { } while (0) @@ -390,6 +477,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_WARN(fmt, ...) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_WARN, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_WARN(...) LOGIT_LOG_AND_RETURN_PRINT(logit::LogLevel::LOG_LVL_WARN, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_WARN(fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_WARN, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_WARN(fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_WARN, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_WARN(fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_WARN, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_WARN(fmt_str, ...) LOGIT_PRINTF_WARN(fmt_str, __VA_ARGS__) +#define LOGIT_FMT_WARN(fmt_str, ...) LOGIT_FORMAT_WARN(fmt_str, __VA_ARGS__) +#endif // WARN macros with index #define LOGIT_WARN_TO(index, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_WARN, index, {}, #__VA_ARGS__, __VA_ARGS__) @@ -400,6 +494,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_WARN_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_WARN, index, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_WARN_TO(index, ...) LOGIT_LOG_AND_RETURN_PRINT_WITH_INDEX(logit::LogLevel::LOG_LVL_WARN, index, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_WARN_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_WARN, index, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_WARN_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_WARN, index, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_WARN_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT_WITH_INDEX(logit::LogLevel::LOG_LVL_WARN, index, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_WARN_TO(index, fmt_str, ...) LOGIT_PRINTF_WARN_TO(index, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_WARN_TO(index, fmt_str, ...) LOGIT_FORMAT_WARN_TO(index, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_WARN(...) do { } while (0) #define LOGIT_WARN0() do { } while (0) @@ -429,6 +530,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_ERROR(fmt, ...) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_ERROR, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_ERROR(...) LOGIT_LOG_AND_RETURN_PRINT(logit::LogLevel::LOG_LVL_ERROR, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_ERROR(fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_ERROR, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_ERROR(fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_ERROR, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_ERROR(fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_ERROR, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_ERROR(fmt_str, ...) LOGIT_PRINTF_ERROR(fmt_str, __VA_ARGS__) +#define LOGIT_FMT_ERROR(fmt_str, ...) LOGIT_FORMAT_ERROR(fmt_str, __VA_ARGS__) +#endif // ERROR macros with index #define LOGIT_ERROR_TO(index, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_ERROR, index, {}, #__VA_ARGS__, __VA_ARGS__) @@ -439,6 +547,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_ERROR_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_ERROR, index, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_ERROR_TO(index, ...) LOGIT_LOG_AND_RETURN_PRINT_WITH_INDEX(logit::LogLevel::LOG_LVL_ERROR, index, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_ERROR_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_ERROR, index, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_ERROR_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_ERROR, index, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_ERROR_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT_WITH_INDEX(logit::LogLevel::LOG_LVL_ERROR, index, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_ERROR_TO(index, fmt_str, ...) LOGIT_PRINTF_ERROR_TO(index, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_ERROR_TO(index, fmt_str, ...) LOGIT_FORMAT_ERROR_TO(index, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_ERROR(...) do { } while (0) #define LOGIT_ERROR0() do { } while (0) @@ -468,6 +583,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_FATAL(fmt, ...) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_FATAL, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_FATAL(...) LOGIT_LOG_AND_RETURN_PRINT(logit::LogLevel::LOG_LVL_FATAL, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_FATAL(fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_FATAL, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_FATAL(fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_FATAL, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_FATAL(fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_FATAL, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_FATAL(fmt_str, ...) LOGIT_PRINTF_FATAL(fmt_str, __VA_ARGS__) +#define LOGIT_FMT_FATAL(fmt_str, ...) LOGIT_FORMAT_FATAL(fmt_str, __VA_ARGS__) +#endif // FATAL macros with index #define LOGIT_FATAL_TO(index, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_FATAL, index, {}, #__VA_ARGS__, __VA_ARGS__) @@ -478,6 +600,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_FATAL_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_WITH_INDEX(logit::LogLevel::LOG_LVL_FATAL, index, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_FATAL_TO(index, ...) LOGIT_LOG_AND_RETURN_PRINT_WITH_INDEX(logit::LogLevel::LOG_LVL_FATAL, index, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINTF_FATAL_TO(index, fmt, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_FATAL, index, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_FATAL_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_NOARGS_WITH_INDEX(logit::LogLevel::LOG_LVL_FATAL, index, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_FATAL_TO(index, fmt_str, ...) LOGIT_LOG_AND_RETURN_FMT_WITH_INDEX(logit::LogLevel::LOG_LVL_FATAL, index, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_FATAL_TO(index, fmt_str, ...) LOGIT_PRINTF_FATAL_TO(index, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_FATAL_TO(index, fmt_str, ...) LOGIT_FORMAT_FATAL_TO(index, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_FATAL(...) do { } while (0) #define LOGIT_FATAL0() do { } while (0) @@ -514,6 +643,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_TRACE_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_TRACE, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_TRACE_IF(condition, fmt) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_TRACE, fmt) #define LOGIT_PRINTF_TRACE_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_TRACE, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_TRACE_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_TRACE, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_TRACE_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_TRACE, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_TRACE_IF(condition, fmt_str, ...) LOGIT_PRINTF_TRACE_IF(condition, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_TRACE_IF(condition, fmt_str, ...) LOGIT_FORMAT_TRACE_IF(condition, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_TRACE_IF(condition, ...) do { } while (0) #define LOGIT_TRACE0_IF(condition) do { } while (0) @@ -523,6 +659,12 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_TRACE_IF(condition, fmt, ...) do { } while (0) #define LOGIT_PRINT_TRACE_IF(condition, fmt) do { } while (0) #define LOGIT_PRINTF_TRACE_IF(condition, fmt, ...) do { } while (0) +#define LOGITF_TRACE(fmt_str, ...) do { } while (0) +#define LOGIT_FMT_TRACE(fmt_str, ...) do { } while (0) +#define LOGITF_TRACE_TO(index, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_TRACE_TO(index, fmt_str, ...) do { } while (0) +#define LOGITF_TRACE_IF(condition, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_TRACE_IF(condition, fmt_str, ...) do { } while (0) #endif #if LOGIT_COMPILED_LEVEL <= LOGIT_LEVEL_INFO @@ -535,6 +677,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_INFO_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_INFO, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_INFO_IF(condition, fmt) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_INFO, fmt) #define LOGIT_PRINTF_INFO_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_INFO, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_INFO_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_INFO, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_INFO_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_INFO, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_INFO_IF(condition, fmt_str, ...) LOGIT_PRINTF_INFO_IF(condition, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_INFO_IF(condition, fmt_str, ...) LOGIT_FORMAT_INFO_IF(condition, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_INFO_IF(condition, ...) do { } while (0) #define LOGIT_INFO0_IF(condition) do { } while (0) @@ -544,6 +693,12 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_INFO_IF(condition, fmt, ...) do { } while (0) #define LOGIT_PRINT_INFO_IF(condition, fmt) do { } while (0) #define LOGIT_PRINTF_INFO_IF(condition, fmt, ...) do { } while (0) +#define LOGITF_INFO(fmt_str, ...) do { } while (0) +#define LOGIT_FMT_INFO(fmt_str, ...) do { } while (0) +#define LOGITF_INFO_TO(index, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_INFO_TO(index, fmt_str, ...) do { } while (0) +#define LOGITF_INFO_IF(condition, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_INFO_IF(condition, fmt_str, ...) do { } while (0) #endif #if LOGIT_COMPILED_LEVEL <= LOGIT_LEVEL_DEBUG @@ -556,6 +711,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_DEBUG_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_DEBUG, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_DEBUG_IF(condition, fmt) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_DEBUG, fmt) #define LOGIT_PRINTF_DEBUG_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_DEBUG, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_DEBUG_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_DEBUG, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_DEBUG_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_DEBUG, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_DEBUG_IF(condition, fmt_str, ...) LOGIT_PRINTF_DEBUG_IF(condition, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_DEBUG_IF(condition, fmt_str, ...) LOGIT_FORMAT_DEBUG_IF(condition, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_DEBUG_IF(condition, ...) do { } while (0) #define LOGIT_DEBUG0_IF(condition) do { } while (0) @@ -565,6 +727,12 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_DEBUG_IF(condition, fmt, ...) do { } while (0) #define LOGIT_PRINT_DEBUG_IF(condition, fmt) do { } while (0) #define LOGIT_PRINTF_DEBUG_IF(condition, fmt, ...) do { } while (0) +#define LOGITF_DEBUG(fmt_str, ...) do { } while (0) +#define LOGIT_FMT_DEBUG(fmt_str, ...) do { } while (0) +#define LOGITF_DEBUG_TO(index, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_DEBUG_TO(index, fmt_str, ...) do { } while (0) +#define LOGITF_DEBUG_IF(condition, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_DEBUG_IF(condition, fmt_str, ...) do { } while (0) #endif #if LOGIT_COMPILED_LEVEL <= LOGIT_LEVEL_WARN @@ -577,6 +745,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_WARN_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_WARN, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_WARN_IF(condition, fmt) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_WARN, fmt) #define LOGIT_PRINTF_WARN_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_WARN, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_WARN_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_WARN, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_WARN_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_WARN, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_WARN_IF(condition, fmt_str, ...) LOGIT_PRINTF_WARN_IF(condition, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_WARN_IF(condition, fmt_str, ...) LOGIT_FORMAT_WARN_IF(condition, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_WARN_IF(condition, ...) do { } while (0) #define LOGIT_WARN0_IF(condition) do { } while (0) @@ -586,6 +761,12 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_WARN_IF(condition, fmt, ...) do { } while (0) #define LOGIT_PRINT_WARN_IF(condition, fmt) do { } while (0) #define LOGIT_PRINTF_WARN_IF(condition, fmt, ...) do { } while (0) +#define LOGITF_WARN(fmt_str, ...) do { } while (0) +#define LOGIT_FMT_WARN(fmt_str, ...) do { } while (0) +#define LOGITF_WARN_TO(index, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_WARN_TO(index, fmt_str, ...) do { } while (0) +#define LOGITF_WARN_IF(condition, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_WARN_IF(condition, fmt_str, ...) do { } while (0) #endif #if LOGIT_COMPILED_LEVEL <= LOGIT_LEVEL_ERROR @@ -598,6 +779,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_ERROR_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_ERROR, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_ERROR_IF(condition, fmt) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_ERROR, fmt) #define LOGIT_PRINTF_ERROR_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_ERROR, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_ERROR_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_ERROR, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_ERROR_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_ERROR, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_ERROR_IF(condition, fmt_str, ...) LOGIT_PRINTF_ERROR_IF(condition, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_ERROR_IF(condition, fmt_str, ...) LOGIT_FORMAT_ERROR_IF(condition, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_ERROR_IF(condition, ...) do { } while (0) #define LOGIT_ERROR0_IF(condition) do { } while (0) @@ -607,6 +795,12 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_ERROR_IF(condition, fmt, ...) do { } while (0) #define LOGIT_PRINT_ERROR_IF(condition, fmt) do { } while (0) #define LOGIT_PRINTF_ERROR_IF(condition, fmt, ...) do { } while (0) +#define LOGITF_ERROR(fmt_str, ...) do { } while (0) +#define LOGIT_FMT_ERROR(fmt_str, ...) do { } while (0) +#define LOGITF_ERROR_TO(index, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_ERROR_TO(index, fmt_str, ...) do { } while (0) +#define LOGITF_ERROR_IF(condition, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_ERROR_IF(condition, fmt_str, ...) do { } while (0) #endif #if LOGIT_COMPILED_LEVEL <= LOGIT_LEVEL_FATAL @@ -619,6 +813,13 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_FATAL_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN(logit::LogLevel::LOG_LVL_FATAL, fmt, #__VA_ARGS__, __VA_ARGS__) #define LOGIT_PRINT_FATAL_IF(condition, fmt) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_FATAL, fmt) #define LOGIT_PRINTF_FATAL_IF(condition, fmt, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_FATAL, logit::format(fmt, __VA_ARGS__)) +#ifdef LOGIT_WITH_FMT +#define LOGITF_FATAL_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_NOARGS(logit::LogLevel::LOG_LVL_FATAL, fmt::format(fmt_str, __VA_ARGS__)) +#define LOGIT_FMT_FATAL_IF(condition, fmt_str, ...) if (condition) LOGIT_LOG_AND_RETURN_FMT(logit::LogLevel::LOG_LVL_FATAL, fmt_str, #__VA_ARGS__, __VA_ARGS__) +#else +#define LOGITF_FATAL_IF(condition, fmt_str, ...) LOGIT_PRINTF_FATAL_IF(condition, fmt_str, __VA_ARGS__) +#define LOGIT_FMT_FATAL_IF(condition, fmt_str, ...) LOGIT_FORMAT_FATAL_IF(condition, fmt_str, __VA_ARGS__) +#endif #else #define LOGIT_FATAL_IF(condition, ...) do { } while (0) #define LOGIT_FATAL0_IF(condition) do { } while (0) @@ -628,6 +829,12 @@ static_assert(LOGIT_LEVEL_FATAL == static_cast(logit::LogLevel::LOG_LVL_FAT #define LOGIT_FORMAT_FATAL_IF(condition, fmt, ...) do { } while (0) #define LOGIT_PRINT_FATAL_IF(condition, fmt) do { } while (0) #define LOGIT_PRINTF_FATAL_IF(condition, fmt, ...) do { } while (0) +#define LOGITF_FATAL(fmt_str, ...) do { } while (0) +#define LOGIT_FMT_FATAL(fmt_str, ...) do { } while (0) +#define LOGITF_FATAL_TO(index, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_FATAL_TO(index, fmt_str, ...) do { } while (0) +#define LOGITF_FATAL_IF(condition, fmt_str, ...) do { } while (0) +#define LOGIT_FMT_FATAL_IF(condition, fmt_str, ...) do { } while (0) #endif #define LOGIT_ONCE(level, ...) \ diff --git a/include/logit_cpp/logit/formatter/compiler/PatternCompiler.hpp b/include/logit_cpp/logit/formatter/compiler/PatternCompiler.hpp index 07c2de6..e1fb5f2 100644 --- a/include/logit_cpp/logit/formatter/compiler/PatternCompiler.hpp +++ b/include/logit_cpp/logit/formatter/compiler/PatternCompiler.hpp @@ -291,14 +291,26 @@ namespace logit { case ValueType::SMART_POINTER_VAL: case ValueType::VARIANT_VAL: case ValueType::OPTIONAL_VAL: +#ifdef LOGIT_WITH_FMT + temp_stream << (record.fmt_mode ? arg.to_string_fmt(record.format.c_str()) : arg.to_string(record.format.c_str())); +#else temp_stream << arg.to_string(record.format.c_str()); +#endif break; default: +#ifdef LOGIT_WITH_FMT + if (arg.is_literal) { + temp_stream << arg.name << ": " << (record.fmt_mode ? arg.to_string_fmt(record.format.c_str()) : arg.to_string(record.format.c_str())); + } else { + temp_stream << (record.fmt_mode ? arg.to_string_fmt(record.format.c_str()) : arg.to_string(record.format.c_str())); + } +#else if (arg.is_literal) { temp_stream << arg.name << ": " << arg.to_string(record.format.c_str()); } else { temp_stream << arg.to_string(record.format.c_str()); } +#endif break; }; } diff --git a/include/logit_cpp/logit/utils/LogRecord.hpp b/include/logit_cpp/logit/utils/LogRecord.hpp index 0f70331..be65e1a 100644 --- a/include/logit_cpp/logit/utils/LogRecord.hpp +++ b/include/logit_cpp/logit/utils/LogRecord.hpp @@ -26,6 +26,7 @@ namespace logit { std::thread::id thread_id; ///< ID of the logging thread. const int logger_index; ///< Logger index (-1 to log to all). const bool print_mode; ///< Flag to determine whether arguments are printed in a raw format without special symbols. + const bool fmt_mode; ///< Flag indicating if fmt formatting should be used. /// \brief Constructor with argument names. /// \param log_level Log severity level. @@ -46,7 +47,8 @@ namespace logit { const std::string& format, const std::string& arg_names, int logger_index, - bool print_mode) : + bool print_mode, + bool fmt_mode = false) : log_level(log_level), timestamp_ms(timestamp_ms), file(file), @@ -56,7 +58,8 @@ namespace logit { arg_names(arg_names), thread_id(std::this_thread::get_id()), logger_index(logger_index), - print_mode(print_mode) { + print_mode(print_mode), + fmt_mode(fmt_mode) { }; }; diff --git a/include/logit_cpp/logit/utils/VariableValue.hpp b/include/logit_cpp/logit/utils/VariableValue.hpp index c57bb56..cc94757 100644 --- a/include/logit_cpp/logit/utils/VariableValue.hpp +++ b/include/logit_cpp/logit/utils/VariableValue.hpp @@ -20,6 +20,9 @@ #include #include #endif +#ifdef LOGIT_WITH_FMT +#include +#endif namespace logit { @@ -376,6 +379,44 @@ namespace logit { return "unknown"; } +#ifdef LOGIT_WITH_FMT + /// \brief Method to get the value as a formatted string using fmt. + /// \param fmt The fmt format string. + /// \return Formatted string representation of the value. + std::string to_string_fmt(const char* fmt) const { + switch (type) { + case ValueType::INT8_VAL: return fmt::format(fmt, pod_value.int8_value); + case ValueType::UINT8_VAL: return fmt::format(fmt, pod_value.uint8_value); + case ValueType::INT16_VAL: return fmt::format(fmt, pod_value.int16_value); + case ValueType::UINT16_VAL: return fmt::format(fmt, pod_value.uint16_value); + case ValueType::INT32_VAL: return fmt::format(fmt, pod_value.int32_value); + case ValueType::UINT32_VAL: return fmt::format(fmt, pod_value.uint32_value); + case ValueType::INT64_VAL: return fmt::format(fmt, pod_value.int64_value); + case ValueType::UINT64_VAL: return fmt::format(fmt, pod_value.uint64_value); + case ValueType::BOOL_VAL: return fmt::format(fmt, pod_value.bool_value); + case ValueType::CHAR_VAL: return fmt::format(fmt, pod_value.char_value); + case ValueType::FLOAT_VAL: return fmt::format(fmt, pod_value.float_value); + case ValueType::DOUBLE_VAL: return fmt::format(fmt, pod_value.double_value); + case ValueType::LONG_DOUBLE_VAL: return fmt::format(fmt, static_cast(pod_value.long_double_value)); + case ValueType::STRING_VAL: + case ValueType::EXCEPTION_VAL: + case ValueType::ENUM_VAL: + case ValueType::PATH_VAL: + case ValueType::DURATION_VAL: + case ValueType::TIME_POINT_VAL: + case ValueType::POINTER_VAL: + case ValueType::SMART_POINTER_VAL: + case ValueType::VARIANT_VAL: + case ValueType::OPTIONAL_VAL: + return fmt::format(fmt, string_value); + case ValueType::ERROR_CODE_VAL: + return fmt::format(fmt, error_code_value.message(), error_code_value.value()); + default: break; + } + return "unknown"; + } +#endif + private: /// \brief Helper function to check if a name is a valid literal. /// \param name The name to check. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 034bce0..823b2f4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,6 +13,9 @@ else() if(NOT LOGIT_WITH_ZSTD) list(REMOVE_ITEM TEST_SOURCES ${CMAKE_CURRENT_LIST_DIR}/file_logger_zstd_compression_test.cpp) endif() + if(NOT LOGIT_WITH_FMT) + list(REMOVE_ITEM TEST_SOURCES ${CMAKE_CURRENT_LIST_DIR}/fmt_macros_test.cpp) + endif() foreach(test_src ${TEST_SOURCES}) get_filename_component(test_name ${test_src} NAME_WE) add_executable(${test_name} ${test_src}) diff --git a/tests/compiled_level_test.cpp b/tests/compiled_level_test.cpp index 6eadbfe..102a865 100644 --- a/tests/compiled_level_test.cpp +++ b/tests/compiled_level_test.cpp @@ -17,6 +17,8 @@ int main() { LOGIT_FORMAT_TRACE("%d", should_not_compile()); LOGIT_PRINT_TRACE(should_not_compile()); LOGIT_PRINTF_TRACE("%d", should_not_compile()); + LOGITF_TRACE("{}", should_not_compile()); + LOGIT_FMT_TRACE("{}", should_not_compile()); LOGIT_TRACE_TO(0, should_not_compile()); LOGIT_TRACE0_TO(0); LOGIT_0TRACE_TO(0); @@ -25,6 +27,8 @@ int main() { LOGIT_FORMAT_TRACE_TO(0, "%d", should_not_compile()); LOGIT_PRINT_TRACE_TO(0, should_not_compile()); LOGIT_PRINTF_TRACE_TO(0, "%d", should_not_compile()); + LOGITF_TRACE_TO(0, "{}", should_not_compile()); + LOGIT_FMT_TRACE_TO(0, "{}", should_not_compile()); LOGIT_TRACE_IF(false, should_not_compile()); LOGIT_TRACE0_IF(false); LOGIT_0TRACE_IF(false); @@ -33,6 +37,8 @@ int main() { LOGIT_FORMAT_TRACE_IF(false, "%d", should_not_compile()); LOGIT_PRINT_TRACE_IF(false, should_not_compile()); LOGIT_PRINTF_TRACE_IF(false, "%d", should_not_compile()); + LOGITF_TRACE_IF(false, "{}", should_not_compile()); + LOGIT_FMT_TRACE_IF(false, "{}", should_not_compile()); // DEBUG macros LOGIT_DEBUG(should_not_compile()); @@ -43,6 +49,8 @@ int main() { LOGIT_FORMAT_DEBUG("%d", should_not_compile()); LOGIT_PRINT_DEBUG(should_not_compile()); LOGIT_PRINTF_DEBUG("%d", should_not_compile()); + LOGITF_DEBUG("{}", should_not_compile()); + LOGIT_FMT_DEBUG("{}", should_not_compile()); LOGIT_DEBUG_TO(0, should_not_compile()); LOGIT_DEBUG0_TO(0); LOGIT_0DEBUG_TO(0); @@ -51,6 +59,8 @@ int main() { LOGIT_FORMAT_DEBUG_TO(0, "%d", should_not_compile()); LOGIT_PRINT_DEBUG_TO(0, should_not_compile()); LOGIT_PRINTF_DEBUG_TO(0, "%d", should_not_compile()); + LOGITF_DEBUG_TO(0, "{}", should_not_compile()); + LOGIT_FMT_DEBUG_TO(0, "{}", should_not_compile()); LOGIT_DEBUG_IF(false, should_not_compile()); LOGIT_DEBUG0_IF(false); LOGIT_0DEBUG_IF(false); @@ -59,5 +69,7 @@ int main() { LOGIT_FORMAT_DEBUG_IF(false, "%d", should_not_compile()); LOGIT_PRINT_DEBUG_IF(false, should_not_compile()); LOGIT_PRINTF_DEBUG_IF(false, "%d", should_not_compile()); + LOGITF_DEBUG_IF(false, "{}", should_not_compile()); + LOGIT_FMT_DEBUG_IF(false, "{}", should_not_compile()); return 0; } diff --git a/tests/fmt_macros_test.cpp b/tests/fmt_macros_test.cpp new file mode 100644 index 0000000..164df02 --- /dev/null +++ b/tests/fmt_macros_test.cpp @@ -0,0 +1,22 @@ +#define LOGIT_FILE_LOGGER_PATH "." +#include +#include +#include + +int main() { + LOGIT_ADD_FILE_LOGGER_DEFAULT(); + LOGITF_INFO("fmt {}", 42); + int value = 5; + LOGIT_FMT_INFO("{:04}", value); + LOGIT_WAIT(); + std::ifstream in(LOGIT_GET_LAST_FILE_PATH(0)); + std::string line; + bool found_fmt = false; + bool found_fmt_arg = false; + while (std::getline(in, line)) { + if (line.find("fmt 42") != std::string::npos) found_fmt = true; + if (line.find("0005") != std::string::npos) found_fmt_arg = true; + } + LOGIT_SHUTDOWN(); + return (found_fmt && found_fmt_arg) ? 0 : 1; +}