diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 63c94c7b..fb9946a4 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,12 @@ 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(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) @@ -60,23 +67,21 @@ inline void pad2(int n, fmt::basic_memory_buffer &dest) dest.push_back('0'); dest.push_back(static_cast('0' + n)); } - else // negatives (unlikely, but just in case, let fmt deal with it) + else // negatives (unlikely, but just in case, let fmt deal with it) { fmt::format_to(dest, "{:02}", n); } } - - 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(n); - if(width > digits) + auto digits = count_digits(n); + if (width > digits) { - const char* zeroes = "0000000000000000000"; - dest.append(zeroes, zeroes + width-digits); + const char *zeroes = "0000000000000000000"; + dest.append(zeroes, zeroes + width - digits); } append_int(n, dest); } @@ -87,7 +92,6 @@ inline void pad3(T n, fmt::basic_memory_buffer &dest) pad_uint(n, 3, dest); } - template inline void pad6(T n, fmt::basic_memory_buffer &dest) { @@ -100,8 +104,6 @@ inline void pad9(T n, fmt::basic_memory_buffer &dest) pad_uint(n, 9, dest); } - - // return fraction of a second of the given time_point. // e.g. // fraction(tp) -> will return the millis part of the second diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index 925b23bc..a51b2132 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -487,7 +487,6 @@ public: auto ns = fmt_helper::time_fraction(msg.time); fmt_helper::pad9(static_cast(ns.count()), dest); - } }; @@ -653,7 +652,7 @@ public: void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { - const size_t field_size = fmt::internal::count_digits(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); }