Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better integrate with std::source_location #2973

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions include/spdlog/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstdint>
#include <exception>
#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
#include <type_traits>

#ifdef __has_include
#if __has_include(<version>)
#include <version>
#endif
#endif

#ifdef SPDLOG_USE_STD_FORMAT
#include <version>
#if __cpp_lib_format >= 202207L
#include <format>
#else
Expand Down Expand Up @@ -101,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
Expand Down Expand Up @@ -314,14 +326,21 @@ 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} {}

#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};
int line{0};
std::uint_least32_t line{0};
const char *funcname{nullptr};
};

Expand Down
9 changes: 7 additions & 2 deletions include/spdlog/spdlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
3 changes: 2 additions & 1 deletion include/spdlog/tweakme.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down