From ce6d21c36d77d3213bc6795a77fbe8ebc68d0eaf Mon Sep 17 00:00:00 2001 From: Stephan Lachnit Date: Mon, 8 Jan 2024 22:54:48 +0100 Subject: [PATCH 1/3] Always include if available Signed-off-by: Stephan Lachnit --- include/spdlog/common.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 1269c14af..f6fb0fe74 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -16,8 +16,13 @@ #include #include +#ifdef __has_include + #if __has_include() + #include + #endif +#endif + #ifdef SPDLOG_USE_STD_FORMAT - #include #if __cpp_lib_format >= 202207L #include #else From cea472eb04ba280f7b8eb8ed1036616b87d1e2f0 Mon Sep 17 00:00:00 2001 From: Stephan Lachnit Date: Mon, 8 Jan 2024 23:00:23 +0100 Subject: [PATCH 2/3] source_loc: use std::uint_least32_t to improve support with std::source_location Signed-off-by: Stephan Lachnit --- include/spdlog/common.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index f6fb0fe74..1bf57f973 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -319,14 +320,14 @@ class SPDLOG_API spdlog_ex : public std::exception { struct source_loc { SPDLOG_CONSTEXPR source_loc() = default; - SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in) + SPDLOG_CONSTEXPR source_loc(const char *filename_in, std::uint_least32_t line_in, const char *funcname_in) : filename{filename_in}, line{line_in}, funcname{funcname_in} {} SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT { return line == 0; } const char *filename{nullptr}; - int line{0}; + std::uint_least32_t line{0}; const char *funcname{nullptr}; }; From 73b5c65b71629f4e46be934b8cebd6eefa1df38d Mon Sep 17 00:00:00 2001 From: Stephan Lachnit Date: Mon, 8 Jan 2024 23:04:27 +0100 Subject: [PATCH 3/3] Use std::source_location to construct source_loc if available Signed-off-by: Stephan Lachnit --- include/spdlog/common.h | 15 ++++++++++++++- include/spdlog/spdlog.h | 9 +++++++-- include/spdlog/tweakme.h | 3 ++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 1bf57f973..e98dafc78 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -107,7 +107,13 @@ #endif #ifndef SPDLOG_FUNCTION - #define SPDLOG_FUNCTION static_cast(__FUNCTION__) + // use std::source_location instead of macros if available + #ifdef __cpp_lib_source_location + #define SPDLOG_STD_SOURCE_LOCATION + #include + #else + #define SPDLOG_FUNCTION static_cast(__FUNCTION__) + #endif #endif #ifdef SPDLOG_NO_EXCEPTIONS @@ -325,6 +331,13 @@ struct source_loc { line{line_in}, funcname{funcname_in} {} + #ifdef SPDLOG_STD_SOURCE_LOCATION + SPDLOG_CONSTEXPR source_loc(const std::source_location& loc) + : filename{loc.file_name()}, + line{loc.line()}, + funcname{loc.function_name()} {} + #endif + SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT { return line == 0; } const char *filename{nullptr}; std::uint_least32_t line{0}; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index a8afbcec4..c8045c738 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -284,8 +284,13 @@ inline void critical(const T &msg) { // #ifndef SPDLOG_NO_SOURCE_LOC - #define SPDLOG_LOGGER_CALL(logger, level, ...) \ - (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) + #ifdef SPDLOG_STD_SOURCE_LOCATION + #define SPDLOG_LOGGER_CALL(logger, level, ...) \ + (logger)->log(spdlog::source_loc{std::source_location::current()}, level, __VA_ARGS__) + #else + #define SPDLOG_LOGGER_CALL(logger, level, ...) \ + (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) + #endif #else #define SPDLOG_LOGGER_CALL(logger, level, ...) \ (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__) diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index a47a90764..573b72bf9 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -131,7 +131,8 @@ // Uncomment (and change if desired) macro to use for function names. // This is compiler dependent. // __PRETTY_FUNCTION__ might be nicer in clang/gcc, and __FUNCTION__ in msvc. -// Defaults to __FUNCTION__ (should work on all compilers) if not defined. +// Defaults to __FUNCTION__ (should work on all compilers) if not defined and +// std::source_location (introduced in C++20) is not available.. // // #ifdef __PRETTY_FUNCTION__ // # define SPDLOG_FUNCTION __PRETTY_FUNCTION__