From 394f79e9d30d4377404ff4cd4de8940b6d1e6544 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 15 Apr 2020 19:44:54 +0300 Subject: [PATCH] small refactoring --- include/spdlog/pattern_formatter-inl.h | 83 +++++++------------------- 1 file changed, 23 insertions(+), 60 deletions(-) diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index e9c18cb8..e24ed42d 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -59,6 +59,12 @@ public: } } + template + static unsigned int count_digits(T n) + { + return fmt_helper::count_digits(n); + } + ~scoped_padder() { if (remaining_pad_ >= 0) @@ -87,6 +93,9 @@ private: struct null_scoped_padder { null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, memory_buf_t & /*dest*/) {} + + template + static unsigned int count_digits(T /* number */) { return 0;} }; template @@ -638,26 +647,12 @@ public: 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); + const auto field_size = ScopedPadder::count_digits(msg.thread_id); ScopedPadder p(field_size, padinfo_, dest); fmt_helper::append_int(msg.thread_id, dest); } }; -// If padding is not needed, there is no need to count the digits of the thread id -template<> -class t_formatter final : public flag_formatter -{ -public: - explicit t_formatter(padding_info padinfo) - : flag_formatter(padinfo) - {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override - { - fmt_helper::append_int(msg.thread_id, dest); - } -}; // Current pid template @@ -671,28 +666,12 @@ public: 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); + auto field_size = ScopedPadder::count_digits(pid); ScopedPadder p(field_size, padinfo_, dest); fmt_helper::append_int(pid, dest); } }; -// If padding is not needed, there is no need to count the digits of the pid -template<> -class pid_formatter final : public flag_formatter -{ -public: - explicit pid_formatter(padding_info padinfo) - : flag_formatter(padinfo) - {} - - void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override - { - const auto pid = static_cast(details::os::pid()); - fmt_helper::append_int(pid, dest); - } -}; - template class v_formatter final : public flag_formatter { @@ -786,8 +765,16 @@ public: return; } - size_t text_size = - padinfo_.enabled() ? std::char_traits::length(msg.source.filename) + fmt_helper::count_digits(msg.source.line) + 1 : 0; + size_t text_size; + if(padinfo_.enabled()) + { + // calc text size for padding based on "filename:line" + text_size = std::char_traits::length(msg.source.filename) + ScopedPadder::count_digits(msg.source.line) + 1; + } + else + { + text_size = 0; + } ScopedPadder p(text_size, padinfo_, dest); fmt_helper::append_string_view(msg.source.filename, dest); @@ -859,7 +846,7 @@ public: return; } - auto field_size = fmt_helper::count_digits(msg.source.line); + auto field_size = ScopedPadder::count_digits(msg.source.line); ScopedPadder p(field_size, padinfo_, dest); fmt_helper::append_int(msg.source.line, dest); } @@ -904,7 +891,7 @@ public: auto delta_units = std::chrono::duration_cast(delta); last_message_time_ = msg.time; auto delta_count = static_cast(delta_units.count()); - auto n_digits = static_cast(fmt_helper::count_digits(delta_count)); + auto n_digits = static_cast(ScopedPadder::count_digits(delta_count)); ScopedPadder p(n_digits, padinfo_, dest); fmt_helper::append_int(delta_count, dest); } @@ -913,30 +900,6 @@ private: log_clock::time_point last_message_time_; }; -// If padding is not needed, there is no need to count the digits of the value -template -class elapsed_formatter final : public flag_formatter -{ -public: - using DurationUnits = Units; - - explicit elapsed_formatter(padding_info padinfo) - : flag_formatter(padinfo) - , last_message_time_(log_clock::now()) - {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override - { - auto delta = (std::max)(msg.time - last_message_time_, log_clock::duration::zero()); - auto delta_units = std::chrono::duration_cast(delta); - last_message_time_ = msg.time; - auto delta_count = static_cast(delta_units.count()); - fmt_helper::append_int(delta_count, dest); - } - -private: - log_clock::time_point last_message_time_; -}; // Full info formatter // pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v