fmt_helper.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include <chrono>
  5. #include <iterator>
  6. #include <spdlog/common.h>
  7. #include <spdlog/fmt/fmt.h>
  8. #include <type_traits>
  9. #ifdef SPDLOG_USE_STD_FORMAT
  10. #include <charconv>
  11. #include <limits>
  12. #endif
  13. // Some fmt helpers to efficiently format and pad ints and strings
  14. namespace spdlog {
  15. namespace details {
  16. namespace fmt_helper {
  17. inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) {
  18. auto *buf_ptr = view.data();
  19. dest.append(buf_ptr, buf_ptr + view.size());
  20. }
  21. #ifdef SPDLOG_USE_STD_FORMAT
  22. template <typename T>
  23. inline void append_int(T n, memory_buf_t &dest) {
  24. // Buffer should be large enough to hold all digits (digits10 + 1) and a sign
  25. SPDLOG_CONSTEXPR const auto BUF_SIZE = std::numeric_limits<T>::digits10 + 2;
  26. char buf[BUF_SIZE];
  27. auto [ptr, ec] = std::to_chars(buf, buf + BUF_SIZE, n, 10);
  28. if (ec == std::errc()) {
  29. dest.append(buf, ptr);
  30. } else {
  31. throw_spdlog_ex("Failed to format int", static_cast<int>(ec));
  32. }
  33. }
  34. #else
  35. template <typename T>
  36. inline void append_int(T n, memory_buf_t &dest) {
  37. fmt::format_int i(n);
  38. dest.append(i.data(), i.data() + i.size());
  39. }
  40. #endif
  41. template <typename T>
  42. SPDLOG_CONSTEXPR_FUNC unsigned int count_digits_fallback(T n) {
  43. // taken from fmt: https://github.com/fmtlib/fmt/blob/8.0.1/include/fmt/format.h#L899-L912
  44. unsigned int count = 1;
  45. for (;;) {
  46. // Integer division is slow so do it for a group of four digits instead
  47. // of for every digit. The idea comes from the talk by Alexandrescu
  48. // "Three Optimization Tips for C++". See speed-test for a comparison.
  49. if (n < 10) return count;
  50. if (n < 100) return count + 1;
  51. if (n < 1000) return count + 2;
  52. if (n < 10000) return count + 3;
  53. n /= 10000u;
  54. count += 4;
  55. }
  56. }
  57. template <typename T>
  58. inline unsigned int count_digits(T n) {
  59. using count_type =
  60. typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
  61. #ifdef SPDLOG_USE_STD_FORMAT
  62. return count_digits_fallback(static_cast<count_type>(n));
  63. #else
  64. return static_cast<unsigned int>(fmt::
  65. // fmt 7.0.0 renamed the internal namespace to detail.
  66. // See: https://github.com/fmtlib/fmt/issues/1538
  67. #if FMT_VERSION < 70000
  68. internal
  69. #else
  70. detail
  71. #endif
  72. ::count_digits(static_cast<count_type>(n)));
  73. #endif
  74. }
  75. inline void pad2(int n, memory_buf_t &dest) {
  76. if (n >= 0 && n < 100) // 0-99
  77. {
  78. dest.push_back(static_cast<char>('0' + n / 10));
  79. dest.push_back(static_cast<char>('0' + n % 10));
  80. } else // unlikely, but just in case, let fmt deal with it
  81. {
  82. fmt_lib::format_to(std::back_inserter(dest), SPDLOG_FMT_STRING("{:02}"), n);
  83. }
  84. }
  85. template <typename T>
  86. inline void pad_uint(T n, unsigned int width, memory_buf_t &dest) {
  87. static_assert(std::is_unsigned<T>::value, "pad_uint must get unsigned T");
  88. for (auto digits = count_digits(n); digits < width; digits++) {
  89. dest.push_back('0');
  90. }
  91. append_int(n, dest);
  92. }
  93. template <typename T>
  94. inline void pad3(T n, memory_buf_t &dest) {
  95. static_assert(std::is_unsigned<T>::value, "pad3 must get unsigned T");
  96. if (n < 1000) {
  97. dest.push_back(static_cast<char>(n / 100 + '0'));
  98. n = n % 100;
  99. dest.push_back(static_cast<char>((n / 10) + '0'));
  100. dest.push_back(static_cast<char>((n % 10) + '0'));
  101. } else {
  102. append_int(n, dest);
  103. }
  104. }
  105. template <typename T>
  106. inline void pad6(T n, memory_buf_t &dest) {
  107. pad_uint(n, 6, dest);
  108. }
  109. template <typename T>
  110. inline void pad9(T n, memory_buf_t &dest) {
  111. pad_uint(n, 9, dest);
  112. }
  113. // return fraction of a second of the given time_point.
  114. // e.g.
  115. // fraction<std::milliseconds>(tp) -> will return the millis part of the second
  116. template <typename ToDuration>
  117. inline ToDuration time_fraction(log_clock::time_point tp) {
  118. using std::chrono::duration_cast;
  119. using std::chrono::seconds;
  120. auto duration = tp.time_since_epoch();
  121. auto secs = duration_cast<seconds>(duration);
  122. return duration_cast<ToDuration>(duration) - duration_cast<ToDuration>(secs);
  123. }
  124. } // namespace fmt_helper
  125. } // namespace details
  126. } // namespace spdlog