From d7c6cc13a5a7cf57fd55ecab91eba2dc1205e0f6 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 29 Nov 2024 14:49:32 +0200 Subject: [PATCH] Removed SPDLOG_USE_STD_FORMAT --- CMakeLists.txt | 28 +++++------------ example/example.cpp | 29 +++-------------- include/spdlog/common.h | 48 +++-------------------------- include/spdlog/details/fmt_helper.h | 26 +--------------- include/spdlog/fmt/bin_to_hex.h | 14 +-------- include/spdlog/fmt/fmt.h | 11 +------ include/spdlog/logger.h | 5 --- include/spdlog/stopwatch.h | 7 +---- 8 files changed, 19 insertions(+), 149 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 784f382a..53ac7436 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,18 +77,9 @@ option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF) # install options option(SPDLOG_SYSTEM_INCLUDES "Include as system headers (skip for clang-tidy)." OFF) option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT}) -option(SPDLOG_USE_STD_FORMAT "Use std::format instead of fmt library." OFF) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of of fetching from gitub." OFF) - option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF) -if(SPDLOG_USE_STD_FORMAT AND CMAKE_CXX_STANDARD LESS 20) - message(FATAL_ERROR "SPDLOG_USE_STD_FORMAT must be used with CMAKE_CXX_STANDARD >= 20") -endif() - -if(SPDLOG_USE_STD_FORMAT AND SPDLOG_FMT_EXTERNAL) - message(FATAL_ERROR "SPDLOG_USE_STD_FORMAT and SPDLOG_FMT_EXTERNAL are mutually exclusive") -endif() # misc tweakme options if(WIN32) @@ -142,19 +133,16 @@ endif() message(STATUS "spdlog version: ${SPDLOG_VERSION}") message(STATUS "spdlog build type: " ${CMAKE_BUILD_TYPE}) message(STATUS "spdlog build shared: " ${BUILD_SHARED_LIBS}) -message(STATUS "spdlog use std format: " ${SPDLOG_USE_STD_FORMAT}) message(STATUS "spdlog fmt external: " ${SPDLOG_FMT_EXTERNAL}) # --------------------------------------------------------------------------------------- -# Find {fmt} library if not using std::format +# Find {fmt} library # --------------------------------------------------------------------------------------- -if(NOT SPDLOG_USE_STD_FORMAT) - if (SPDLOG_FMT_EXTERNAL) - find_package(fmt REQUIRED) - message(STATUS "Using external fmt lib version: ${fmt_VERSION}") - else() - include(cmake/fmtlib.cmake) - endif() +if (SPDLOG_FMT_EXTERNAL) + find_package(fmt REQUIRED) + message(STATUS "Using external fmt lib version: ${fmt_VERSION}") +else() + include(cmake/fmtlib.cmake) endif() # --------------------------------------------------------------------------------------- @@ -292,9 +280,7 @@ target_include_directories(spdlog ${SPDLOG_INCLUDES_LEVEL} PUBLIC "$") target_link_libraries(spdlog PUBLIC Threads::Threads) -if(NOT SPDLOG_USE_STD_FORMAT) - target_link_libraries(spdlog PUBLIC fmt::fmt) -endif() +target_link_libraries(spdlog PUBLIC fmt::fmt) spdlog_enable_warnings(spdlog) set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION ${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR}) set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX d) diff --git a/example/example.cpp b/example/example.cpp index 2c63b896..9a3361f1 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -161,8 +161,8 @@ void async_example() { // {:p} - don't print the position on each line start. // {:n} - don't split the output to lines. -#if !defined SPDLOG_USE_STD_FORMAT || defined(_MSC_VER) - #include "spdlog/fmt/bin_to_hex.h" + +#include "spdlog/fmt/bin_to_hex.h" void binary_example() { std::vector buf; for (int i = 0; i < 80; i++) { @@ -177,26 +177,14 @@ void binary_example() { // logger->info("hexdump style: {:a}", spdlog::to_hex(buf)); // logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20)); } -#else -void binary_example() { - // not supported with std::format yet -} -#endif // Log a vector of numbers -#ifndef SPDLOG_USE_STD_FORMAT - #include "fmt/ranges.h" +#include "fmt/ranges.h" void vector_example() { std::vector vec = {1, 2, 3}; spdlog::info("Vector example: {}", vec); } -#else -void vector_example() {} -#endif - -// ! DSPDLOG_USE_STD_FORMAT - // Compile time log levels. // define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE) void trace_example() { @@ -250,7 +238,7 @@ struct my_type { : i(i) {} }; -#ifndef SPDLOG_USE_STD_FORMAT // when using fmtlib + template <> struct fmt::formatter : fmt::formatter { auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) { @@ -258,15 +246,6 @@ struct fmt::formatter : fmt::formatter { } }; -#else // when using std::format -template <> -struct std::formatter : std::formatter { - auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) { - return format_to(ctx.out(), "[my_type i={}]", my.i); - } -}; -#endif - void user_defined_example() { spdlog::info("user defined type: {}", my_type(14)); } // Custom error handler. Will be triggered on log failure. diff --git a/include/spdlog/common.h b/include/spdlog/common.h index a586bf7b..495c4e9a 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -21,9 +21,6 @@ #include #endif -#ifdef SPDLOG_USE_STD_FORMAT - #include -#endif #if defined(SPDLOG_SHARED_LIB) #if defined(_WIN32) @@ -41,16 +38,11 @@ #include "fmt/fmt.h" -#if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8 - #define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string) +#define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string) #define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string) #if defined(SPDLOG_WCHAR_FILENAMES) #include "fmt/xchar.h" #endif -#else - #define SPDLOG_FMT_RUNTIME(format_string) format_string - #define SPDLOG_FMT_STRING(format_string) format_string -#endif #ifndef SPDLOG_FUNCTION #define SPDLOG_FUNCTION static_cast(__FUNCTION__) @@ -98,29 +90,14 @@ using err_handler = std::function; using string_view_t = std::basic_string_view; using wstring_view_t = std::basic_string_view; -#ifdef SPDLOG_USE_STD_FORMAT -namespace fmt_lib = std; -using memory_buf_t = std::string; -using wmemory_buf_t = std::wstring; - -template - #if __cpp_lib_format >= 202207L - using format_string_t = std::format_string; - #else - using format_string_t = std::string_view; - #endif - - #define SPDLOG_BUF_TO_STRING(x) x -#else // use fmt lib instead of std::format namespace fmt_lib = fmt; - using memory_buf_t = fmt::basic_memory_buffer; +using wmemory_buf_t = fmt::basic_memory_buffer; + template using format_string_t = fmt::format_string; -using wmemory_buf_t = fmt::basic_memory_buffer; - #define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x) -#endif // SPDLOG_USE_STD_FORMAT +#define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x) #define SPDLOG_LEVEL_TRACE 0 #define SPDLOG_LEVEL_DEBUG 1 #define SPDLOG_LEVEL_INFO 2 @@ -231,27 +208,10 @@ namespace details { [[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) noexcept { return str; } #endif -// convert format_string<...> to string_view depending on format lib versions -#if defined(SPDLOG_USE_STD_FORMAT) - #if __cpp_lib_format >= 202207L // std::format and __cpp_lib_format >= 202207L -template -[[nodiscard]] constexpr std::basic_string_view to_string_view(std::basic_format_string fmt) noexcept { - return fmt.get(); -} - #else // std::format and __cpp_lib_format < 202207L -template -[[nodiscard]] constexpr std::basic_string_view to_string_view(std::basic_format_string fmt) noexcept { - return fmt; -} - #endif -#else // {fmt} version - template [[nodiscard]] constexpr fmt::basic_string_view to_string_view(fmt::basic_format_string fmt) noexcept { return fmt; } -#endif - } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 5ee8bb14..751bd696 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -9,11 +9,6 @@ #include "../common.h" #include "../fmt/fmt.h" -#ifdef SPDLOG_USE_STD_FORMAT - #include - #include -#endif - // Some fmt helpers to efficiently format and pad ints and strings namespace spdlog { namespace details { @@ -24,27 +19,11 @@ inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) { dest.append(buf_ptr, buf_ptr + view.size()); } -#ifdef SPDLOG_USE_STD_FORMAT -template -inline void append_int(T n, memory_buf_t &dest) { - // Buffer should be large enough to hold all digits (digits10 + 1) and a sign - constexpr const auto BUF_SIZE = std::numeric_limits::digits10 + 2; - char buf[BUF_SIZE]; - - auto [ptr, ec] = std::to_chars(buf, buf + BUF_SIZE, n, 10); - if (ec == std::errc()) { - dest.append(buf, ptr); - } else { - throw_spdlog_ex("Failed to format int", static_cast(ec)); - } -} -#else template inline void append_int(T n, memory_buf_t &dest) { fmt::format_int i(n); dest.append(i.data(), i.data() + i.size()); } -#endif template constexpr unsigned int count_digits_fallback(T n) { @@ -66,9 +45,7 @@ constexpr unsigned int count_digits_fallback(T n) { template inline unsigned int count_digits(T n) { using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type; -#ifdef SPDLOG_USE_STD_FORMAT - return count_digits_fallback(static_cast(n)); -#else + return static_cast(fmt:: // fmt 7.0.0 renamed the internal namespace to detail. // See: https://github.com/fmtlib/fmt/issues/1538 @@ -78,7 +55,6 @@ inline unsigned int count_digits(T n) { detail #endif ::count_digits(static_cast(n))); -#endif } inline void pad2(int n, memory_buf_t &dest) { diff --git a/include/spdlog/fmt/bin_to_hex.h b/include/spdlog/fmt/bin_to_hex.h index 71ebe26a..4dc6f337 100644 --- a/include/spdlog/fmt/bin_to_hex.h +++ b/include/spdlog/fmt/bin_to_hex.h @@ -88,13 +88,7 @@ inline details::dump_info to_hex(const It range_begin, const It range_end, s } // namespace spdlog -namespace -#ifdef SPDLOG_USE_STD_FORMAT - std -#else - fmt -#endif -{ +namespace fmt { template struct formatter, char> { @@ -142,13 +136,7 @@ struct formatter, char> { constexpr const char *hex_upper = "0123456789ABCDEF"; constexpr const char *hex_lower = "0123456789abcdef"; const char *hex_chars = use_uppercase ? hex_upper : hex_lower; - -#if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION < 60000 - auto inserter = ctx.begin(); -#else auto inserter = ctx.out(); -#endif - int size_per_line = static_cast(the_range.size_per_line()); auto start_of_line = the_range.get_begin(); for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) { diff --git a/include/spdlog/fmt/fmt.h b/include/spdlog/fmt/fmt.h index e631e6bb..af6f5203 100644 --- a/include/spdlog/fmt/fmt.h +++ b/include/spdlog/fmt/fmt.h @@ -5,14 +5,5 @@ #pragma once -// -// Include a bundled header-only copy of fmtlib or an external one. -// By default, spdlog include its own copy. -// #include "../spdlog_config.h" - -#if defined(SPDLOG_USE_STD_FORMAT) // use std::format - #include -#else - #include "fmt/format.h" -#endif +#include "fmt/format.h" diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 416de65e..7a2bd52d 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -187,14 +187,9 @@ protected: void log_with_format_(source_loc loc, level lvl, const format_string_t &fmt, Args &&...args) { assert(should_log(lvl)); SPDLOG_TRY { -#ifdef SPDLOG_USE_STD_FORMAT - auto formatted = std::vformat(fmt, std::make_format_args(args...)); - sink_it_(details::log_msg(loc, name_, lvl, formatted)); -#else // use {fmt} lib memory_buf_t buf; fmt::vformat_to(std::back_inserter(buf), fmt, fmt::make_format_args(args...)); sink_it_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()))); -#endif } SPDLOG_LOGGER_CATCH(loc) } diff --git a/include/spdlog/stopwatch.h b/include/spdlog/stopwatch.h index ea9182e8..61f168bc 100644 --- a/include/spdlog/stopwatch.h +++ b/include/spdlog/stopwatch.h @@ -51,12 +51,7 @@ public: } // namespace spdlog // Support for fmt formatting (e.g. "{:012.9}" or just "{}") -namespace -#ifdef SPDLOG_USE_STD_FORMAT - std -#else - fmt -#endif +namespace fmt { template <>