diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index de6f998f..8632f01d 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -4,7 +4,8 @@ #pragma once -#include "chrono" +#include +#include #include "spdlog/fmt/fmt.h" // Some fmt helpers to efficiently format and pad ints and strings @@ -41,88 +42,65 @@ inline void append_int(T n, fmt::basic_memory_buffer &dest) dest.append(i.data(), i.data() + i.size()); } + + +template +inline void append_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) + { + const char* zeroes = "0000000000000000000"; + dest.append(zeroes, zeroes + width-digits); + } + append_int(n, dest); +} + template inline void pad2(int n, fmt::basic_memory_buffer &dest) { if (n > 99) { append_int(n, dest); - return; } - if (n > 9) // 10-99 + else if (n > 9) // 10-99 { dest.push_back(static_cast('0' + n / 10)); dest.push_back(static_cast('0' + n % 10)); - return; } - if (n >= 0) // 0-9 + else if (n >= 0) // 0-9 { dest.push_back('0'); dest.push_back(static_cast('0' + n)); - return; } - // negatives (unlikely, but just in case, let fmt deal with it) - fmt::format_to(dest, "{:02}", n); + else // negatives (unlikely, but just in case, let fmt deal with it) + { + fmt::format_to(dest, "{:02}", n); + } } -template -inline void pad3(int n, fmt::basic_memory_buffer &dest) +template +inline void pad3(T n, fmt::basic_memory_buffer &dest) { - if (n > 999) - { - append_int(n, dest); - return; - } - - if (n > 99) // 100-999 - { - dest.push_back(static_cast('0' + n / 100)); - pad2(n % 100, dest); - return; - } - if (n > 9) // 10-99 - { - dest.push_back('0'); - dest.push_back(static_cast('0' + n / 10)); - dest.push_back(static_cast('0' + n % 10)); - return; - } - if (n >= 0) - { - dest.push_back('0'); - dest.push_back('0'); - dest.push_back(static_cast('0' + n)); - return; - } - // negatives (unlikely, but just in case let fmt deal with it) - fmt::format_to(dest, "{:03}", n); + append_uint(n, 3, dest); } -template -inline void pad6(size_t n, fmt::basic_memory_buffer &dest) + +template +inline void pad6(T n, fmt::basic_memory_buffer &dest) { - if (n > 99999) - { - append_int(n, dest); - return; - } - pad3(static_cast(n / 1000), dest); - pad3(static_cast(n % 1000), dest); + append_uint(n, 6, dest); } - -template -inline void pad9(size_t n, fmt::basic_memory_buffer &dest) +template +inline void pad9(T n, fmt::basic_memory_buffer &dest) { - if (n > 99999999) - { - append_int(n, dest); - return; - } - pad6(static_cast(n / 1000), dest); - pad3(static_cast(n % 1000), dest); + append_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 a9b1a49c..ead3c4bf 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -452,7 +452,7 @@ public: scoped_pad p(field_size, padinfo_, dest); auto millis = fmt_helper::time_fraction(msg.time); - fmt_helper::pad3(static_cast(millis.count()), dest); + fmt_helper::pad3(static_cast(millis.count()), dest); } }; @@ -818,7 +818,7 @@ public: fmt_helper::append_buf(cached_datetime_, dest); auto millis = fmt_helper::time_fraction(msg.time); - fmt_helper::pad3(static_cast(millis.count()), dest); + fmt_helper::pad3(static_cast(millis.count()), dest); dest.push_back(']'); dest.push_back(' ');