From d38bd138cd5f12b3c6fbd2d70506fff54fb39962 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 12 Apr 2020 02:21:14 +0300 Subject: [PATCH] Micro optimze pattern-formatter when padding not needed --- include/spdlog/pattern_formatter-inl.h | 59 +++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 6bb319c8..35e84412 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -644,6 +644,22 @@ public: } }; +// 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 class pid_formatter final : public flag_formatter @@ -662,6 +678,23 @@ public: } }; +// 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 { @@ -857,7 +890,6 @@ public: // print elapsed time since last message template - class elapsed_formatter final : public flag_formatter { public: @@ -883,6 +915,31 @@ 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 class full_formatter final : public flag_formatter