From 4b0267910cd670c75e39ef41218ad93a265631b3 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 15 Jul 2019 01:17:22 +0300 Subject: [PATCH] Refactored padding handling in formatter using templates --- .../spdlog/details/pattern_formatter-inl.h | 26 +++++++------------ include/spdlog/details/pattern_formatter.h | 2 +- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index c0025b1c..90271b97 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -60,7 +60,7 @@ public: total_pad_ = half_pad + reminder; // for the right side } } - + ~scoped_padder() { if (total_pad_) @@ -85,8 +85,7 @@ private: 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*/, fmt::memory_buffer & /*dest*/) {} }; template @@ -434,7 +433,6 @@ public: const size_t field_size = 3; ScopedPadder p(field_size, padinfo_, dest); fmt_helper::pad3(static_cast(millis.count()), dest); - } }; @@ -454,7 +452,6 @@ public: const size_t field_size = 6; ScopedPadder p(field_size, padinfo_, dest); fmt_helper::pad6(static_cast(micros.count()), dest); - } }; @@ -579,7 +576,7 @@ public: }; // ISO 8601 offset from UTC in timezone (+-HH:MM) - template +template class z_formatter final : public flag_formatter { public: @@ -639,7 +636,7 @@ private: }; // Thread id - template +template class t_formatter final : public flag_formatter { public: @@ -766,9 +763,8 @@ 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 = + padinfo_.enabled() ? std::char_traits::length(msg.source.filename) + fmt_helper::count_digits(msg.source.line) + 1 : 0; ScopedPadder p(text_size, padinfo_, dest); fmt_helper::append_string_view(msg.source.filename, dest); @@ -792,7 +788,7 @@ public: { return; } - size_t text_size = padinfo_.enabled() ? std::char_traits::length(msg.source.filename): 0; + size_t text_size = padinfo_.enabled() ? std::char_traits::length(msg.source.filename) : 0; ScopedPadder p(text_size, padinfo_, dest); fmt_helper::append_string_view(msg.source.filename, dest); } @@ -825,7 +821,7 @@ public: } }; - template +template class source_linenum_formatter final : public flag_formatter { public: @@ -1043,8 +1039,7 @@ SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg) return details::os::gmtime(log_clock::to_time_t(msg.time)); } - -template +template SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_info padding) { switch (flag) @@ -1291,7 +1286,7 @@ SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &patter if (it != end) { - if(padding.enabled()) + if (padding.enabled()) { handle_flag_(*it, padding); } @@ -1299,7 +1294,6 @@ SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &patter { handle_flag_(*it, padding); } - } else { diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index 37f6c010..1fcaf394 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -82,7 +82,7 @@ private: std::vector> formatters_; std::tm get_time_(const details::log_msg &msg); - template + template void handle_flag_(char flag, details::padding_info padding); // Extract given pad spec (e.g. %8X)