diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 8e6b7e1d..435e7ba8 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -185,6 +185,31 @@ private: spdlog::details::source_location loc_; }; +template +struct value_wrapper +{ + SPDLOG_CONSTEVAL value_wrapper(const T& value, details::source_location loc = details::source_location::current()) + : value_{value} + , loc_{loc} + {} + T value() + { + return value_; + } + source_loc loc() + { + return source_loc{loc_.file_name(), loc_.line(), loc_.function_name()}; + } + operator T() + { + return value(); + } + +private: + T value_; + spdlog::details::source_location loc_; +}; + namespace sinks { class sink; } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index b8269304..61ee3e30 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -98,9 +98,9 @@ public: } template - void log(level::level_enum lvl, const T &msg) + void log(level::level_enum lvl, const value_wrapper &msg) { - log(source_loc{}, lvl, msg); + log(msg.loc(), lvl, msg.value()); } // T cannot be statically converted to format string (including string_view/wstring_view) @@ -136,9 +136,9 @@ public: log_it_(log_msg, log_enabled, traceback_enabled); } - void log(level::level_enum lvl, string_view_t msg) + void log(level::level_enum lvl, value_wrapper msg) { - log(source_loc{}, lvl, msg); + log(msg.loc(), lvl, msg.value()); } template @@ -263,39 +263,39 @@ public: #endif template - void trace(const T &msg, details::source_location loc = details::source_location::current()) + void trace(const value_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::trace, msg); + log(msg.loc(), level::trace, msg.value()); } template - void debug(const T &msg, details::source_location loc = details::source_location::current()) + void debug(const value_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::debug, msg); + log(msg.value(), level::debug, msg.value()); } template - void info(const T &msg, details::source_location loc = details::source_location::current()) + void info(const value_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::info, msg); + log(msg.loc(), level::info, msg.value()); } template - void warn(const T &msg, details::source_location loc = details::source_location::current()) + void warn(const value_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::warn, msg); + log(msg.loc(), level::warn, msg.value()); } template - void error(const T &msg, details::source_location loc = details::source_location::current()) + void error(const value_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::err, msg); + log(msg.loc(), level::err, msg.value()); } template - void critical(const T &msg, details::source_location loc = details::source_location::current()) + void critical(const value_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::critical, msg); + log(msg.loc(), level::critical, msg.value()); } // return true logging is enabled for the given level. diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 25c21dae..1fdf0e78 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -252,39 +252,39 @@ inline void critical(wformat_string_t fmt, Args &&... args) #endif template -inline void trace(const T &msg, details::source_location loc = details::source_location::current()) +inline void trace(const value_wrapper &msg) { - default_logger_raw()->trace(msg, loc); + default_logger_raw()->trace(msg); } template -inline void debug(const T &msg, details::source_location loc = details::source_location::current()) +inline void debug(const value_wrapper &msg) { - default_logger_raw()->debug(msg, loc); + default_logger_raw()->debug(msg); } template -inline void info(const T &msg, details::source_location loc = details::source_location::current()) +inline void info(const value_wrapper &msg) { - default_logger_raw()->info(msg, loc); + default_logger_raw()->info(msg); } template -inline void warn(const T &msg, details::source_location loc = details::source_location::current()) +inline void warn(const value_wrapper &msg) { - default_logger_raw()->warn(msg, loc); + default_logger_raw()->warn(msg); } template -inline void error(const T &msg, details::source_location loc = details::source_location::current()) +inline void error(const value_wrapper &msg) { - default_logger_raw()->error(msg, loc); + default_logger_raw()->error(msg); } template -inline void critical(const T &msg, details::source_location loc = details::source_location::current()) +inline void critical(const value_wrapper &msg) { - default_logger_raw()->critical(msg, loc); + default_logger_raw()->critical(msg); } } // namespace spdlog