From f5492aed12b144df73faf8ae336b3dbc39ebbc38 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 28 Aug 2019 18:46:09 +0300 Subject: [PATCH] Unified usage of fmt::memory_buffer across the codebase --- include/spdlog/common-inl.h | 2 +- include/spdlog/common.h | 2 + include/spdlog/details/file_helper-inl.h | 2 +- include/spdlog/details/file_helper.h | 2 +- include/spdlog/details/fmt_helper.h | 36 +++----- include/spdlog/details/log_msg_buffer.h | 2 +- include/spdlog/details/os-inl.h | 4 +- include/spdlog/details/os.h | 2 +- .../spdlog/details/pattern_formatter-inl.h | 92 +++++++++---------- include/spdlog/details/pattern_formatter.h | 4 +- include/spdlog/formatter.h | 2 +- include/spdlog/logger.h | 6 +- include/spdlog/sinks/android_sink.h | 2 +- include/spdlog/sinks/ansicolor_sink-inl.h | 4 +- include/spdlog/sinks/ansicolor_sink.h | 2 +- include/spdlog/sinks/basic_file_sink-inl.h | 2 +- include/spdlog/sinks/daily_file_sink.h | 4 +- include/spdlog/sinks/dup_filter_sink.h | 2 +- include/spdlog/sinks/msvc_sink.h | 2 +- include/spdlog/sinks/ostream_sink.h | 2 +- include/spdlog/sinks/rotating_file_sink-inl.h | 4 +- include/spdlog/sinks/stdout_sinks-inl.h | 2 +- include/spdlog/sinks/syslog_sink.h | 2 +- include/spdlog/sinks/wincolor_sink-inl.h | 6 +- include/spdlog/sinks/wincolor_sink.h | 4 +- tests/test_file_helper.cpp | 2 +- tests/test_fmt_helper.cpp | 10 +- tests/test_pattern_formatter.cpp | 41 +++++---- tests/test_sink.h | 2 +- 29 files changed, 123 insertions(+), 126 deletions(-) diff --git a/include/spdlog/common-inl.h b/include/spdlog/common-inl.h index 95fedcd0..7ba12511 100644 --- a/include/spdlog/common-inl.h +++ b/include/spdlog/common-inl.h @@ -44,7 +44,7 @@ SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg) SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) { - fmt::memory_buffer outbuf; + memory_buf_t outbuf; fmt::format_system_error(outbuf, last_errno, msg); msg_ = fmt::to_string(outbuf); } diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 8058cada..d1fa4776 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -105,6 +105,8 @@ using basic_string_view_t = fmt::basic_string_view; using string_view_t = basic_string_view_t; +using memory_buf_t = fmt::basic_memory_buffer; + #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT #ifndef _WIN32 #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 891107c9..d8c9cab4 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -66,7 +66,7 @@ SPDLOG_INLINE void file_helper::close() } } -SPDLOG_INLINE void file_helper::write(const fmt::memory_buffer &buf) +SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf) { size_t msg_size = buf.size(); auto data = buf.data(); diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 6fdecd06..990d99e3 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -26,7 +26,7 @@ public: void reopen(bool truncate); void flush(); void close(); - void write(const fmt::memory_buffer &buf); + void write(const memory_buf_t &buf); size_t size() const; const filename_t &filename() const; static bool file_exists(const filename_t &fname); diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 67494ed5..ebb747c6 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -12,21 +12,14 @@ namespace spdlog { namespace details { namespace fmt_helper { -template -inline spdlog::string_view_t to_string_view(const fmt::basic_memory_buffer &buf) SPDLOG_NOEXCEPT +inline spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEXCEPT { return spdlog::string_view_t(buf.data(), buf.size()); } -template -inline void append_buf(const fmt::basic_memory_buffer &buf, fmt::basic_memory_buffer &dest) -{ - auto *buf_ptr = buf.data(); - dest.append(buf_ptr, buf_ptr + buf.size()); -} -template -inline void append_string_view(spdlog::string_view_t view, fmt::basic_memory_buffer &dest) + +inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) { auto *buf_ptr = view.data(); if (buf_ptr != nullptr) @@ -35,8 +28,8 @@ inline void append_string_view(spdlog::string_view_t view, fmt::basic_memory_buf } } -template -inline void append_int(T n, fmt::basic_memory_buffer &dest) +template +inline void append_int(T n, memory_buf_t &dest) { fmt::format_int i(n); dest.append(i.data(), i.data() + i.size()); @@ -49,8 +42,7 @@ inline unsigned count_digits(T n) return static_cast(fmt::internal::count_digits(static_cast(n))); } -template -inline void pad2(int n, fmt::basic_memory_buffer &dest) +inline void pad2(int n, memory_buf_t &dest) { if (n > 99) { @@ -72,8 +64,8 @@ inline void pad2(int n, fmt::basic_memory_buffer &dest) } } -template -inline void pad_uint(T n, unsigned int width, fmt::basic_memory_buffer &dest) +template +inline void pad_uint(T n, unsigned int width, memory_buf_t &dest) { static_assert(std::is_unsigned::value, "pad_uint must get unsigned T"); auto digits = count_digits(n); @@ -85,20 +77,20 @@ inline void pad_uint(T n, unsigned int width, fmt::basic_memory_buffer -inline void pad3(T n, fmt::basic_memory_buffer &dest) +template +inline void pad3(T n, memory_buf_t &dest) { pad_uint(n, 3, dest); } -template -inline void pad6(T n, fmt::basic_memory_buffer &dest) +template +inline void pad6(T n, memory_buf_t &dest) { pad_uint(n, 6, dest); } -template -inline void pad9(T n, fmt::basic_memory_buffer &dest) +template +inline void pad9(T n, memory_buf_t &dest) { pad_uint(n, 9, dest); } diff --git a/include/spdlog/details/log_msg_buffer.h b/include/spdlog/details/log_msg_buffer.h index 0089cce5..2326f81f 100644 --- a/include/spdlog/details/log_msg_buffer.h +++ b/include/spdlog/details/log_msg_buffer.h @@ -14,7 +14,7 @@ namespace details { class log_msg_buffer : public log_msg { - fmt::basic_memory_buffer buffer; + memory_buf_t buffer; void update_string_views() { logger_name = string_view_t{buffer.data(), logger_name.size()}; diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 242405da..b40a5391 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -350,7 +350,7 @@ SPDLOG_INLINE void sleep_for_millis(int milliseconds) SPDLOG_NOEXCEPT #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) { - fmt::memory_buffer buf; + memory_buf_t buf; wstr_to_utf8buf(filename, buf); return fmt::to_string(buf); } @@ -406,7 +406,7 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT } #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) -SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t wstr, fmt::memory_buffer &target) +SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t wstr, memory_buf_t &target) { if (wstr.size() > static_cast(std::numeric_limits::max())) { diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 27694494..46f523f9 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -81,7 +81,7 @@ bool is_color_terminal() SPDLOG_NOEXCEPT; bool in_terminal(FILE *file) SPDLOG_NOEXCEPT; #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) -void wstr_to_utf8buf(basic_string_view_t wstr, fmt::memory_buffer &target); +void wstr_to_utf8buf(basic_string_view_t wstr, memory_buf_t &target); #endif } // namespace os diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index a086dcc5..5f395d59 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -35,7 +35,7 @@ namespace details { class scoped_padder { public: - scoped_padder(size_t wrapped_size, const padding_info &padinfo, fmt::memory_buffer &dest) + scoped_padder(size_t wrapped_size, const padding_info &padinfo, memory_buf_t &dest) : padinfo_(padinfo) , dest_(dest) { @@ -78,14 +78,14 @@ private: } const padding_info &padinfo_; - fmt::memory_buffer &dest_; + memory_buf_t &dest_; size_t total_pad_; string_view_t spaces_{" ", 64}; }; struct null_scoped_padder { - null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, fmt::memory_buffer & /*dest*/) {} + null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, memory_buf_t & /*dest*/) {} }; template @@ -96,7 +96,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { ScopedPadder p(msg.logger_name.size(), padinfo_, dest); fmt_helper::append_string_view(msg.logger_name, dest); @@ -112,7 +112,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { string_view_t &level_name = level::to_string_view(msg.level); ScopedPadder p(level_name.size(), padinfo_, dest); @@ -129,7 +129,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { string_view_t level_name{level::to_short_c_str(msg.level)}; ScopedPadder p(level_name.size(), padinfo_, dest); @@ -162,7 +162,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { string_view_t field_value{days[static_cast(tm_time.tm_wday)]}; ScopedPadder p(field_value.size(), padinfo_, dest); @@ -181,7 +181,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { string_view_t field_value{full_days[static_cast(tm_time.tm_wday)]}; ScopedPadder p(field_value.size(), padinfo_, dest); @@ -200,7 +200,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { string_view_t field_value{months[static_cast(tm_time.tm_mon)]}; ScopedPadder p(field_value.size(), padinfo_, dest); @@ -220,7 +220,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { string_view_t field_value{full_months[static_cast(tm_time.tm_mon)]}; ScopedPadder p(field_value.size(), padinfo_, dest); @@ -237,7 +237,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 24; ScopedPadder p(field_size, padinfo_, dest); @@ -269,7 +269,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 2; ScopedPadder p(field_size, padinfo_, dest); @@ -286,7 +286,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 10; ScopedPadder p(field_size, padinfo_, dest); @@ -308,7 +308,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 4; ScopedPadder p(field_size, padinfo_, dest); @@ -325,7 +325,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 2; ScopedPadder p(field_size, padinfo_, dest); @@ -342,7 +342,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 2; ScopedPadder p(field_size, padinfo_, dest); @@ -359,7 +359,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 2; ScopedPadder p(field_size, padinfo_, dest); @@ -376,7 +376,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 2; ScopedPadder p(field_size, padinfo_, dest); @@ -393,7 +393,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 2; ScopedPadder p(field_size, padinfo_, dest); @@ -410,7 +410,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 2; ScopedPadder p(field_size, padinfo_, dest); @@ -427,7 +427,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { auto millis = fmt_helper::time_fraction(msg.time); const size_t field_size = 3; @@ -445,7 +445,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { auto micros = fmt_helper::time_fraction(msg.time); @@ -464,7 +464,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { auto ns = fmt_helper::time_fraction(msg.time); const size_t field_size = 9; @@ -482,7 +482,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { const size_t field_size = 10; ScopedPadder p(field_size, padinfo_, dest); @@ -501,7 +501,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 2; ScopedPadder p(field_size, padinfo_, dest); @@ -518,7 +518,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 11; ScopedPadder p(field_size, padinfo_, dest); @@ -542,7 +542,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 5; ScopedPadder p(field_size, padinfo_, dest); @@ -562,7 +562,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 8; ScopedPadder p(field_size, padinfo_, dest); @@ -588,7 +588,7 @@ public: z_formatter(const z_formatter &) = delete; z_formatter &operator=(const z_formatter &) = delete; - void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 6; ScopedPadder p(field_size, padinfo_, dest); @@ -644,7 +644,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { const auto field_size = fmt_helper::count_digits(msg.thread_id); ScopedPadder p(field_size, padinfo_, dest); @@ -661,7 +661,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { const auto pid = static_cast(details::os::pid()); auto field_size = fmt_helper::count_digits(pid); @@ -678,7 +678,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { ScopedPadder p(msg.payload.size(), padinfo_, dest); fmt_helper::append_string_view(msg.payload, dest); @@ -692,7 +692,7 @@ public: : ch_(ch) {} - void format(const details::log_msg &, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { dest.push_back(ch_); } @@ -711,7 +711,7 @@ public: { str_ += ch; } - void format(const details::log_msg &, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { fmt_helper::append_string_view(str_, dest); } @@ -728,7 +728,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { msg.color_range_start = dest.size(); } @@ -741,7 +741,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { msg.color_range_end = dest.size(); } @@ -756,7 +756,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { if (msg.source.empty()) { @@ -782,7 +782,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { if (msg.source.empty()) { @@ -808,7 +808,7 @@ public: return rv != nullptr ? rv + 1 : filename; } - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { if (msg.source.empty()) { @@ -829,7 +829,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { if (msg.source.empty()) { @@ -851,7 +851,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { if (msg.source.empty()) { @@ -876,7 +876,7 @@ public: , last_message_time_(log_clock::now()) {} - void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { auto delta = msg.time - last_message_time_; auto delta_units = std::chrono::duration_cast(delta); @@ -898,7 +898,7 @@ public: : flag_formatter(padinfo) {} - void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) override + void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override { using std::chrono::duration_cast; using std::chrono::milliseconds; @@ -934,7 +934,7 @@ public: cache_timestamp_ = secs; } - fmt_helper::append_buf(cached_datetime_, dest); + dest.append(cached_datetime_.data(), cached_datetime_.data()+cached_datetime_.size()); auto millis = fmt_helper::time_fraction(msg.time); fmt_helper::pad3(static_cast(millis.count()), dest); @@ -981,7 +981,7 @@ public: private: std::chrono::seconds cache_timestamp_{0}; - fmt::basic_memory_buffer cached_datetime_; + memory_buf_t cached_datetime_; }; } // namespace details @@ -1012,7 +1012,7 @@ SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const return details::make_unique(pattern_, pattern_time_type_, eol_); } -SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, fmt::memory_buffer &dest) +SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) { #ifndef SPDLOG_NO_DATETIME auto secs = std::chrono::duration_cast(msg.time.time_since_epoch()); diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index 1fcaf394..7191ea9b 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -50,7 +50,7 @@ public: {} flag_formatter() = default; virtual ~flag_formatter() = default; - virtual void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) = 0; + virtual void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0; protected: padding_info padinfo_; @@ -71,7 +71,7 @@ public: pattern_formatter &operator=(const pattern_formatter &other) = delete; std::unique_ptr clone() const override; - void format(const details::log_msg &msg, fmt::memory_buffer &dest) override; + void format(const details::log_msg &msg, memory_buf_t &dest) override; private: std::string pattern_; diff --git a/include/spdlog/formatter.h b/include/spdlog/formatter.h index b133f6e9..9a4ed035 100644 --- a/include/spdlog/formatter.h +++ b/include/spdlog/formatter.h @@ -12,7 +12,7 @@ class formatter { public: virtual ~formatter() = default; - virtual void format(const details::log_msg &msg, fmt::memory_buffer &dest) = 0; + virtual void format(const details::log_msg &msg, memory_buf_t &dest) = 0; virtual std::unique_ptr clone() const = 0; }; } // namespace spdlog diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 8151603e..767abdbd 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -87,7 +87,7 @@ public: } SPDLOG_TRY { - fmt::memory_buffer buf; + memory_buf_t buf; fmt::format_to(buf, fmt, args...); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); if (level_enabled) @@ -243,7 +243,7 @@ public: fmt::wmemory_buffer wbuf; fmt::format_to(wbuf, fmt, args...); - fmt::memory_buffer buf; + 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())); @@ -312,7 +312,7 @@ public: try { - fmt::memory_buffer buf; + 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())); diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index 14c6ff3b..d1e87f2d 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -40,7 +40,7 @@ protected: void sink_it_(const details::log_msg &msg) override { const android_LogPriority priority = convert_to_android_(msg.level); - fmt::memory_buffer formatted; + memory_buf_t formatted; if (use_raw_msg_) { details::fmt_helper::append_string_view(msg.payload, formatted); diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 13a0691e..280ad7cd 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -44,7 +44,7 @@ SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg // If color is not supported in the terminal, log as is instead. std::lock_guard lock(mutex_); - fmt::memory_buffer formatted; + memory_buf_t formatted; formatter_->format(msg, formatted); if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { @@ -115,7 +115,7 @@ SPDLOG_INLINE void ansicolor_sink::print_ccode_(const string_view_ } template -SPDLOG_INLINE void ansicolor_sink::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end) +SPDLOG_INLINE void ansicolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) { fwrite(formatted.data() + start, sizeof(char), end - start, target_file_); } diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index f383b5a5..6ef10e78 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -82,7 +82,7 @@ private: std::unique_ptr formatter_; std::unordered_map colors_; void print_ccode_(const string_view_t &color_code); - void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end); + void print_range_(const memory_buf_t &formatted, size_t start, size_t end); }; template diff --git a/include/spdlog/sinks/basic_file_sink-inl.h b/include/spdlog/sinks/basic_file_sink-inl.h index 108b8ff7..0e5275a9 100644 --- a/include/spdlog/sinks/basic_file_sink-inl.h +++ b/include/spdlog/sinks/basic_file_sink-inl.h @@ -28,7 +28,7 @@ SPDLOG_INLINE const filename_t &basic_file_sink::filename() const template SPDLOG_INLINE void basic_file_sink::sink_it_(const details::log_msg &msg) { - fmt::memory_buffer formatted; + memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); file_helper_.write(formatted); } diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 341b5c42..7fc0626e 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -30,7 +30,7 @@ struct daily_filename_calculator { filename_t basename, ext; std::tie(basename, ext) = details::file_helper::split_by_extension(filename); - std::conditional::value, fmt::memory_buffer, fmt::wmemory_buffer>::type w; + std::conditional::value, memory_buf_t, fmt::wmemory_buffer>::type w; fmt::format_to( w, SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext); return fmt::to_string(w); @@ -78,7 +78,7 @@ protected: file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(time)), truncate_); rotation_tp_ = next_rotation_tp_(); } - fmt::memory_buffer formatted; + memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); file_helper_.write(formatted); } diff --git a/include/spdlog/sinks/dup_filter_sink.h b/include/spdlog/sinks/dup_filter_sink.h index 66c59359..ab116a05 100644 --- a/include/spdlog/sinks/dup_filter_sink.h +++ b/include/spdlog/sinks/dup_filter_sink.h @@ -66,7 +66,7 @@ protected: // log the "skipped.." message if (skip_counter_ > 0) { - fmt::basic_memory_buffer buf; + memory_buf_t buf; fmt::format_to(buf, "Skipped {} duplicate messages..", skip_counter_); details::log_msg skipped_msg{msg.logger_name, msg.level, string_view_t{buf.data(), buf.size()}}; dist_sink::sink_it_(skipped_msg); diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index 08e029a0..39b2ce91 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -28,7 +28,7 @@ protected: void sink_it_(const details::log_msg &msg) override { - fmt::memory_buffer formatted; + memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); OutputDebugStringA(fmt::to_string(formatted).c_str()); } diff --git a/include/spdlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h index 6a728a3a..8d47d770 100644 --- a/include/spdlog/sinks/ostream_sink.h +++ b/include/spdlog/sinks/ostream_sink.h @@ -25,7 +25,7 @@ public: protected: void sink_it_(const details::log_msg &msg) override { - fmt::memory_buffer formatted; + memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); ostream_.write(formatted.data(), static_cast(formatted.size())); if (force_flush_) diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index cb67e337..5e2b1fd9 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -43,7 +43,7 @@ SPDLOG_INLINE rotating_file_sink::rotating_file_sink( template SPDLOG_INLINE filename_t rotating_file_sink::calc_filename(const filename_t &filename, std::size_t index) { - typename std::conditional::value, fmt::memory_buffer, fmt::wmemory_buffer>::type w; + typename std::conditional::value, memory_buf_t, fmt::wmemory_buffer>::type w; if (index != 0u) { filename_t basename, ext; @@ -66,7 +66,7 @@ SPDLOG_INLINE const filename_t &rotating_file_sink::filename() const template SPDLOG_INLINE void rotating_file_sink::sink_it_(const details::log_msg &msg) { - fmt::memory_buffer formatted; + memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); current_size_ += formatted.size(); if (current_size_ > max_size_) diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h index 19ecdaae..e7894987 100644 --- a/include/spdlog/sinks/stdout_sinks-inl.h +++ b/include/spdlog/sinks/stdout_sinks-inl.h @@ -26,7 +26,7 @@ template SPDLOG_INLINE void stdout_sink_base::log(const details::log_msg &msg) { std::lock_guard lock(mutex_); - fmt::memory_buffer formatted; + memory_buf_t formatted; formatter_->format(msg, formatted); fwrite(formatted.data(), sizeof(char), formatted.size(), file_); fflush(file_); // flush every line to terminal diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index fe868c32..aeaa943e 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -50,7 +50,7 @@ protected: if (enable_formatting_) { - fmt::memory_buffer formatted; + memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); payload = string_view_t(formatted.data(), formatted.size()); } diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index e058ce7c..84dbbff2 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -52,7 +52,7 @@ template void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) { std::lock_guard lock(mutex_); - fmt::memory_buffer formatted; + memory_buf_t formatted; formatter_->format(msg, formatted); if (!in_console_) { @@ -131,14 +131,14 @@ WORD SPDLOG_INLINE wincolor_sink::set_foreground_color_(WORD attri // print a range of formatted message to console template -void SPDLOG_INLINE wincolor_sink::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end) +void SPDLOG_INLINE wincolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) { auto size = static_cast(end - start); ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr); } template -void SPDLOG_INLINE wincolor_sink::write_to_file_(const fmt::memory_buffer &formatted) +void SPDLOG_INLINE wincolor_sink::write_to_file_(const memory_buf_t &formatted) { auto size = static_cast(formatted.size()); if (size == 0) diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 0ff43e07..22099e28 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -58,10 +58,10 @@ protected: WORD set_foreground_color_(WORD attribs); // print a range of formatted message to console - void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end); + void print_range_(const memory_buf_t &formatted, size_t start, size_t end); // in case we are redirected to file (not in console mode) - void write_to_file_(const fmt::memory_buffer &formatted); + void write_to_file_(const memory_buf_t &formatted); }; template diff --git a/tests/test_file_helper.cpp b/tests/test_file_helper.cpp index 073ef93a..74e3744e 100644 --- a/tests/test_file_helper.cpp +++ b/tests/test_file_helper.cpp @@ -10,7 +10,7 @@ static const std::string target_filename = "logs/file_helper_test.txt"; static void write_with_helper(file_helper &helper, size_t howmany) { - fmt::memory_buffer formatted; + spdlog::memory_buf_t formatted; fmt::format_to(formatted, "{}", std::string(howmany, '1')); helper.write(formatted); helper.flush(); diff --git a/tests/test_fmt_helper.cpp b/tests/test_fmt_helper.cpp index 5af6f8a7..0e10f4de 100644 --- a/tests/test_fmt_helper.cpp +++ b/tests/test_fmt_helper.cpp @@ -2,30 +2,32 @@ #include "includes.h" #include "spdlog/details/fmt_helper.h" +using spdlog::memory_buf_t; + void test_pad2(int n, const char *expected) { - fmt::memory_buffer buf; + memory_buf_t buf; spdlog::details::fmt_helper::pad2(n, buf); REQUIRE(fmt::to_string(buf) == expected); } void test_pad3(uint32_t n, const char *expected) { - fmt::memory_buffer buf; + memory_buf_t buf; spdlog::details::fmt_helper::pad3(n, buf); REQUIRE(fmt::to_string(buf) == expected); } void test_pad6(std::size_t n, const char *expected) { - fmt::memory_buffer buf; + memory_buf_t buf; spdlog::details::fmt_helper::pad6(n, buf); REQUIRE(fmt::to_string(buf) == expected); } void test_pad9(std::size_t n, const char *expected) { - fmt::memory_buffer buf; + memory_buf_t buf; spdlog::details::fmt_helper::pad9(n, buf); REQUIRE(fmt::to_string(buf) == expected); } diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index cda99071..9bf8d4b1 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -1,8 +1,9 @@ #include "includes.h" +using spdlog::memory_buf_t; + // log to str and return it template - static std::string log_to_str(const std::string &msg, const Args &... args) { std::ostringstream oss; @@ -61,9 +62,9 @@ TEST_CASE("color range test1", "[pattern_formatter]") { auto formatter = std::make_shared("%^%v%$", spdlog::pattern_time_type::local, "\n"); - fmt::memory_buffer buf; + memory_buf_t buf; fmt::format_to(buf, "Hello"); - fmt::memory_buffer formatted; + memory_buf_t formatted; std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, spdlog::string_view_t(buf.data(), buf.size())); formatter->format(msg, formatted); @@ -77,7 +78,7 @@ TEST_CASE("color range test2", "[pattern_formatter]") auto formatter = std::make_shared("%^%$", spdlog::pattern_time_type::local, "\n"); std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, ""); - fmt::memory_buffer formatted; + memory_buf_t formatted; formatter->format(msg, formatted); REQUIRE(msg.color_range_start == 0); REQUIRE(msg.color_range_end == 0); @@ -89,7 +90,7 @@ TEST_CASE("color range test3", "[pattern_formatter]") auto formatter = std::make_shared("%^***%$"); std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored"); - fmt::memory_buffer formatted; + memory_buf_t formatted; formatter->format(msg, formatted); REQUIRE(msg.color_range_start == 0); REQUIRE(msg.color_range_end == 3); @@ -101,7 +102,7 @@ TEST_CASE("color range test4", "[pattern_formatter]") std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored"); - fmt::memory_buffer formatted; + memory_buf_t formatted; formatter->format(msg, formatted); REQUIRE(msg.color_range_start == 2); REQUIRE(msg.color_range_end == 5); @@ -113,7 +114,7 @@ TEST_CASE("color range test5", "[pattern_formatter]") auto formatter = std::make_shared("**%^"); std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored"); - fmt::memory_buffer formatted; + memory_buf_t formatted; formatter->format(msg, formatted); REQUIRE(msg.color_range_start == 2); REQUIRE(msg.color_range_end == 0); @@ -124,7 +125,7 @@ TEST_CASE("color range test6", "[pattern_formatter]") auto formatter = std::make_shared("**%$"); std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored"); - fmt::memory_buffer formatted; + memory_buf_t formatted; formatter->format(msg, formatted); REQUIRE(msg.color_range_start == 0); REQUIRE(msg.color_range_end == 2); @@ -198,8 +199,8 @@ TEST_CASE("clone-default-formatter", "[pattern_formatter]") std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message"); - fmt::memory_buffer formatted_1; - fmt::memory_buffer formatted_2; + memory_buf_t formatted_1; + memory_buf_t formatted_2; formatter_1->format(msg, formatted_1); formatter_2->format(msg, formatted_2); @@ -213,8 +214,8 @@ TEST_CASE("clone-default-formatter2", "[pattern_formatter]") std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message"); - fmt::memory_buffer formatted_1; - fmt::memory_buffer formatted_2; + memory_buf_t formatted_1; + memory_buf_t formatted_2; formatter_1->format(msg, formatted_1); formatter_2->format(msg, formatted_2); @@ -228,8 +229,8 @@ TEST_CASE("clone-formatter", "[pattern_formatter]") std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message"); - fmt::memory_buffer formatted_1; - fmt::memory_buffer formatted_2; + memory_buf_t formatted_1; + memory_buf_t formatted_2; formatter_1->format(msg, formatted_1); formatter_2->format(msg, formatted_2); REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2)); @@ -243,8 +244,8 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]") std::string logger_name = "test2"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message"); - fmt::memory_buffer formatted_1; - fmt::memory_buffer formatted_2; + memory_buf_t formatted_1; + memory_buf_t formatted_2; formatter_1->format(msg, formatted_1); formatter_2->format(msg, formatted_2); REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2)); @@ -263,7 +264,7 @@ static const char *test_path = "/a/b//myfile.cpp"; TEST_CASE("short filename formatter-1", "[pattern_formatter]") { spdlog::pattern_formatter formatter("%s", spdlog::pattern_time_type::local, ""); - fmt::memory_buffer formatted; + memory_buf_t formatted; std::string logger_name = "logger-name"; spdlog::source_loc source_loc{test_path, 123, "some_func()"}; spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello"); @@ -274,7 +275,7 @@ TEST_CASE("short filename formatter-1", "[pattern_formatter]") TEST_CASE("short filename formatter-2", "[pattern_formatter]") { spdlog::pattern_formatter formatter("%s:%#", spdlog::pattern_time_type::local, ""); - fmt::memory_buffer formatted; + memory_buf_t formatted; std::string logger_name = "logger-name"; spdlog::source_loc source_loc{"myfile.cpp", 123, "some_func()"}; spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello"); @@ -285,7 +286,7 @@ TEST_CASE("short filename formatter-2", "[pattern_formatter]") TEST_CASE("short filename formatter-3", "[pattern_formatter]") { spdlog::pattern_formatter formatter("%s %v", spdlog::pattern_time_type::local, ""); - fmt::memory_buffer formatted; + memory_buf_t formatted; std::string logger_name = "logger-name"; spdlog::source_loc source_loc{"", 123, "some_func()"}; spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello"); @@ -296,7 +297,7 @@ TEST_CASE("short filename formatter-3", "[pattern_formatter]") TEST_CASE("full filename formatter", "[pattern_formatter]") { spdlog::pattern_formatter formatter("%g", spdlog::pattern_time_type::local, ""); - fmt::memory_buffer formatted; + memory_buf_t formatted; std::string logger_name = "logger-name"; spdlog::source_loc source_loc{test_path, 123, "some_func()"}; spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello"); diff --git a/tests/test_sink.h b/tests/test_sink.h index 08945a85..57db65c1 100644 --- a/tests/test_sink.h +++ b/tests/test_sink.h @@ -49,7 +49,7 @@ public: protected: void sink_it_(const details::log_msg &msg) override { - fmt::memory_buffer formatted; + memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); // save the line without the eol auto eol_len = strlen(details::os::default_eol);