Switch to vformat_to

Drive-by: reduce the amount of occurences of #ifdef SPDLOG_USE_STD_FORMAT
This commit is contained in:
Charles Milette
2022-04-21 21:46:58 -04:00
parent b3ce5ed379
commit ebeb3707b1
10 changed files with 66 additions and 166 deletions

View File

@@ -44,15 +44,13 @@
#include <spdlog/fmt/fmt.h>
#ifndef SPDLOG_USE_STD_FORMAT
# if FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8
# define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
# if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
# include <spdlog/fmt/xchar.h>
# endif
# else
# define SPDLOG_FMT_RUNTIME(format_string) format_string
#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)
# if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
# include <spdlog/fmt/xchar.h>
# endif
#else
# define SPDLOG_FMT_RUNTIME(format_string) format_string
#endif
// visual studio up to 2013 does not support noexcept nor constexpr

View File

@@ -23,6 +23,20 @@ inline spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEX
return spdlog::string_view_t{buf.data(), buf.size()};
}
#ifdef SPDLOG_USE_STD_FORMAT
template<typename T>
std::basic_string<T> to_string(const std::basic_string<T> &buf)
{
return buf;
}
#else
template<typename T, size_t Size>
std::basic_string<T> to_string(const fmt::basic_memory_buffer<T, Size> &buf)
{
return fmt::to_string(buf);
}
#endif
inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest)
{
auto *buf_ptr = view.data();

View File

@@ -388,11 +388,7 @@ SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
{
memory_buf_t buf;
wstr_to_utf8buf(filename, buf);
# ifdef SPDLOG_USE_STD_FORMAT
return buf;
# else
return fmt::to_string(buf);
# endif
return fmt_helper::to_string(buf);
}
#else
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)

View File

@@ -363,13 +363,10 @@ protected:
}
SPDLOG_TRY
{
#ifdef SPDLOG_USE_STD_FORMAT
memory_buf_t buf = std::vformat(fmt, std::make_format_args(std::forward<Args>(args)...));
#else
memory_buf_t buf;
fmt::detail::vformat_to(buf, fmt, fmt::make_format_args(std::forward<Args>(args)...));
#endif
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
fmt_lib::vformat_to(std::back_inserter(buf), fmt, fmt_lib::make_format_args(std::forward<Args>(args)...));
details::log_msg log_msg(loc, name_, lvl, details::fmt_helper::to_string_view(buf));
log_it_(log_msg, log_enabled, traceback_enabled);
}
SPDLOG_LOGGER_CATCH(loc)
@@ -388,13 +385,9 @@ protected:
SPDLOG_TRY
{
// format to wmemory_buffer and convert to utf8
;
# ifdef SPDLOG_USE_STD_FORMAT
wmemory_buf_t wbuf = std::vformat(fmt, std::make_wformat_args(std::forward<Args>(args)...));
# else
wmemory_buf_t wbuf;
fmt::detail::vformat_to(wbuf, fmt, fmt::make_format_args<fmt::wformat_context>(std::forward<Args>(args)...));
# endif
fmt_lib::vformat_to(std::back_inserter(wbuf), fmt, fmt_lib::make_format_args<fmt_lib::wformat_context>(std::forward<Args>(args)...));
memory_buf_t buf;
details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));

View File

@@ -30,11 +30,8 @@ protected:
{
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
# ifdef SPDLOG_USE_STD_FORMAT
OutputDebugStringA(formatted.c_str());
# else
OutputDebugStringA(fmt::to_string(formatted).c_str());
# endif
formatted.push_back('\0'); // add a null terminator for OutputDebugStringA
OutputDebugStringA(formatted.data());
}
void flush_() override {}

View File

@@ -50,11 +50,7 @@ public:
{
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(q_.at(i), formatted);
#ifdef SPDLOG_USE_STD_FORMAT
ret.push_back(std::move(formatted));
#else
ret.push_back(fmt::to_string(formatted));
#endif
ret.push_back(fmt_helper::to_string(formatted));
}
return ret;
}