spdlog
Loading...
Searching...
No Matches
fmt_helper.h
Go to the documentation of this file.
1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3#pragma once
4
5#include <chrono>
6#include <type_traits>
7#include <iterator>
8#include <spdlog/fmt/fmt.h>
9#include <spdlog/common.h>
10
11// Some fmt helpers to efficiently format and pad ints and strings
12namespace spdlog {
13namespace details {
14namespace fmt_helper {
15
16inline spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEXCEPT
17{
18 return spdlog::string_view_t{buf.data(), buf.size()};
19}
20
22{
23 auto *buf_ptr = view.data();
24 dest.append(buf_ptr, buf_ptr + view.size());
25}
26
27template<typename T>
28inline void append_int(T n, memory_buf_t &dest)
29{
30 fmt::format_int i(n);
31 dest.append(i.data(), i.data() + i.size());
32}
33
34template<typename T>
35inline unsigned int count_digits(T n)
36{
37 using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
38 return static_cast<unsigned int>(fmt::
39// fmt 7.0.0 renamed the internal namespace to detail.
40// See: https://github.com/fmtlib/fmt/issues/1538
41#if FMT_VERSION < 70000
42 internal
43#else
44 detail
45#endif
46 ::count_digits(static_cast<count_type>(n)));
47}
48
49inline void pad2(int n, memory_buf_t &dest)
50{
51 if (n >= 0 && n < 100) // 0-99
52 {
53 dest.push_back(static_cast<char>('0' + n / 10));
54 dest.push_back(static_cast<char>('0' + n % 10));
55 }
56 else // unlikely, but just in case, let fmt deal with it
57 {
58 fmt::format_to(std::back_inserter(dest), SPDLOG_FMT_RUNTIME("{:02}"), n);
59 }
60}
61
62template<typename T>
63inline void pad_uint(T n, unsigned int width, memory_buf_t &dest)
64{
65 static_assert(std::is_unsigned<T>::value, "pad_uint must get unsigned T");
66 for (auto digits = count_digits(n); digits < width; digits++)
67 {
68 dest.push_back('0');
69 }
70 append_int(n, dest);
71}
72
73template<typename T>
74inline void pad3(T n, memory_buf_t &dest)
75{
76 static_assert(std::is_unsigned<T>::value, "pad3 must get unsigned T");
77 if (n < 1000)
78 {
79 dest.push_back(static_cast<char>(n / 100 + '0'));
80 n = n % 100;
81 dest.push_back(static_cast<char>((n / 10) + '0'));
82 dest.push_back(static_cast<char>((n % 10) + '0'));
83 }
84 else
85 {
86 append_int(n, dest);
87 }
88}
89
90template<typename T>
91inline void pad6(T n, memory_buf_t &dest)
92{
93 pad_uint(n, 6, dest);
94}
95
96template<typename T>
97inline void pad9(T n, memory_buf_t &dest)
98{
99 pad_uint(n, 9, dest);
100}
101
102// return fraction of a second of the given time_point.
103// e.g.
104// fraction<std::milliseconds>(tp) -> will return the millis part of the second
105template<typename ToDuration>
106inline ToDuration time_fraction(log_clock::time_point tp)
107{
110 auto duration = tp.time_since_epoch();
111 auto secs = duration_cast<seconds>(duration);
112 return duration_cast<ToDuration>(duration) - duration_cast<ToDuration>(secs);
113}
114
115} // namespace fmt_helper
116} // namespace details
117} // namespace spdlog
T back_inserter(T... args)
#define SPDLOG_NOEXCEPT
Definition common.h:53
#define SPDLOG_FMT_RUNTIME(format_string)
Definition common.h:45
type
Definition core.h:1048
T
Definition core.h:320
T duration_cast(T... args)
Definition args.h:19
void pad6(T n, memory_buf_t &dest)
Definition fmt_helper.h:91
unsigned int count_digits(T n)
Definition fmt_helper.h:35
void pad_uint(T n, unsigned int width, memory_buf_t &dest)
Definition fmt_helper.h:63
void pad3(T n, memory_buf_t &dest)
Definition fmt_helper.h:74
void append_string_view(spdlog::string_view_t view, memory_buf_t &dest)
Definition fmt_helper.h:21
ToDuration time_fraction(log_clock::time_point tp)
Definition fmt_helper.h:106
void pad9(T n, memory_buf_t &dest)
Definition fmt_helper.h:97
void pad2(int n, memory_buf_t &dest)
Definition fmt_helper.h:49
void append_int(T n, memory_buf_t &dest)
Definition fmt_helper.h:28
Definition async.h:25
fmt::basic_string_view< char > string_view_t
Definition common.h:114
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:116
Definition core.h:971