From aa6f732844e8996f0384c6a0dd71da134d76cea0 Mon Sep 17 00:00:00 2001 From: Gianluca Martino Date: Fri, 10 Mar 2023 14:48:02 +0100 Subject: [PATCH] Added support also for spdlog::log functions. Added overloading for std::source_location in logger class. --- include/spdlog/logger.h | 31 +++++++++++++++ include/spdlog/spdlog.h | 84 ++++++++++++++++++++++++++++++----------- 2 files changed, 93 insertions(+), 22 deletions(-) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 1fafdabd..f321ad5d 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -27,6 +27,13 @@ #include +#ifdef __has_include +# if __has_include() && __cplusplus >= 202002L +# include +# define SPDLOG_SOURCE_LOCATION +# endif +#endif + #ifndef SPDLOG_NO_EXCEPTIONS # define SPDLOG_LOGGER_CATCH(location) \ catch (const std::exception &ex) \ @@ -90,6 +97,14 @@ public: log_(loc, lvl, details::to_string_view(fmt), std::forward(args)...); } +#ifdef SPDLOG_SOURCE_LOCATION + template + void log(std::source_location loc, level::level_enum lvl, format_string_t fmt, Args &&... args) + { + log_(spdlog::source_loc{loc.file_name(), static_cast(loc.line()), loc.function_name()}, lvl, details::to_string_view(fmt), std::forward(args)...); + } +#endif + template void log(level::level_enum lvl, format_string_t fmt, Args &&... args) { @@ -109,6 +124,14 @@ public: log(loc, lvl, "{}", msg); } +#ifdef SPDLOG_SOURCE_LOCATION + template + void log(std::source_location loc, level::level_enum lvl, const T &msg) + { + log(spdlog::source_loc{loc.file_name(), static_cast(loc.line()), loc.function_name()}, lvl, msg); + } +#endif + void log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, string_view_t msg) { bool log_enabled = should_log(lvl); @@ -183,6 +206,14 @@ public: log_(loc, lvl, details::to_string_view(fmt), std::forward(args)...); } +# ifdef SPDLOG_SOURCE_LOCATION + template + void log(std::source_location loc, level::level_enum lvl, wformat_string_t fmt, Args &&... args) + { + log_(spdlog::source_loc{loc.file_name(), static_cast(loc.line()), loc.function_name()}, lvl, details::to_string_view(fmt), std::forward(args)...); + } +# endif + template void log(level::level_enum lvl, wformat_string_t fmt, Args &&... args) { diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 720d7a29..7e3532c3 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -20,13 +20,6 @@ #include #include -#ifdef __has_include -# if __has_include() && __cplusplus >= 202002L -# include -# define SPDLOG_SOURCE_LOCATION -# endif -#endif - namespace spdlog { using default_factory = synchronous_factory; @@ -147,6 +140,7 @@ SPDLOG_API void set_default_logger(std::shared_ptr default_logge // spdlog::apply_logger_env_levels(mylogger); SPDLOG_API void apply_logger_env_levels(std::shared_ptr logger); +#ifndef SPDLOG_SOURCE_LOCATION template inline void log(source_loc source, level::level_enum lvl, format_string_t fmt, Args &&... args) { @@ -159,7 +153,6 @@ inline void log(level::level_enum lvl, format_string_t fmt, Args &&... default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward(args)...); } -#ifndef SPDLOG_SOURCE_LOCATION template inline void trace(format_string_t fmt, Args &&... args) { @@ -196,6 +189,18 @@ inline void critical(format_string_t fmt, Args &&... args) default_logger_raw()->critical(fmt, std::forward(args)...); } +template +inline void log(source_loc source, level::level_enum lvl, const T &msg) +{ + default_logger_raw()->log(source, lvl, msg); +} + +template +inline void log(level::level_enum lvl, const T &msg) +{ + default_logger_raw()->log(lvl, msg); +} + template inline void trace(const T &msg) { @@ -231,21 +236,7 @@ inline void critical(const T &msg) { default_logger_raw()->critical(msg); } -#endif - -template -inline void log(source_loc source, level::level_enum lvl, const T &msg) -{ - default_logger_raw()->log(source, lvl, msg); -} -template -inline void log(level::level_enum lvl, const T &msg) -{ - default_logger_raw()->log(lvl, msg); -} - -#ifndef SPDLOG_SOURCE_LOCATION # ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT template inline void log(source_loc source, level::level_enum lvl, wformat_string_t fmt, Args &&... args) @@ -298,6 +289,27 @@ inline void critical(wformat_string_t fmt, Args &&... args) #endif #ifdef SPDLOG_SOURCE_LOCATION +template +struct log +{ + log(level::level_enum lvl, format_string_t fmt, Args &&...args, const std::source_location &loc = std::source_location::current()) + { + default_logger_raw()->log(spdlog::source_loc{loc.file_name(), static_cast(loc.line()), loc.function_name()}, lvl, fmt, std::forward(args)...); + } + + log(source_loc source, level::level_enum lvl, format_string_t fmt, Args &&... args) + { + default_logger_raw()->log(source, lvl, fmt, std::forward(args)...); + } + +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + log(level::level_enum lvl, wformat_string_t fmt, Args &&... args, const std::source_location &loc = std::source_location::current()) + { + default_logger_raw()->log(spdlog::source_loc{loc.file_name(), static_cast(loc.line()), loc.function_name()}, lvl, fmt, std::forward(args)...); + } +# endif +}; + template struct trace { @@ -394,6 +406,9 @@ struct critical # endif }; +template +log(level::level_enum lvl, format_string_t fmt, Args &&... args) -> log; + template trace(format_string_t fmt, Args &&... args) -> trace; @@ -413,6 +428,9 @@ template critical(format_string_t fmt, Args &&... args) -> critical; # ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT +template +log(level::level_enum lvl, wformat_string_t fmt, Args &&... args) -> log; + template trace(wformat_string_t fmt, Args &&... args) -> trace; @@ -432,6 +450,25 @@ template critical(wformat_string_t fmt, Args &&... args) -> critical; # endif +template +struct log +{ + log(source_loc source, level::level_enum lvl, const T &msg) + { + default_logger_raw()->log(source, lvl, msg); + } + + log(level::level_enum lvl, const T &msg, const std::source_location &loc = std::source_location::current()) + { + default_logger_raw()->log(spdlog::source_loc{loc.file_name(), static_cast(loc.line()), loc.function_name()}, lvl, msg); + } + + log(level::level_enum lvl, format_string_t fmt, T&& arg, const std::source_location &loc = std::source_location::current()) + { + default_logger_raw()->log(spdlog::source_loc{loc.file_name(), static_cast(loc.line()), loc.function_name()}, lvl, fmt, std::forward(arg)); + } +}; + template struct trace { @@ -516,6 +553,9 @@ struct critical } }; +template +log(level::level_enum lvl, const T &msg) -> log; + template trace(const T &msg) -> trace;