From a12a21a18e0f0142bbe6dc337e80b7944c997fc4 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 25 Jul 2018 23:33:03 +0300 Subject: [PATCH] Improved millis formatting --- include/spdlog/details/fmt_helper.h | 46 +++++----------------- include/spdlog/details/pattern_formatter.h | 32 ++++++--------- include/spdlog/details/thread_pool.h | 2 +- 3 files changed, 24 insertions(+), 56 deletions(-) diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 1997c35d..455cbc0a 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -68,11 +68,18 @@ inline void pad2(int n, fmt::basic_memory_buffer &dest) template inline void pad3(int n, fmt::basic_memory_buffer &dest) { - if (n > 99) + if (n > 999) { append_int(n, dest); return; } + + if (n > 99) // 100-999 + { + append_int(n / 100, dest); + pad2(n % 100, dest); + return; + } if (n > 9) // 10-99 { dest.push_back('0'); @@ -94,46 +101,13 @@ inline void pad3(int n, fmt::basic_memory_buffer &dest) template inline void pad6(size_t n, fmt::basic_memory_buffer &dest) { - // todo: maybe replace this implementation with - // pad3(n / 1000, dest); - // pad3(n % 1000, dest); - if (n > 99999) { append_int(n, dest); return; } - if (n > 9999) - { - dest.push_back('0'); - } - else if (n > 999) - { - dest.push_back('0'); - dest.push_back('0'); - } - else if (n > 99) - { - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - } - else if (n > 9) - { - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - } - else // 0-9 - { - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - dest.push_back('0'); - } - append_int(n, dest); + pad3(static_cast(n / 1000), dest); + pad3(static_cast(n % 1000), dest); } } // namespace fmt_helper diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index 78a477b8..5c1804de 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -69,7 +69,7 @@ static const char *ampm(const tm &t) return t.tm_hour >= 12 ? "PM" : "AM"; } -static int to12h(const tm &t) +static unsigned int to12h(const tm &t) { return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour; } @@ -231,9 +231,11 @@ class e_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { + using namespace std::chrono; auto duration = msg.time.time_since_epoch(); - auto millis = std::chrono::duration_cast(duration).count() % 1000; - fmt_helper::pad3(static_cast(millis), dest); + auto secs = duration_cast(duration); + auto millis = duration_cast(duration) - duration_cast(secs); + fmt_helper::pad3(static_cast(millis.count()), dest); } }; @@ -464,13 +466,14 @@ class full_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) override { + using namespace std::chrono; #ifndef SPDLOG_NO_DATETIME // cache the date/time part for the next second. auto duration = msg.time.time_since_epoch(); - std::chrono::seconds seconds = std::chrono::duration_cast(duration); + auto secs = duration_cast(duration); - if (cache_timestamp_ != seconds || cached_datetime_.size() == 0) + if (cache_timestamp_ != secs || cached_datetime_.size() == 0) { cached_datetime_.resize(0); cached_datetime_.push_back('['); @@ -492,22 +495,15 @@ class full_formatter SPDLOG_FINAL : public flag_formatter fmt_helper::pad2(tm_time.tm_sec, cached_datetime_); cached_datetime_.push_back('.'); - cache_timestamp_ = seconds; + cache_timestamp_ = secs; } fmt_helper::append_buf(cached_datetime_, dest); - // cache the millis part for the next milli. - auto millis = std::chrono::duration_cast(duration).count() % 1000; - if (millis != millis_cache_timestamp_ || cached_millis_.size() == 0) - { - cached_millis_.resize(0); - fmt_helper::pad3(static_cast(millis), cached_millis_); - cached_millis_.push_back(']'); - cached_millis_.push_back(' '); - millis_cache_timestamp_ = millis; - } + auto millis = duration_cast(duration) - duration_cast(secs); + fmt_helper::pad3(static_cast(millis.count()), dest); + dest.push_back(']'); + dest.push_back(' '); - fmt_helper::append_buf(cached_millis_, dest); #else // no datetime needed (void)tm_time; #endif @@ -531,9 +527,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter private: std::chrono::seconds cache_timestamp_{0}; - std::chrono::milliseconds::rep millis_cache_timestamp_{0}; fmt::basic_memory_buffer cached_datetime_; - fmt::basic_memory_buffer cached_millis_; }; } // namespace details diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index 11b9d429..2620a19e 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -108,7 +108,7 @@ class thread_pool { public: using item_type = async_msg; - using q_type = details::mpmc_blocking_queue; + using q_type = details::mpmc_blocking_queue; thread_pool(size_t q_max_items, size_t threads_n) : q_(q_max_items)