Use std::source_location to construct source_loc if available

Signed-off-by: Stephan Lachnit <stephanlachnit@debian.org>
pull/2973/head
Stephan Lachnit 2 years ago
parent cea472eb04
commit 73b5c65b71
No known key found for this signature in database
GPG Key ID: B35B49EA5D563EFE

@ -107,7 +107,13 @@
#endif
#ifndef SPDLOG_FUNCTION
#define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
// use std::source_location instead of macros if available
#ifdef __cpp_lib_source_location
#define SPDLOG_STD_SOURCE_LOCATION
#include <source_location>
#else
#define SPDLOG_FUNCTION static_cast<const char *>(__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};

@ -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__)

@ -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__

Loading…
Cancel
Save