Files
spdlog/include/spdlog/details/fmt_helper.h

122 lines
3.5 KiB
C
Raw Normal View History

2019-06-04 00:09:16 +03:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2019-05-11 20:06:17 +03:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2018-06-15 14:15:35 +03:00
#pragma once
2018-11-16 12:55:19 +02:00
#include <chrono>
#include <type_traits>
2018-07-26 00:20:31 +03:00
#include "spdlog/fmt/fmt.h"
2019-04-05 16:44:17 +03:00
#include "spdlog/common.h"
2018-07-26 00:20:31 +03:00
2018-06-15 14:15:35 +03:00
// Some fmt helpers to efficiently format and pad ints and strings
namespace spdlog {
namespace details {
namespace fmt_helper {
2018-07-09 21:07:44 +03:00
template<size_t Buffer_Size>
inline spdlog::string_view_t to_string_view(const fmt::basic_memory_buffer<char, Buffer_Size> &buf) SPDLOG_NOEXCEPT
{
return spdlog::string_view_t(buf.data(), buf.size());
}
2018-11-19 17:15:39 +01:00
2018-07-09 21:07:44 +03:00
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)
2018-06-15 14:15:35 +03:00
{
2018-07-04 00:38:23 +03:00
auto *buf_ptr = buf.data();
2018-06-15 14:15:35 +03:00
dest.append(buf_ptr, buf_ptr + buf.size());
}
template<size_t Buffer_Size>
inline void append_string_view(spdlog::string_view_t view, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
auto *buf_ptr = view.data();
2018-10-19 17:12:35 +03:00
if (buf_ptr != nullptr)
{
dest.append(buf_ptr, buf_ptr + view.size());
}
}
2018-07-09 21:07:44 +03:00
template<typename T, size_t Buffer_Size>
inline void append_int(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
2018-06-15 14:15:35 +03:00
{
fmt::format_int i(n);
dest.append(i.data(), i.data() + i.size());
}
2018-11-19 17:15:39 +01:00
template<typename T>
inline unsigned count_digits(T n)
{
2018-11-25 10:54:06 +02:00
using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
2019-01-08 17:31:46 +02:00
return static_cast<unsigned>(fmt::internal::count_digits(static_cast<count_type>(n)));
2018-11-19 17:15:39 +01:00
}
2018-07-10 23:53:00 +03:00
template<size_t Buffer_Size>
2018-11-16 12:55:19 +02:00
inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
2018-06-15 14:15:35 +03:00
{
2018-11-16 12:55:19 +02:00
if (n > 99)
2018-06-15 14:15:35 +03:00
{
append_int(n, dest);
2018-07-25 23:33:03 +03:00
}
2018-11-16 12:55:19 +02:00
else if (n > 9) // 10-99
2018-06-15 14:15:35 +03:00
{
dest.push_back(static_cast<char>('0' + n / 10));
dest.push_back(static_cast<char>('0' + n % 10));
2018-06-15 14:15:35 +03:00
}
2018-11-16 12:55:19 +02:00
else if (n >= 0) // 0-9
2018-06-15 14:15:35 +03:00
{
dest.push_back('0');
dest.push_back(static_cast<char>('0' + n));
2018-06-15 14:15:35 +03:00
}
2018-11-19 02:29:36 +01:00
else // negatives (unlikely, but just in case, let fmt deal with it)
2018-11-16 12:55:19 +02:00
{
fmt::format_to(dest, "{:02}", n);
}
2018-06-15 14:15:35 +03:00
}
2018-11-16 13:28:34 +02:00
template<typename T, size_t Buffer_Size>
inline void pad_uint(T n, unsigned int width, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
2018-11-27 11:37:09 +02:00
static_assert(std::is_unsigned<T>::value, "pad_uint must get unsigned T");
2018-11-19 17:15:39 +01:00
auto digits = count_digits(n);
2018-11-19 02:29:36 +01:00
if (width > digits)
2018-11-16 13:28:34 +02:00
{
2018-11-19 02:29:36 +01:00
const char *zeroes = "0000000000000000000";
dest.append(zeroes, zeroes + width - digits);
2018-11-16 13:28:34 +02:00
}
append_int(n, dest);
}
2018-11-16 12:55:19 +02:00
template<typename T, size_t Buffer_Size>
inline void pad3(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
2018-07-04 01:41:05 +03:00
{
2018-11-16 13:28:34 +02:00
pad_uint(n, 3, dest);
2018-07-04 01:41:05 +03:00
}
2018-11-16 12:55:19 +02:00
template<typename T, size_t Buffer_Size>
inline void pad6(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
2018-11-12 16:44:34 +02:00
{
2018-11-16 13:28:34 +02:00
pad_uint(n, 6, dest);
2018-11-16 12:55:19 +02:00
}
template<typename T, size_t Buffer_Size>
inline void pad9(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
2018-11-16 13:28:34 +02:00
pad_uint(n, 9, dest);
2018-11-12 16:44:34 +02:00
}
2018-07-26 00:20:31 +03:00
// return fraction of a second of the given time_point.
// e.g.
// fraction<std::milliseconds>(tp) -> will return the millis part of the second
template<typename ToDuration>
2018-07-26 00:23:44 +03:00
inline ToDuration time_fraction(const log_clock::time_point &tp)
2018-07-26 00:20:31 +03:00
{
2018-09-27 00:39:17 +03:00
using std::chrono::duration_cast;
using std::chrono::seconds;
2018-07-26 00:20:31 +03:00
auto duration = tp.time_since_epoch();
auto secs = duration_cast<seconds>(duration);
return duration_cast<ToDuration>(duration) - duration_cast<ToDuration>(secs);
}
2018-06-15 14:15:35 +03:00
} // namespace fmt_helper
} // namespace details
} // namespace spdlog