From d5f4d79e1a73d3edc48d06574907f18cc57ec15a Mon Sep 17 00:00:00 2001 From: Gianluca Martino Date: Thu, 9 Mar 2023 13:38:50 +0100 Subject: [PATCH] Include source_location only after checking if source_location exists in the std library used. Implementation extended to all logging levels. --- include/spdlog/spdlog.h | 200 ++++++++++++++++++++++++++++------------ tests/test_misc.cpp | 4 +- 2 files changed, 145 insertions(+), 59 deletions(-) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index e25d4338..6efab462 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -19,8 +19,12 @@ #include #include #include -#if __cplusplus >= 202002L -#include + +#ifdef __has_include +# if __has_include() +# include +# define SPDLOG_SOURCE_LOCATION +# endif #endif namespace spdlog { @@ -155,6 +159,7 @@ 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) { @@ -167,46 +172,12 @@ inline void debug(format_string_t fmt, Args &&... args) default_logger_raw()->debug(fmt, std::forward(args)...); } -#if __cplusplus >= 202002L -template -struct info -{ - info(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()}, level::info, fmt, std::forward(args)...); - } - -# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT - info(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()}, level::info, fmt, std::forward(args)...); - } -# endif -}; - -template -info(format_string_t fmt, Args &&... args) -> info; - -# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT -template -info(wformat_string_t fmt, Args &&... args) -> info; -# endif -#else template inline void info(format_string_t fmt, Args &&... args) { default_logger_raw()->info(fmt, std::forward(args)...); } -# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT -template -inline void info(wformat_string_t fmt, Args &&... args) -{ - default_logger_raw()->info(fmt, std::forward(args)...); -} -# endif -#endif - template inline void warn(format_string_t fmt, Args &&... args) { @@ -224,6 +195,7 @@ inline void critical(format_string_t fmt, Args &&... args) { default_logger_raw()->critical(fmt, std::forward(args)...); } +#endif template inline void log(source_loc source, level::level_enum lvl, const T &msg) @@ -237,7 +209,8 @@ inline void log(level::level_enum lvl, const T &msg) default_logger_raw()->log(lvl, msg); } -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT +#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) { @@ -262,6 +235,12 @@ inline void debug(wformat_string_t fmt, Args &&... args) default_logger_raw()->debug(fmt, std::forward(args)...); } +template +inline void info(wformat_string_t fmt, Args &&... args) +{ + default_logger_raw()->info(fmt, std::forward(args)...); +} + template inline void warn(wformat_string_t fmt, Args &&... args) { @@ -279,37 +258,144 @@ inline void critical(wformat_string_t fmt, Args &&... args) { default_logger_raw()->critical(fmt, std::forward(args)...); } +# endif #endif -template -inline void trace(const T &msg) +#ifdef SPDLOG_SOURCE_LOCATION +template +struct trace { - default_logger_raw()->trace(msg); -} + trace(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()}, level::trace, fmt, std::forward(args)...); + } -template -inline void debug(const T &msg) +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + trace(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()}, level::trace, fmt, std::forward(args)...); + } +# endif +}; + +template +struct debug { - default_logger_raw()->debug(msg); -} + debug(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()}, level::debug, fmt, std::forward(args)...); + } -template -inline void warn(const T &msg) +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + debug(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()}, level::debug, fmt, std::forward(args)...); + } +# endif +}; + +template +struct info { - default_logger_raw()->warn(msg); -} + info(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()}, level::info, fmt, std::forward(args)...); + } -template -inline void error(const T &msg) +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + info(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()}, level::info, fmt, std::forward(args)...); + } +# endif +}; + +template +struct warn { - default_logger_raw()->error(msg); -} + warn(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()}, level::warn, fmt, std::forward(args)...); + } -template -inline void critical(const T &msg) +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + warn(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()}, level::warn, fmt, std::forward(args)...); + } +# endif +}; + +template +struct error { - default_logger_raw()->critical(msg); -} + error(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()}, level::err, fmt, std::forward(args)...); + } + +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + error(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()}, level::err, fmt, std::forward(args)...); + } +# endif +}; + +template +struct critical +{ + critical(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()}, level::critical, fmt, std::forward(args)...); + } + +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + critical(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()}, level::critical, fmt, std::forward(args)...); + } +# endif +}; + +template +trace(format_string_t fmt, Args &&... args) -> trace; + +template +debug(format_string_t fmt, Args &&... args) -> debug; + +template +info(format_string_t fmt, Args &&... args) -> info; + +template +warn(format_string_t fmt, Args &&... args) -> warn; + +template +error(format_string_t fmt, Args &&... args) -> error; + +template +critical(format_string_t fmt, Args &&... args) -> critical; + +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT +template +trace(wformat_string_t fmt, Args &&... args) -> trace; + +template +debug(wformat_string_t fmt, Args &&... args) -> debug; + +template +info(wformat_string_t fmt, Args &&... args) -> info; + +template +warn(wformat_string_t fmt, Args &&... args) -> warn; + +template +error(wformat_string_t fmt, Args &&... args) -> error; + +template +critical(wformat_string_t fmt, Args &&... args) -> critical; +# endif +#endif } // namespace spdlog diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index b6807154..6c51cd3d 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -255,11 +255,11 @@ TEST_CASE("default logger API", "[default logger]") REQUIRE(oss.str() == "*** Hello again 2" + std::string(spdlog::details::os::default_eol)); oss.str(""); - spdlog::error(123); + spdlog::error("{}", 123); REQUIRE(oss.str() == "*** 123" + std::string(spdlog::details::os::default_eol)); oss.str(""); - spdlog::critical(std::string("some string")); + spdlog::critical("some string"); REQUIRE(oss.str() == "*** some string" + std::string(spdlog::details::os::default_eol)); oss.str("");