From 0367289b3fc18ccadb9cd63b3c175602ab3145d1 Mon Sep 17 00:00:00 2001 From: Bailey Chittle Date: Fri, 24 Mar 2023 10:25:20 -0400 Subject: [PATCH] separate std string view from std format --- CMakeLists.txt | 8 +++++++- example/example.cpp | 2 +- include/spdlog/common.h | 17 ++++++++++++++--- include/spdlog/details/log_msg_buffer-inl.h | 8 ++++---- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3830dfe2..63e8f753 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ endif() # --------------------------------------------------------------------------------------- # Compiler config # --------------------------------------------------------------------------------------- -if(SPDLOG_USE_STD_FORMAT) +if(SPDLOG_USE_STD_FORMAT OR SPDLOG_USE_STD_STRING_VIEW) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) elseif(NOT CMAKE_CXX_STANDARD) @@ -88,6 +88,7 @@ option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF) 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_USE_STD_STRING_VIEW "Use std::string_view instead of the fmt implementation of string_view." 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) @@ -104,6 +105,10 @@ if(SPDLOG_USE_STD_FORMAT AND SPDLOG_FMT_EXTERNAL) message(FATAL_ERROR "SPDLOG_USE_STD_FORMAT and SPDLOG_FMT_EXTERNAL are mutually exclusive") endif() +if(SPDLOG_USE_STD_FORMAT AND NOT SPDLOG_USE_STD_STRING_VIEW) + message(FATAL_ERROR "SPDLOG_USE_STD_FORMAT requires SPDLOG_USE_STD_STRING_VIEW") +endif() + # misc tweakme options if(WIN32) option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF) @@ -248,6 +253,7 @@ foreach( SPDLOG_NO_TLS SPDLOG_NO_ATOMIC_LEVELS SPDLOG_DISABLE_DEFAULT_LOGGER + SPDLOG_USE_STD_STRING_VIEW SPDLOG_USE_STD_FORMAT) if(${SPDLOG_OPTION}) target_compile_definitions(spdlog PUBLIC ${SPDLOG_OPTION}) diff --git a/example/example.cpp b/example/example.cpp index 316a22b1..636a1be3 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -279,7 +279,7 @@ struct fmt::formatter : fmt::formatter { auto format(my_type my, format_context &ctx) -> decltype(ctx.out()) { - return format_to(ctx.out(), "[my_type i={}]", my.i); + return fmt::format_to(ctx.out(), "[my_type i={}]", my.i); } }; diff --git a/include/spdlog/common.h b/include/spdlog/common.h index e69201a8..a841ecf2 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -132,10 +132,15 @@ using log_clock = std::chrono::system_clock; using sink_ptr = std::shared_ptr; using sinks_init_list = std::initializer_list; using err_handler = std::function; +#ifdef SPDLOG_USE_STD_STRING_VIEW +using string_view_t = std::string_view; +#else +using string_view_t = fmt::basic_string_view; +#endif + #ifdef SPDLOG_USE_STD_FORMAT namespace fmt_lib = std; -using string_view_t = std::string_view; using memory_buf_t = std::string; template @@ -164,7 +169,6 @@ using wformat_string_t = std::wstring_view; #else // use fmt lib instead of std::format namespace fmt_lib = fmt; -using string_view_t = fmt::basic_string_view; using memory_buf_t = fmt::basic_memory_buffer; template @@ -360,7 +364,7 @@ SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(spdlog::wstring_view } #endif -#ifndef SPDLOG_USE_STD_FORMAT +#if !defined(SPDLOG_USE_STD_STRING_VIEW) && !defined(SPDLOG_USE_STD_FORMAT) template inline fmt::basic_string_view to_string_view(fmt::basic_format_string fmt) { @@ -372,6 +376,13 @@ SPDLOG_CONSTEXPR_FUNC std::basic_string_view to_string_view(std::basic_format { return fmt.get(); } +#elif defined(SPDLOG_USE_STD_STRING_VIEW) && !defined(SPDLOG_USE_STD_FORMAT) +template +SPDLOG_CONSTEXPR_FUNC std::basic_string_view to_string_view(fmt::basic_format_string fmt) SPDLOG_NOEXCEPT +{ + auto tmp = fmt::basic_string_view{fmt}; + return std::string_view{tmp.data(), tmp.size()}; +} #endif // make_unique support for pre c++14 diff --git a/include/spdlog/details/log_msg_buffer-inl.h b/include/spdlog/details/log_msg_buffer-inl.h index 84d83dc2..21f5ee4d 100644 --- a/include/spdlog/details/log_msg_buffer-inl.h +++ b/include/spdlog/details/log_msg_buffer-inl.h @@ -13,16 +13,16 @@ namespace details { SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg &orig_msg) : log_msg{orig_msg} { - buffer.append(logger_name.begin(), logger_name.end()); - buffer.append(payload.begin(), payload.end()); + buffer.append(logger_name.data(), logger_name.data() + logger_name.size()); + buffer.append(payload.data(), payload.data() + payload.size()); update_string_views(); } SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other) : log_msg{other} { - buffer.append(logger_name.begin(), logger_name.end()); - buffer.append(payload.begin(), payload.end()); + buffer.append(logger_name.data(), logger_name.data() + logger_name.size()); + buffer.append(payload.data(), payload.data() + payload.size()); update_string_views(); }