diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 67f808ba..25e5383e 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -73,63 +73,58 @@ public: void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; + // FormatString is a type derived from fmt::compile_string + template::value, int>::type = 0, typename... Args> + void log(source_loc loc, level::level_enum lvl, const FormatString &fmt, const Args &... args) + { + log_(loc, lvl, fmt, args...); + } + + // FormatString is NOT a type derived from fmt::compile_string but is a string_view_t or can be implicitly converted to one template void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) { - bool log_enabled = should_log(lvl); - bool traceback_enabled = tracer_.enabled(); - if (!log_enabled && !traceback_enabled) - { - return; - } - SPDLOG_TRY - { - memory_buf_t buf; - fmt::format_to(buf, fmt, args...); - details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - log_it_(log_msg, log_enabled, traceback_enabled); - } - SPDLOG_LOGGER_CATCH() + log_(loc, lvl, fmt, args...); } - template - void log(level::level_enum lvl, string_view_t fmt, const Args &... args) + template + void log(level::level_enum lvl, const FormatString &fmt, const Args &... args) { log(source_loc{}, lvl, fmt, args...); } - template - void trace(string_view_t fmt, const Args &... args) + template + void trace(const FormatString &fmt, const Args &... args) { log(level::trace, fmt, args...); } - template - void debug(string_view_t fmt, const Args &... args) + template + void debug(const FormatString &fmt, const Args &... args) { log(level::debug, fmt, args...); } - template - void info(string_view_t fmt, const Args &... args) + template + void info(const FormatString &fmt, const Args &... args) { log(level::info, fmt, args...); } - template - void warn(string_view_t fmt, const Args &... args) + template + void warn(const FormatString &fmt, const Args &... args) { log(level::warn, fmt, args...); } - template - void error(string_view_t fmt, const Args &... args) + template + void error(const FormatString &fmt, const Args &... args) { log(level::err, fmt, args...); } - template - void critical(string_view_t fmt, const Args &... args) + template + void critical(const FormatString &fmt, const Args &... args) { log(level::critical, fmt, args...); } @@ -140,8 +135,10 @@ public: log(source_loc{}, lvl, msg); } - // T can be statically converted to string_view - template::value, T>::type * = nullptr> + // T can be statically converted to string_view and isn't a fmt::compile_string + template::value && !fmt::is_compile_string::value, T>::type + * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { log(loc, lvl, string_view_t{msg}); @@ -251,48 +248,6 @@ public: SPDLOG_LOGGER_CATCH() } - template - void log(level::level_enum lvl, wstring_view_t fmt, const Args &... args) - { - log(source_loc{}, lvl, fmt, args...); - } - - template - void trace(wstring_view_t fmt, const Args &... args) - { - log(level::trace, fmt, args...); - } - - template - void debug(wstring_view_t fmt, const Args &... args) - { - log(level::debug, fmt, args...); - } - - template - void info(wstring_view_t fmt, const Args &... args) - { - log(level::info, fmt, args...); - } - - template - void warn(wstring_view_t fmt, const Args &... args) - { - log(level::warn, fmt, args...); - } - - template - void error(wstring_view_t fmt, const Args &... args) - { - log(level::err, fmt, args...); - } - - template - void critical(wstring_view_t fmt, const Args &... args) - { - log(level::critical, fmt, args...); - } - // T can be statically converted to wstring_view template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) @@ -370,6 +325,26 @@ protected: err_handler custom_err_handler_{nullptr}; details::backtracer tracer_; + // common implementation for after templated public api has been resolved + template + void log_(source_loc loc, level::level_enum lvl, const FormatString &fmt, const Args &... args) + { + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) + { + return; + } + SPDLOG_TRY + { + memory_buf_t buf; + fmt::format_to(buf, fmt, args...); + details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); + log_it_(log_msg, log_enabled, traceback_enabled); + } + SPDLOG_LOGGER_CATCH() + } + // log the given message (if the given log level is high enough), // and save backtrace (if backtrace is enabled). void log_it_(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 55de6676..fa219950 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -121,50 +121,50 @@ SPDLOG_API spdlog::logger *default_logger_raw(); SPDLOG_API void set_default_logger(std::shared_ptr default_logger); -template -inline void log(source_loc source, level::level_enum lvl, string_view_t fmt, const Args &... args) +template +inline void log(source_loc source, level::level_enum lvl, const FormatString &fmt, const Args &... args) { default_logger_raw()->log(source, lvl, fmt, args...); } -template -inline void log(level::level_enum lvl, string_view_t fmt, const Args &... args) +template +inline void log(level::level_enum lvl, const FormatString &fmt, const Args &... args) { default_logger_raw()->log(source_loc{}, lvl, fmt, args...); } -template -inline void trace(string_view_t fmt, const Args &... args) +template +inline void trace(const FormatString &fmt, const Args &... args) { default_logger_raw()->trace(fmt, args...); } -template -inline void debug(string_view_t fmt, const Args &... args) +template +inline void debug(const FormatString &fmt, const Args &... args) { default_logger_raw()->debug(fmt, args...); } -template -inline void info(string_view_t fmt, const Args &... args) +template +inline void info(const FormatString &fmt, const Args &... args) { default_logger_raw()->info(fmt, args...); } -template -inline void warn(string_view_t fmt, const Args &... args) +template +inline void warn(const FormatString &fmt, const Args &... args) { default_logger_raw()->warn(fmt, args...); } -template -inline void error(string_view_t fmt, const Args &... args) +template +inline void error(const FormatString &fmt, const Args &... args) { default_logger_raw()->error(fmt, args...); } -template -inline void critical(string_view_t fmt, const Args &... args) +template +inline void critical(const FormatString &fmt, const Args &... args) { default_logger_raw()->critical(fmt, args...); } @@ -217,57 +217,6 @@ inline void critical(const T &msg) default_logger_raw()->critical(msg); } -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT -template -inline void log(source_loc source, level::level_enum lvl, wstring_view_t fmt, const Args &... args) -{ - default_logger_raw()->log(source, lvl, fmt, args...); -} - -template -inline void log(level::level_enum lvl, wstring_view_t fmt, const Args &... args) -{ - default_logger_raw()->log(lvl, fmt, args...); -} - -template -inline void trace(wstring_view_t fmt, const Args &... args) -{ - default_logger_raw()->trace(fmt, args...); -} - -template -inline void debug(wstring_view_t fmt, const Args &... args) -{ - default_logger_raw()->debug(fmt, args...); -} - -template -inline void info(wstring_view_t fmt, const Args &... args) -{ - default_logger_raw()->info(fmt, args...); -} - -template -inline void warn(wstring_view_t fmt, const Args &... args) -{ - default_logger_raw()->warn(fmt, args...); -} - -template -inline void error(wstring_view_t fmt, const Args &... args) -{ - default_logger_raw()->error(fmt, args...); -} - -template -inline void critical(wstring_view_t fmt, const Args &... args) -{ - default_logger_raw()->critical(fmt, args...); -} - -#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT - } // namespace spdlog //