From 17f441e43c14b27070e8151d08ce6529a3daabae Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Fri, 11 Nov 2022 00:01:41 -0500 Subject: [PATCH] Fix pre-VS 17.5 compilation --- include/spdlog/details/fmt_helper.h | 28 ++++++++++++++++++++++++++++ include/spdlog/logger.h | 14 ++++++++------ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index d3c355d5..9d55a3ce 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -11,6 +11,10 @@ #ifdef SPDLOG_USE_STD_FORMAT # include # include +# include +# if __cpp_lib_format >= 202207L +# include +# endif #endif // Some fmt helpers to efficiently format and pad ints and strings @@ -23,6 +27,30 @@ inline spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEX return spdlog::string_view_t{buf.data(), buf.size()}; } +inline spdlog::string_view_t to_string_view(spdlog::string_view_t str) SPDLOG_NOEXCEPT +{ + return str; +} + +inline spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) SPDLOG_NOEXCEPT +{ + return str; +} + +#ifndef SPDLOG_USE_STD_FORMAT +template +inline fmt::basic_string_view to_string_view(fmt::basic_format_string fmt) +{ + return fmt; +} +#elif __cpp_lib_format >= 202207L +template +SPDLOG_CONSTEXPR_FUNC std::basic_string_view to_string_view(std::basic_format_string fmt) SPDLOG_NOEXCEPT +{ + return fmt.get(); +} +#endif + inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) { auto *buf_ptr = view.data(); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index d5e88a37..094ee961 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -17,6 +17,7 @@ #include #include #include +#include #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT # ifndef _WIN32 @@ -87,7 +88,7 @@ public: template void log(source_loc loc, level::level_enum lvl, format_string_t fmt, Args &&... args) { - log_(loc, lvl, fmt, std::forward(args)...); + log_(loc, lvl, details::fmt_helper::to_string_view(fmt), std::forward(args)...); } template @@ -180,7 +181,7 @@ public: template void log(source_loc loc, level::level_enum lvl, wformat_string_t fmt, Args &&... args) { - log_(loc, lvl, fmt, std::forward(args)...); + log_(loc, lvl, details::fmt_helper::to_string_view(fmt), std::forward(args)...); } template @@ -357,7 +358,7 @@ protected: // common implementation for after templated public api has been resolved template - void log_(source_loc loc, level::level_enum lvl, format_string_t fmt, Args &&... args) + 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(); @@ -368,7 +369,7 @@ protected: SPDLOG_TRY { memory_buf_t buf; - fmt_lib::format_to(std::back_inserter(buf), fmt, std::forward(args)...); + fmt_lib::vformat_to(std::back_inserter(buf), fmt, fmt_lib::make_format_args(std::forward(args)...)); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); log_it_(log_msg, log_enabled, traceback_enabled); @@ -378,7 +379,7 @@ protected: #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT template - void log_(source_loc loc, level::level_enum lvl, wformat_string_t fmt, Args &&... args) + 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(); @@ -390,7 +391,8 @@ protected: { // format to wmemory_buffer and convert to utf8 wmemory_buf_t wbuf; - fmt_lib::format_to(std::back_inserter(wbuf), fmt, std::forward(args)...); + fmt_lib::vformat_to( + std::back_inserter(wbuf), fmt, fmt_lib::make_format_args(std::forward(args)...)); memory_buf_t buf; details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);