Commit f09d0f23 authored by Daniel Chabrowski's avatar Daniel Chabrowski

Add helper for count_digits

parent 14a071c4
...@@ -18,6 +18,7 @@ inline spdlog::string_view_t to_string_view(const fmt::basic_memory_buffer<char, ...@@ -18,6 +18,7 @@ inline spdlog::string_view_t to_string_view(const fmt::basic_memory_buffer<char,
{ {
return spdlog::string_view_t(buf.data(), buf.size()); return spdlog::string_view_t(buf.data(), buf.size());
} }
template<size_t Buffer_Size1, size_t Buffer_Size2> template<size_t Buffer_Size1, size_t Buffer_Size2>
inline void append_buf(const fmt::basic_memory_buffer<char, Buffer_Size1> &buf, fmt::basic_memory_buffer<char, Buffer_Size2> &dest) inline void append_buf(const fmt::basic_memory_buffer<char, Buffer_Size1> &buf, fmt::basic_memory_buffer<char, Buffer_Size2> &dest)
{ {
...@@ -42,6 +43,13 @@ inline void append_int(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest) ...@@ -42,6 +43,13 @@ inline void append_int(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
dest.append(i.data(), i.data() + i.size()); dest.append(i.data(), i.data() + i.size());
} }
template<typename T>
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<count_type>(n));
}
template<size_t Buffer_Size> template<size_t Buffer_Size>
inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest) inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{ {
...@@ -69,7 +77,7 @@ template<typename T, size_t Buffer_Size> ...@@ -69,7 +77,7 @@ template<typename T, size_t Buffer_Size>
inline void pad_uint(T n, unsigned int width, fmt::basic_memory_buffer<char, Buffer_Size> &dest) inline void pad_uint(T n, unsigned int width, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{ {
static_assert(std::is_unsigned<T>::value, "append_uint must get unsigned T"); static_assert(std::is_unsigned<T>::value, "append_uint must get unsigned T");
auto digits = fmt::internal::count_digits(static_cast<uint64_t>(n)); auto digits = count_digits(n);
if (width > digits) if (width > digits)
{ {
const char *zeroes = "0000000000000000000"; const char *zeroes = "0000000000000000000";
......
...@@ -652,7 +652,7 @@ public: ...@@ -652,7 +652,7 @@ public:
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override 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<uint64_t>(msg.thread_id)); const auto field_size = fmt_helper::count_digits(msg.thread_id);
scoped_pad p(field_size, padinfo_, dest); scoped_pad p(field_size, padinfo_, dest);
fmt_helper::append_int(msg.thread_id, dest); fmt_helper::append_int(msg.thread_id, dest);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment