From f09d0f230163c6e09a8d83d0bbe94ac7162cc9ed Mon Sep 17 00:00:00 2001 From: Daniel Chabrowski Date: Mon, 19 Nov 2018 17:15:39 +0100 Subject: [PATCH] Add helper for count_digits --- include/spdlog/details/fmt_helper.h | 10 +++++++++- include/spdlog/details/pattern_formatter.h | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index c8db3109..f786c4ee 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -18,6 +18,7 @@ inline spdlog::string_view_t to_string_view(const fmt::basic_memory_buffer inline void append_buf(const fmt::basic_memory_buffer &buf, fmt::basic_memory_buffer &dest) { @@ -42,6 +43,13 @@ inline void append_int(T n, fmt::basic_memory_buffer &dest) dest.append(i.data(), i.data() + i.size()); } +template +inline unsigned count_digits(T n) +{ + using count_type = std::conditional<(sizeof(std::size_t) > sizeof(std::uint32_t)), std::uint64_t, std::uint32_t>::type; + return fmt::internal::count_digits(static_cast(n)); +} + template inline void pad2(int n, fmt::basic_memory_buffer &dest) { @@ -69,7 +77,7 @@ template inline void pad_uint(T n, unsigned int width, fmt::basic_memory_buffer &dest) { static_assert(std::is_unsigned::value, "append_uint must get unsigned T"); - auto digits = fmt::internal::count_digits(static_cast(n)); + auto digits = count_digits(n); if (width > digits) { const char *zeroes = "0000000000000000000"; diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index bbc07b3a..a51b2132 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -652,7 +652,7 @@ public: void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { - const auto field_size = fmt::internal::count_digits(static_cast(msg.thread_id)); + const auto field_size = fmt_helper::count_digits(msg.thread_id); scoped_pad p(field_size, padinfo_, dest); fmt_helper::append_int(msg.thread_id, dest); }