2018-06-15 19:15:35 +08:00
|
|
|
//
|
|
|
|
// Created by gabi on 6/15/18.
|
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-07-26 05:20:31 +08:00
|
|
|
#include "chrono"
|
2018-10-17 22:40:41 +08:00
|
|
|
#include "spdlog/details/log_msg.h"
|
2018-07-26 05:20:31 +08:00
|
|
|
#include "spdlog/fmt/fmt.h"
|
|
|
|
|
2018-06-15 19:15:35 +08:00
|
|
|
// Some fmt helpers to efficiently format and pad ints and strings
|
|
|
|
namespace spdlog {
|
|
|
|
namespace details {
|
|
|
|
namespace fmt_helper {
|
2018-07-10 02:07:44 +08:00
|
|
|
|
2018-10-19 21:48:22 +08:00
|
|
|
template<size_t Buffer_Size>
|
|
|
|
inline fmt::string_view to_string_view(const fmt::basic_memory_buffer<char, Buffer_Size> &buf) SPDLOG_NOEXCEPT
|
|
|
|
{
|
|
|
|
return fmt::string_view(buf.data(), buf.size());
|
|
|
|
}
|
2018-07-10 02:07:44 +08: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 19:15:35 +08:00
|
|
|
{
|
2018-07-04 05:38:23 +08:00
|
|
|
auto *buf_ptr = buf.data();
|
2018-06-15 19:15:35 +08:00
|
|
|
dest.append(buf_ptr, buf_ptr + buf.size());
|
|
|
|
}
|
|
|
|
|
2018-10-19 07:15:50 +08:00
|
|
|
template<size_t Buffer_Size>
|
|
|
|
inline void append_string_view(fmt::string_view view, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
|
|
|
{
|
|
|
|
auto *buf_ptr = view.data();
|
2018-10-19 07:45:35 +08:00
|
|
|
if(buf_ptr != nullptr)
|
|
|
|
{
|
|
|
|
dest.append(buf_ptr, buf_ptr + view.size());
|
|
|
|
}
|
2018-10-19 07:15:50 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 02:07:44 +08: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 19:15:35 +08:00
|
|
|
{
|
|
|
|
fmt::format_int i(n);
|
|
|
|
dest.append(i.data(), i.data() + i.size());
|
|
|
|
}
|
|
|
|
|
2018-07-11 04:53:00 +08:00
|
|
|
template<size_t Buffer_Size>
|
2018-07-10 02:07:44 +08:00
|
|
|
inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
2018-06-15 19:15:35 +08:00
|
|
|
{
|
2018-06-26 07:00:33 +08:00
|
|
|
if (n > 99)
|
2018-06-15 19:15:35 +08:00
|
|
|
{
|
|
|
|
append_int(n, dest);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-26 07:00:33 +08:00
|
|
|
if (n > 9) // 10-99
|
|
|
|
{
|
2018-08-17 05:32:13 +08:00
|
|
|
dest.push_back(static_cast<char>('0' + n / 10));
|
|
|
|
dest.push_back(static_cast<char>('0' + n % 10));
|
2018-06-26 07:00:33 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-06-15 19:15:35 +08:00
|
|
|
if (n >= 0) // 0-9
|
|
|
|
{
|
|
|
|
dest.push_back('0');
|
2018-08-17 05:32:13 +08:00
|
|
|
dest.push_back(static_cast<char>('0' + n));
|
2018-06-15 19:15:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-06-26 07:00:33 +08:00
|
|
|
// negatives (unlikely, but just in case, let fmt deal with it)
|
2018-06-15 19:15:35 +08:00
|
|
|
fmt::format_to(dest, "{:02}", n);
|
|
|
|
}
|
|
|
|
|
2018-07-11 04:53:00 +08:00
|
|
|
template<size_t Buffer_Size>
|
2018-07-10 02:07:44 +08:00
|
|
|
inline void pad3(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
2018-06-15 19:15:35 +08:00
|
|
|
{
|
2018-07-26 04:33:03 +08:00
|
|
|
if (n > 999)
|
2018-06-15 19:15:35 +08:00
|
|
|
{
|
|
|
|
append_int(n, dest);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-26 04:33:03 +08:00
|
|
|
|
|
|
|
if (n > 99) // 100-999
|
|
|
|
{
|
2018-09-28 06:27:37 +08:00
|
|
|
dest.push_back(static_cast<char>('0' + n / 100));
|
2018-07-26 04:33:03 +08:00
|
|
|
pad2(n % 100, dest);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-15 19:15:35 +08:00
|
|
|
if (n > 9) // 10-99
|
|
|
|
{
|
|
|
|
dest.push_back('0');
|
2018-08-17 05:32:13 +08:00
|
|
|
dest.push_back(static_cast<char>('0' + n / 10));
|
|
|
|
dest.push_back(static_cast<char>('0' + n % 10));
|
2018-06-15 19:15:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-06-26 07:00:33 +08:00
|
|
|
if (n >= 0)
|
2018-06-15 19:15:35 +08:00
|
|
|
{
|
|
|
|
dest.push_back('0');
|
|
|
|
dest.push_back('0');
|
2018-08-17 05:32:13 +08:00
|
|
|
dest.push_back(static_cast<char>('0' + n));
|
2018-06-15 19:15:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-06-26 07:00:33 +08:00
|
|
|
// negatives (unlikely, but just in case let fmt deal with it)
|
|
|
|
fmt::format_to(dest, "{:03}", n);
|
2018-06-15 19:15:35 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 04:53:00 +08:00
|
|
|
template<size_t Buffer_Size>
|
2018-07-10 02:07:44 +08:00
|
|
|
inline void pad6(size_t n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
2018-07-04 06:41:05 +08:00
|
|
|
{
|
|
|
|
if (n > 99999)
|
|
|
|
{
|
|
|
|
append_int(n, dest);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-26 04:33:03 +08:00
|
|
|
pad3(static_cast<int>(n / 1000), dest);
|
|
|
|
pad3(static_cast<int>(n % 1000), dest);
|
2018-07-04 06:41:05 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 05:20:31 +08: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 05:23:44 +08:00
|
|
|
inline ToDuration time_fraction(const log_clock::time_point &tp)
|
2018-07-26 05:20:31 +08:00
|
|
|
{
|
2018-09-27 05:39:17 +08:00
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using std::chrono::seconds;
|
2018-07-26 05:20:31 +08: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 19:15:35 +08:00
|
|
|
} // namespace fmt_helper
|
|
|
|
} // namespace details
|
2018-08-08 22:59:57 +08:00
|
|
|
} // namespace spdlog
|