diff --git a/CMakeLists.txt b/CMakeLists.txt index 4260a1fe..a66e8405 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,7 +83,7 @@ option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF) # install options option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT}) -option(SPDLOG_USE_STD_FORMAT "Use std::format instead of fmt library. No compile-time format string checking." OFF) +option(SPDLOG_USE_STD_FORMAT "Use std::format instead of fmt library." OFF) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF) option(SPDLOG_FMT_EXTERNAL_HO "Use external fmt header-only library instead of bundled" OFF) option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF) diff --git a/appveyor.yml b/appveyor.yml index 705fbc6b..4ae8cb5e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -92,7 +92,7 @@ environment: WCHAR_FILES: 'OFF' BUILD_EXAMPLE: 'OFF' USE_STD_FORMAT: 'ON' - CXX_STANDARD: 23 # std::format is only available with /std:c++latest at the moment. + CXX_STANDARD: 20 APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022 - GENERATOR: '"Visual Studio 17 2022" -A x64' BUILD_TYPE: Release @@ -102,7 +102,7 @@ environment: WCHAR_FILES: 'ON' BUILD_EXAMPLE: 'OFF' USE_STD_FORMAT: 'ON' - CXX_STANDARD: 23 # std::format is only available with /std:c++latest at the moment. + CXX_STANDARD: 20 APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022 build_script: - cmd: >- diff --git a/include/spdlog/common.h b/include/spdlog/common.h index f97fd48c..e0ed90ed 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -17,7 +17,12 @@ #include #ifdef SPDLOG_USE_STD_FORMAT -# include +# include +# if __cpp_lib_format >= 202207L +# include +# else +# include +# endif #endif #ifdef SPDLOG_COMPILED_LIB @@ -134,7 +139,11 @@ using string_view_t = std::string_view; using memory_buf_t = std::string; template +# if __cpp_lib_format >= 202207L +using format_string_t = std::format_string; +# else using format_string_t = std::string_view; +# endif template struct is_convertible_to_basic_format_string : std::integral_constant>::value> @@ -145,7 +154,11 @@ using wstring_view_t = std::wstring_view; using wmemory_buf_t = std::wstring; template +# if __cpp_lib_format >= 202207L +using wformat_string_t = std::wformat_string; +# else using wformat_string_t = std::wstring_view; +# endif # endif # define SPDLOG_BUF_TO_STRING(x) x #else // use fmt lib instead of std::format diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 71544e84..d5e88a37 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -357,7 +357,7 @@ protected: // common implementation for after templated public api has been resolved template - void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&... args) + void log_(source_loc loc, level::level_enum lvl, format_string_t fmt, Args &&... args) { bool log_enabled = should_log(lvl); bool traceback_enabled = tracer_.enabled(); @@ -368,11 +368,7 @@ protected: SPDLOG_TRY { memory_buf_t buf; -#ifdef SPDLOG_USE_STD_FORMAT - fmt_lib::vformat_to(std::back_inserter(buf), fmt, fmt_lib::make_format_args(std::forward(args)...)); -#else - fmt::vformat_to(fmt::appender(buf), fmt, fmt::make_format_args(std::forward(args)...)); -#endif + fmt_lib::format_to(std::back_inserter(buf), fmt, 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); @@ -382,7 +378,7 @@ protected: #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT template - void log_(source_loc loc, level::level_enum lvl, wstring_view_t fmt, Args &&... args) + void log_(source_loc loc, level::level_enum lvl, wformat_string_t fmt, Args &&... args) { bool log_enabled = should_log(lvl); bool traceback_enabled = tracer_.enabled(); @@ -394,12 +390,7 @@ protected: { // format to wmemory_buffer and convert to utf8 wmemory_buf_t wbuf; -# ifdef SPDLOG_USE_STD_FORMAT - fmt_lib::vformat_to( - std::back_inserter(wbuf), fmt, fmt_lib::make_format_args(std::forward(args)...)); -# else - fmt::vformat_to(std::back_inserter(wbuf), fmt, fmt::make_format_args(std::forward(args)...)); -# endif + fmt_lib::format_to(std::back_inserter(wbuf), fmt, std::forward(args)...); memory_buf_t buf; details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf); @@ -408,27 +399,6 @@ protected: } SPDLOG_LOGGER_CATCH(loc) } - - // T can be statically converted to wstring_view, and no formatting needed. - template::value, int>::type = 0> - void log_(source_loc loc, level::level_enum lvl, const T &msg) - { - bool log_enabled = should_log(lvl); - bool traceback_enabled = tracer_.enabled(); - if (!log_enabled && !traceback_enabled) - { - return; - } - SPDLOG_TRY - { - memory_buf_t buf; - details::os::wstr_to_utf8buf(msg, buf); - 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(loc) - } - #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT // log the given message (if the given log level is high enough), diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index 5c118858..5bcb5ff4 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -82,8 +82,7 @@ /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// -// Uncomment to use C++20 std::format instead of fmt. This removes compile -// time checking of format strings, but doesn't depend on the fmt library. +// Uncomment to use C++20 std::format instead of fmt. // // #define SPDLOG_USE_STD_FORMAT ///////////////////////////////////////////////////////////////////////////////