From d8f13cbd5bea70f20da22d984bdba93766998a0f Mon Sep 17 00:00:00 2001 From: dkavolis <12998363+dkavolis@users.noreply.github.com> Date: Thu, 22 Jul 2021 16:19:48 +0100 Subject: [PATCH] replace FormatString template argument with fmt::basic_format_string --- include/spdlog/common.h | 11 +++++ include/spdlog/logger.h | 100 ++++++++++++++++++++++++++++---------- include/spdlog/spdlog.h | 82 +++++++++++++++++++++++++------ tests/test_errors.cpp | 6 +-- tests/test_stdout_api.cpp | 2 +- 5 files changed, 154 insertions(+), 47 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index d6434893..7636df4d 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -123,13 +123,24 @@ using wmemory_buf_t = fmt::basic_memory_buffer; template struct is_convertible_to_wstring_view : std::is_convertible {}; +template +struct is_convertible_to_wformat_string : std::is_convertible> +{}; # endif // _WIN32 #else template struct is_convertible_to_wstring_view : std::false_type {}; +template +struct is_convertible_to_wformat_string : std::false_type +{}; #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT +template +struct is_convertible_to_basic_format_string + : std::integral_constant>::value || is_convertible_to_wformat_string::value> +{}; + #if defined(SPDLOG_NO_ATOMIC_LEVELS) using level_t = details::null_atomic_int; #else diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index befe7321..6cbcdf6f 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -77,14 +77,14 @@ public: logger &operator=(logger other) SPDLOG_NOEXCEPT; void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; - template - void log(source_loc loc, level::level_enum lvl, const FormatString &fmt, Args &&...args) + template + void log(source_loc loc, level::level_enum lvl, fmt::format_string fmt, Args &&...args) { log_(loc, lvl, fmt, std::forward(args)...); } - template - void log(level::level_enum lvl, const FormatString &fmt, Args &&...args) + template + void log(level::level_enum lvl, fmt::format_string fmt, Args &&...args) { log(source_loc{}, lvl, fmt, std::forward(args)...); } @@ -134,50 +134,98 @@ public: } // T cannot be statically converted to string_view or wstring_view - template::value && - !is_convertible_to_wstring_view::value, - int>::type = 0> + template::value, int>::type = 0> void log(source_loc loc, level::level_enum lvl, const T &msg) { log(loc, lvl, "{}", msg); } - template - void trace(const FormatString &fmt, Args &&...args) + template + void trace(fmt::format_string fmt, Args &&...args) { log(level::trace, fmt, std::forward(args)...); } - template - void debug(const FormatString &fmt, Args &&...args) + template + void debug(fmt::format_string fmt, Args &&...args) { log(level::debug, fmt, std::forward(args)...); } - template - void info(const FormatString &fmt, Args &&...args) + template + void info(fmt::format_string fmt, Args &&...args) { log(level::info, fmt, std::forward(args)...); } - template - void warn(const FormatString &fmt, Args &&...args) + template + void warn(fmt::format_string fmt, Args &&...args) { log(level::warn, fmt, std::forward(args)...); } - template - void error(const FormatString &fmt, Args &&...args) + template + void error(fmt::format_string fmt, Args &&...args) { log(level::err, fmt, std::forward(args)...); } - template - void critical(const FormatString &fmt, Args &&...args) + template + void critical(fmt::format_string fmt, Args &&...args) { log(level::critical, fmt, std::forward(args)...); } +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + template + void log(level::level_enum lvl, fmt::wformat_string fmt, Args &&...args) + { + log(source_loc{}, lvl, fmt, std::forward(args)...); + } + + template + void log(source_loc loc, level::level_enum lvl, fmt::wformat_string fmt, Args &&...args) + { + log_(loc, lvl, fmt, std::forward(args)...); + } + + template + void trace(fmt::wformat_string fmt, Args &&...args) + { + log(level::trace, fmt, std::forward(args)...); + } + + template + void debug(fmt::wformat_string fmt, Args &&...args) + { + log(level::debug, fmt, std::forward(args)...); + } + + template + void info(fmt::wformat_string fmt, Args &&...args) + { + log(level::info, fmt, std::forward(args)...); + } + + template + void warn(fmt::wformat_string fmt, Args &&...args) + { + log(level::warn, fmt, std::forward(args)...); + } + + template + void error(fmt::wformat_string fmt, Args &&...args) + { + log(level::err, fmt, std::forward(args)...); + } + + template + void critical(fmt::wformat_string fmt, Args &&...args) + { + log(level::critical, fmt, std::forward(args)...); + } +#endif + template void trace(const T &msg) { @@ -269,9 +317,8 @@ protected: details::backtracer tracer_; // common implementation for after templated public api has been resolved - template, - typename std::enable_if::value, Char>::type * = nullptr> - void log_(source_loc loc, level::level_enum lvl, const FormatString &fmt, Args &&...args) + template + void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&...args) { bool log_enabled = should_log(lvl); bool traceback_enabled = tracer_.enabled(); @@ -282,7 +329,7 @@ protected: SPDLOG_TRY { memory_buf_t buf; - fmt::detail::vformat_to(buf, fmt::to_string_view(fmt), fmt::make_args_checked(fmt, args...)); + fmt::detail::vformat_to(buf, fmt, fmt::make_format_args(args...)); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); log_it_(log_msg, log_enabled, traceback_enabled); } @@ -290,9 +337,8 @@ protected: } #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT - template, - typename std::enable_if::value, Char>::type * = nullptr> - void log_(source_loc loc, level::level_enum lvl, const FormatString &fmt, Args &&...args) + template + void log_(source_loc loc, level::level_enum lvl, wstring_view_t fmt, Args &&...args) { bool log_enabled = should_log(lvl); bool traceback_enabled = tracer_.enabled(); @@ -304,7 +350,7 @@ protected: { // format to wmemory_buffer and convert to utf8 fmt::wmemory_buffer wbuf; - fmt::detail::vformat_to(wbuf, fmt::wstring_view(fmt), fmt::make_args_checked(fmt, args...)); + fmt::detail::vformat_to(wbuf, fmt, fmt::make_format_args(args...)); memory_buf_t buf; details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 41c6bbb8..812896da 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -127,50 +127,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, const FormatString &fmt, Args &&...args) +template +inline void log(source_loc source, level::level_enum lvl, fmt::format_string fmt, Args &&...args) { default_logger_raw()->log(source, lvl, fmt, std::forward(args)...); } -template -inline void log(level::level_enum lvl, const FormatString &fmt, Args &&...args) +template +inline void log(level::level_enum lvl, fmt::format_string fmt, Args &&...args) { default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward(args)...); } -template -inline void trace(const FormatString &fmt, Args &&...args) +template +inline void trace(fmt::format_string fmt, Args &&...args) { default_logger_raw()->trace(fmt, std::forward(args)...); } -template -inline void debug(const FormatString &fmt, Args &&...args) +template +inline void debug(fmt::format_string fmt, Args &&...args) { default_logger_raw()->debug(fmt, std::forward(args)...); } -template -inline void info(const FormatString &fmt, Args &&...args) +template +inline void info(fmt::format_string fmt, Args &&...args) { default_logger_raw()->info(fmt, std::forward(args)...); } -template -inline void warn(const FormatString &fmt, Args &&...args) +template +inline void warn(fmt::format_string fmt, Args &&...args) { default_logger_raw()->warn(fmt, std::forward(args)...); } -template -inline void error(const FormatString &fmt, Args &&...args) +template +inline void error(fmt::format_string fmt, Args &&...args) { default_logger_raw()->error(fmt, std::forward(args)...); } -template -inline void critical(const FormatString &fmt, Args &&...args) +template +inline void critical(fmt::format_string fmt, Args &&...args) { default_logger_raw()->critical(fmt, std::forward(args)...); } @@ -187,6 +187,56 @@ inline void log(level::level_enum lvl, const T &msg) default_logger_raw()->log(lvl, msg); } +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT +template +inline void log(source_loc source, level::level_enum lvl, fmt::wformat_string fmt, Args &&...args) +{ + default_logger_raw()->log(source, lvl, fmt, std::forward(args)...); +} + +template +inline void log(level::level_enum lvl, fmt::wformat_string fmt, Args &&...args) +{ + default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward(args)...); +} + +template +inline void trace(fmt::wformat_string fmt, Args &&...args) +{ + default_logger_raw()->trace(fmt, std::forward(args)...); +} + +template +inline void debug(fmt::wformat_string fmt, Args &&...args) +{ + default_logger_raw()->debug(fmt, std::forward(args)...); +} + +template +inline void info(fmt::wformat_string fmt, Args &&...args) +{ + default_logger_raw()->info(fmt, std::forward(args)...); +} + +template +inline void warn(fmt::wformat_string fmt, Args &&...args) +{ + default_logger_raw()->warn(fmt, std::forward(args)...); +} + +template +inline void error(fmt::wformat_string fmt, Args &&...args) +{ + default_logger_raw()->error(fmt, std::forward(args)...); +} + +template +inline void critical(fmt::wformat_string fmt, Args &&...args) +{ + default_logger_raw()->critical(fmt, std::forward(args)...); +} +#endif + template inline void trace(const T &msg) { diff --git a/tests/test_errors.cpp b/tests/test_errors.cpp index b7673581..bcc21d73 100644 --- a/tests/test_errors.cpp +++ b/tests/test_errors.cpp @@ -29,7 +29,7 @@ TEST_CASE("default_error_handler", "[errors]]") auto logger = spdlog::create("test-error", filename, true); logger->set_pattern("%v"); - logger->info("Test message {} {}", 1); + logger->info(fmt::runtime("Test message {} {}"), 1); logger->info("Test message {}", 2); logger->flush(); @@ -49,7 +49,7 @@ TEST_CASE("custom_error_handler", "[errors]]") logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); logger->info("Good message #1"); - REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex); + REQUIRE_THROWS_AS(logger->info(fmt::runtime("Bad format msg {} {}"), "xxx"), custom_ex); logger->info("Good message #2"); require_message_count(SIMPLE_LOG, 2); } @@ -88,7 +88,7 @@ TEST_CASE("async_error_handler", "[errors]]") ofs << err_msg; }); logger->info("Good message #1"); - logger->info("Bad format msg {} {}", "xxx"); + logger->info(fmt::runtime("Bad format msg {} {}"), "xxx"); logger->info("Good message #2"); spdlog::drop("logger"); // force logger to drain the queue and shutdown } diff --git a/tests/test_stdout_api.cpp b/tests/test_stdout_api.cpp index 3392439d..d55223ff 100644 --- a/tests/test_stdout_api.cpp +++ b/tests/test_stdout_api.cpp @@ -95,4 +95,4 @@ TEST_CASE("wchar_api", "[stdout]") spdlog::drop_all(); } -#endif \ No newline at end of file +#endif