diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 1bf57f97..e98dafc7 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 a8afbcec..c8045c73 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 a47a9076..573b72bf 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__