Warning, file /include/spdlog/details/fmt_helper.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003 #pragma once
0004
0005 #include <chrono>
0006 #include <iterator>
0007 #include <spdlog/common.h>
0008 #include <spdlog/fmt/fmt.h>
0009 #include <type_traits>
0010
0011 #ifdef SPDLOG_USE_STD_FORMAT
0012 #include <charconv>
0013 #include <limits>
0014 #endif
0015
0016
0017 namespace spdlog {
0018 namespace details {
0019 namespace fmt_helper {
0020
0021 inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) {
0022 auto *buf_ptr = view.data();
0023 dest.append(buf_ptr, buf_ptr + view.size());
0024 }
0025
0026 #ifdef SPDLOG_USE_STD_FORMAT
0027 template <typename T>
0028 inline void append_int(T n, memory_buf_t &dest) {
0029
0030 SPDLOG_CONSTEXPR const auto BUF_SIZE = std::numeric_limits<T>::digits10 + 2;
0031 char buf[BUF_SIZE];
0032
0033 auto [ptr, ec] = std::to_chars(buf, buf + BUF_SIZE, n, 10);
0034 if (ec == std::errc()) {
0035 dest.append(buf, ptr);
0036 } else {
0037 throw_spdlog_ex("Failed to format int", static_cast<int>(ec));
0038 }
0039 }
0040 #else
0041 template <typename T>
0042 inline void append_int(T n, memory_buf_t &dest) {
0043 fmt::format_int i(n);
0044 dest.append(i.data(), i.data() + i.size());
0045 }
0046 #endif
0047
0048 template <typename T>
0049 SPDLOG_CONSTEXPR_FUNC unsigned int count_digits_fallback(T n) {
0050
0051 unsigned int count = 1;
0052 for (;;) {
0053
0054
0055
0056 if (n < 10) return count;
0057 if (n < 100) return count + 1;
0058 if (n < 1000) return count + 2;
0059 if (n < 10000) return count + 3;
0060 n /= 10000u;
0061 count += 4;
0062 }
0063 }
0064
0065 template <typename T>
0066 inline unsigned int count_digits(T n) {
0067 using count_type =
0068 typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
0069 #ifdef SPDLOG_USE_STD_FORMAT
0070 return count_digits_fallback(static_cast<count_type>(n));
0071 #else
0072 return static_cast<unsigned int>(fmt::
0073
0074
0075 #if FMT_VERSION < 70000
0076 internal
0077 #else
0078 detail
0079 #endif
0080 ::count_digits(static_cast<count_type>(n)));
0081 #endif
0082 }
0083
0084 inline void pad2(int n, memory_buf_t &dest) {
0085 if (n >= 0 && n < 100)
0086 {
0087 dest.push_back(static_cast<char>('0' + n / 10));
0088 dest.push_back(static_cast<char>('0' + n % 10));
0089 } else
0090 {
0091 fmt_lib::format_to(std::back_inserter(dest), SPDLOG_FMT_STRING("{:02}"), n);
0092 }
0093 }
0094
0095 template <typename T>
0096 inline void pad_uint(T n, unsigned int width, memory_buf_t &dest) {
0097 static_assert(std::is_unsigned<T>::value, "pad_uint must get unsigned T");
0098 for (auto digits = count_digits(n); digits < width; digits++) {
0099 dest.push_back('0');
0100 }
0101 append_int(n, dest);
0102 }
0103
0104 template <typename T>
0105 inline void pad3(T n, memory_buf_t &dest) {
0106 static_assert(std::is_unsigned<T>::value, "pad3 must get unsigned T");
0107 if (n < 1000) {
0108 dest.push_back(static_cast<char>(n / 100 + '0'));
0109 n = n % 100;
0110 dest.push_back(static_cast<char>((n / 10) + '0'));
0111 dest.push_back(static_cast<char>((n % 10) + '0'));
0112 } else {
0113 append_int(n, dest);
0114 }
0115 }
0116
0117 template <typename T>
0118 inline void pad6(T n, memory_buf_t &dest) {
0119 pad_uint(n, 6, dest);
0120 }
0121
0122 template <typename T>
0123 inline void pad9(T n, memory_buf_t &dest) {
0124 pad_uint(n, 9, dest);
0125 }
0126
0127
0128
0129
0130 template <typename ToDuration>
0131 inline ToDuration time_fraction(log_clock::time_point tp) {
0132 using std::chrono::duration_cast;
0133 using std::chrono::seconds;
0134 auto duration = tp.time_since_epoch();
0135 auto secs = duration_cast<seconds>(duration);
0136 return duration_cast<ToDuration>(duration) - duration_cast<ToDuration>(secs);
0137 }
0138
0139 }
0140 }
0141 }