diff --git a/include/spdlog/common.h b/include/spdlog/common.h index a40b129b..e86eb837 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -172,11 +172,11 @@ struct format_string_wrapper , loc_{loc} {} #endif - T fmt() + T format_string() { return fmt_; } - source_loc loc() + source_loc location() { return source_loc{loc_.file_name(), loc_.line(), loc_.function_name()}; } @@ -186,6 +186,34 @@ private: spdlog::details::source_location loc_; }; +template +struct message_wrapper +{ + message_wrapper(const T &msg, details::source_location loc = details::source_location::current()) + : msg_{msg} + , loc_{loc} + {} + + T message() + { + return msg_; + } + + source_loc location() + { + return source_loc{loc_.file_name(), loc_.line(), loc_.function_name()}; + } + + operator T() + { + return msg_; + } + +private: + T msg_; + spdlog::details::source_location loc_; +}; + namespace sinks { class sink; } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 4aa5a325..dec86ddb 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -88,19 +88,19 @@ public: template void log(source_loc loc, level::level_enum lvl, format_string_t fmt, Args &&...args) { - log_(loc, lvl, details::to_string_view(fmt.fmt()), std::forward(args)...); + log_(loc, lvl, details::to_string_view(fmt.format_string()), std::forward(args)...); } template void log(level::level_enum lvl, format_string_t fmt, Args &&...args) { - log(fmt.loc(), lvl, fmt, std::forward(args)...); + log(fmt.location(), lvl, fmt, std::forward(args)...); } template - void log(level::level_enum lvl, const T &msg) + void log(level::level_enum lvl, const message_wrapper &msg) { - log(source_loc{}, lvl, msg); + log(msg.location(), lvl, msg.message()); } // T cannot be statically converted to format string (including string_view/wstring_view) @@ -141,37 +141,37 @@ public: log(source_loc{}, lvl, msg); } - template + template= 1, int>::type = 0> void trace(format_string_t fmt, Args &&...args) { log(level::trace, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void debug(format_string_t fmt, Args &&...args) { log(level::debug, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void info(format_string_t fmt, Args &&...args) { log(level::info, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void warn(format_string_t fmt, Args &&...args) { log(level::warn, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void error(format_string_t fmt, Args &&...args) { log(level::err, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void critical(format_string_t fmt, Args &&...args) { log(level::critical, fmt, std::forward(args)...); @@ -181,13 +181,13 @@ public: template void log(source_loc loc, level::level_enum lvl, wformat_string_t fmt, Args &&...args) { - log_(loc, lvl, details::to_string_view(fmt.fmt()), std::forward(args)...); + log_(loc, lvl, details::to_string_view(fmt.format_string()), std::forward(args)...); } template void log(level::level_enum lvl, wformat_string_t fmt, Args &&...args) { - log(fmt.loc(), lvl, fmt, std::forward(args)...); + log(fmt.location(), lvl, fmt, std::forward(args)...); } void log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, wstring_view_t msg) @@ -225,37 +225,37 @@ public: log(source_loc{}, lvl, msg); } - template + template= 1, int>::type = 0> void trace(wformat_string_t fmt, Args &&...args) { log(level::trace, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void debug(wformat_string_t fmt, Args &&...args) { log(level::debug, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void info(wformat_string_t fmt, Args &&...args) { log(level::info, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void warn(wformat_string_t fmt, Args &&...args) { log(level::warn, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void error(wformat_string_t fmt, Args &&...args) { log(level::err, fmt, std::forward(args)...); } - template + template= 1, int>::type = 0> void critical(wformat_string_t fmt, Args &&...args) { log(level::critical, fmt, std::forward(args)...); @@ -263,39 +263,39 @@ public: #endif template - void trace(const T &msg, details::source_location loc = details::source_location::current()) + void trace(const message_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::trace, msg); + log(msg.location(), level::trace, msg.message()); } template - void debug(const T &msg, details::source_location loc = details::source_location::current()) + void debug(const message_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::debug, msg); + log(msg.location(), level::debug, msg.message()); } template - void info(const T &msg, details::source_location loc = details::source_location::current()) + void info(const message_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::info, msg); + log(msg.location(), level::info, msg.message()); } template - void warn(const T &msg, details::source_location loc = details::source_location::current()) + void warn(const message_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::warn, msg); + log(msg.location(), level::warn, msg); } template - void error(const T &msg, details::source_location loc = details::source_location::current()) + void error(const message_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::err, msg); + log(msg.location(), level::err, msg.message()); } template - void critical(const T &msg, details::source_location loc = details::source_location::current()) + void critical(const message_wrapper &msg) { - log(source_loc{loc.file_name(), loc.line(), loc.function_name()}, level::critical, msg); + log(msg.location(), level::critical, msg.message()); } // return true logging is enabled for the given level. diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 70bcbe23..890cd806 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -153,37 +153,37 @@ inline void log(level::level_enum lvl, format_string_t fmt, Args &&...a default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void trace(format_string_t fmt, Args &&...args) { default_logger_raw()->trace(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void debug(format_string_t fmt, Args &&...args) { default_logger_raw()->debug(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void info(format_string_t fmt, Args &&...args) { default_logger_raw()->info(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void warn(format_string_t fmt, Args &&...args) { default_logger_raw()->warn(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void error(format_string_t fmt, Args &&...args) { default_logger_raw()->error(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void critical(format_string_t fmt, Args &&...args) { default_logger_raw()->critical(fmt, std::forward(args)...); @@ -214,37 +214,37 @@ inline void log(level::level_enum lvl, wformat_string_t fmt, Args &&... default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void trace(wformat_string_t fmt, Args &&...args) { default_logger_raw()->trace(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void debug(wformat_string_t fmt, Args &&...args) { default_logger_raw()->debug(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void info(wformat_string_t fmt, Args &&...args) { default_logger_raw()->info(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void warn(wformat_string_t fmt, Args &&...args) { default_logger_raw()->warn(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void error(wformat_string_t fmt, Args &&...args) { default_logger_raw()->error(fmt, std::forward(args)...); } -template +template= 1, int>::type = 0> inline void critical(wformat_string_t fmt, Args &&...args) { default_logger_raw()->critical(fmt, std::forward(args)...); @@ -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 message_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 message_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 message_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 message_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 message_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 message_wrapper &msg) { - default_logger_raw()->critical(msg, loc); + default_logger_raw()->critical(msg); } } // namespace spdlog