From c2a9bf997481adc360bdf621ff8ab148a695cc5a Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 19 Oct 2018 16:48:22 +0300 Subject: [PATCH] Small refactoring and support wchar again --- include/spdlog/details/fmt_helper.h | 5 +++++ include/spdlog/details/logger_impl.h | 13 +++++++++---- include/spdlog/details/pattern_formatter.h | 3 --- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 4de48899..818c988f 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -13,6 +13,11 @@ namespace spdlog { namespace details { namespace fmt_helper { +template +inline fmt::string_view to_string_view(const fmt::basic_memory_buffer &buf) SPDLOG_NOEXCEPT +{ + return fmt::string_view(buf.data(), buf.size()); +} template inline void append_buf(const fmt::basic_memory_buffer &buf, fmt::basic_memory_buffer &dest) { diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index a557d45d..7e307d7f 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -63,9 +63,10 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar try { + using details::fmt_helper::to_string_view; fmt::memory_buffer buf; fmt::format_to(buf, fmt, args...); - details::log_msg log_msg(&name_, lvl, fmt::string_view(buf.data(), buf.size())); + details::log_msg log_msg(&name_, lvl, to_string_view(buf)); sink_it_(log_msg); } SPDLOG_CATCH_AND_HANDLE @@ -95,9 +96,10 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg) } try { + using details::fmt_helper::to_string_view; fmt::memory_buffer buf; fmt::format_to(buf, "{}", msg); - details::log_msg log_msg(&name_, lvl, fmt::string_view(buf.data(), buf.size())); + details::log_msg log_msg(&name_, lvl, to_string_view(buf)); sink_it_(log_msg); } SPDLOG_CATCH_AND_HANDLE @@ -209,11 +211,14 @@ inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *fmt, const try { // format to wmemory_buffer and convert to utf8 - details::log_msg log_msg(&name_, lvl); + using details::fmt_helper::to_string_view; fmt::wmemory_buffer wbuf; fmt::format_to(wbuf, fmt, args...); - wbuf_to_utf8buf(wbuf, log_msg.raw); + fmt::memory_buffer buf; + wbuf_to_utf8buf(wbuf, buf); + details::log_msg log_msg(&name_, lvl, to_string_view(buf)); sink_it_(log_msg); + } SPDLOG_CATCH_AND_HANDLE } diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index de384948..d1ff0e9c 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -83,7 +83,6 @@ class a_formatter : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - // fmt_helper::append_string_view(days[tm_time.tm_wday], dest); fmt_helper::append_string_view(days[tm_time.tm_wday], dest); } }; @@ -94,7 +93,6 @@ class A_formatter : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - // fmt_helper::append_string_view(full_days[tm_time.tm_wday], dest); fmt_helper::append_string_view(full_days[tm_time.tm_wday], dest); } }; @@ -105,7 +103,6 @@ class b_formatter : public flag_formatter { void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { - // fmt_helper::append_string_view(months[tm_time.tm_mon], dest); fmt_helper::append_string_view(months[tm_time.tm_mon], dest); } };