diff --git a/include/spdlog/common.h b/include/spdlog/common.h index e0ed90ed..1c6c4190 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -336,6 +336,44 @@ struct file_event_handlers namespace details { +// to_string_view + +SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEXCEPT +{ + return spdlog::string_view_t{buf.data(), buf.size()}; +} + +SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(spdlog::string_view_t str) SPDLOG_NOEXCEPT +{ + return str; +} + +#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) +SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf) SPDLOG_NOEXCEPT +{ + return spdlog::wstring_view_t{buf.data(), buf.size()}; +} + +SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) SPDLOG_NOEXCEPT +{ + return str; +} +#endif + +#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 + // make_unique support for pre c++14 #if __cplusplus >= 201402L // C++14 and beyond diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 8f6396bf..d9867180 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -11,10 +11,6 @@ #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 @@ -22,37 +18,6 @@ namespace spdlog { namespace details { namespace fmt_helper { -inline spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEXCEPT -{ - 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; -} - -#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) -inline spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) SPDLOG_NOEXCEPT -{ - return str; -} -#endif - -#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 eb4be7a8..15572d76 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -17,7 +17,6 @@ #include #include #include -#include #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT # ifndef _WIN32 @@ -88,7 +87,7 @@ public: template void log(source_loc loc, level::level_enum lvl, format_string_t fmt, Args &&... args) { - log_(loc, lvl, details::fmt_helper::to_string_view(fmt), std::forward(args)...); + log_(loc, lvl, details::to_string_view(fmt), std::forward(args)...); } template @@ -181,7 +180,7 @@ public: template void log(source_loc loc, level::level_enum lvl, wformat_string_t fmt, Args &&... args) { - log_(loc, lvl, details::fmt_helper::to_string_view(fmt), std::forward(args)...); + log_(loc, lvl, details::to_string_view(fmt), std::forward(args)...); } template @@ -374,6 +373,7 @@ protected: #else fmt::vformat_to(fmt::appender(buf), fmt, fmt::make_format_args(std::forward(args)...)); #endif + details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); log_it_(log_msg, log_enabled, traceback_enabled); } diff --git a/tests/test_fmt_helper.cpp b/tests/test_fmt_helper.cpp index a9db5d02..52141902 100644 --- a/tests/test_fmt_helper.cpp +++ b/tests/test_fmt_helper.cpp @@ -2,7 +2,7 @@ #include "includes.h" using spdlog::memory_buf_t; -using spdlog::details::fmt_helper::to_string_view; +using spdlog::details::to_string_view; void test_pad2(int n, const char *expected) { diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 76cac855..b89f51d4 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -2,7 +2,7 @@ #include "test_sink.h" using spdlog::memory_buf_t; -using spdlog::details::fmt_helper::to_string_view; +using spdlog::details::to_string_view; // log to str and return it template