bin_to_hex.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. #pragma once
  6. #include <cctype>
  7. #include <spdlog/common.h>
  8. #if defined(__has_include)
  9. #if __has_include(<version>)
  10. #include <version>
  11. #endif
  12. #endif
  13. #if __cpp_lib_span >= 202002L
  14. #include <span>
  15. #endif
  16. //
  17. // Support for logging binary data as hex
  18. // format flags, any combination of the following:
  19. // {:X} - print in uppercase.
  20. // {:s} - don't separate each byte with space.
  21. // {:p} - don't print the position on each line start.
  22. // {:n} - don't split the output to lines.
  23. // {:a} - show ASCII if :n is not set
  24. //
  25. // Examples:
  26. //
  27. // std::vector<char> v(200, 0x0b);
  28. // logger->info("Some buffer {}", spdlog::to_hex(v));
  29. // char buf[128];
  30. // logger->info("Some buffer {:X}", spdlog::to_hex(std::begin(buf), std::end(buf)));
  31. // logger->info("Some buffer {:X}", spdlog::to_hex(std::begin(buf), std::end(buf), 16));
  32. namespace spdlog {
  33. namespace details {
  34. template <typename It>
  35. class dump_info {
  36. public:
  37. dump_info(It range_begin, It range_end, size_t size_per_line)
  38. : begin_(range_begin),
  39. end_(range_end),
  40. size_per_line_(size_per_line) {}
  41. // do not use begin() and end() to avoid collision with fmt/ranges
  42. It get_begin() const { return begin_; }
  43. It get_end() const { return end_; }
  44. size_t size_per_line() const { return size_per_line_; }
  45. private:
  46. It begin_, end_;
  47. size_t size_per_line_;
  48. };
  49. } // namespace details
  50. // create a dump_info that wraps the given container
  51. template <typename Container>
  52. inline details::dump_info<typename Container::const_iterator> to_hex(const Container &container,
  53. size_t size_per_line = 32) {
  54. static_assert(sizeof(typename Container::value_type) == 1,
  55. "sizeof(Container::value_type) != 1");
  56. using Iter = typename Container::const_iterator;
  57. return details::dump_info<Iter>(std::begin(container), std::end(container), size_per_line);
  58. }
  59. #if __cpp_lib_span >= 202002L
  60. template <typename Value, size_t Extent>
  61. inline details::dump_info<typename std::span<Value, Extent>::iterator> to_hex(
  62. const std::span<Value, Extent> &container, size_t size_per_line = 32) {
  63. using Container = std::span<Value, Extent>;
  64. static_assert(sizeof(typename Container::value_type) == 1,
  65. "sizeof(Container::value_type) != 1");
  66. using Iter = typename Container::iterator;
  67. return details::dump_info<Iter>(std::begin(container), std::end(container), size_per_line);
  68. }
  69. #endif
  70. // create dump_info from ranges
  71. template <typename It>
  72. inline details::dump_info<It> to_hex(const It range_begin,
  73. const It range_end,
  74. size_t size_per_line = 32) {
  75. return details::dump_info<It>(range_begin, range_end, size_per_line);
  76. }
  77. } // namespace spdlog
  78. namespace
  79. #ifdef SPDLOG_USE_STD_FORMAT
  80. std
  81. #else
  82. fmt
  83. #endif
  84. {
  85. template <typename T>
  86. struct formatter<spdlog::details::dump_info<T>, char> {
  87. const char delimiter = ' ';
  88. bool put_newlines = true;
  89. bool put_delimiters = true;
  90. bool use_uppercase = false;
  91. bool put_positions = true; // position on start of each line
  92. bool show_ascii = false;
  93. // parse the format string flags
  94. template <typename ParseContext>
  95. SPDLOG_CONSTEXPR_FUNC auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
  96. auto it = ctx.begin();
  97. while (it != ctx.end() && *it != '}') {
  98. switch (*it) {
  99. case 'X':
  100. use_uppercase = true;
  101. break;
  102. case 's':
  103. put_delimiters = false;
  104. break;
  105. case 'p':
  106. put_positions = false;
  107. break;
  108. case 'n':
  109. put_newlines = false;
  110. show_ascii = false;
  111. break;
  112. case 'a':
  113. if (put_newlines) {
  114. show_ascii = true;
  115. }
  116. break;
  117. }
  118. ++it;
  119. }
  120. return it;
  121. }
  122. // format the given bytes range as hex
  123. template <typename FormatContext, typename Container>
  124. auto format(const spdlog::details::dump_info<Container> &the_range, FormatContext &ctx) const
  125. -> decltype(ctx.out()) {
  126. SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF";
  127. SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef";
  128. const char *hex_chars = use_uppercase ? hex_upper : hex_lower;
  129. #if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION < 60000
  130. auto inserter = ctx.begin();
  131. #else
  132. auto inserter = ctx.out();
  133. #endif
  134. int size_per_line = static_cast<int>(the_range.size_per_line());
  135. auto start_of_line = the_range.get_begin();
  136. for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) {
  137. auto ch = static_cast<unsigned char>(*i);
  138. if (put_newlines &&
  139. (i == the_range.get_begin() || i - start_of_line >= size_per_line)) {
  140. if (show_ascii && i != the_range.get_begin()) {
  141. *inserter++ = delimiter;
  142. *inserter++ = delimiter;
  143. for (auto j = start_of_line; j < i; j++) {
  144. auto pc = static_cast<unsigned char>(*j);
  145. *inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
  146. }
  147. }
  148. put_newline(inserter, static_cast<size_t>(i - the_range.get_begin()));
  149. // put first byte without delimiter in front of it
  150. *inserter++ = hex_chars[(ch >> 4) & 0x0f];
  151. *inserter++ = hex_chars[ch & 0x0f];
  152. start_of_line = i;
  153. continue;
  154. }
  155. if (put_delimiters && i != the_range.get_begin()) {
  156. *inserter++ = delimiter;
  157. }
  158. *inserter++ = hex_chars[(ch >> 4) & 0x0f];
  159. *inserter++ = hex_chars[ch & 0x0f];
  160. }
  161. if (show_ascii) // add ascii to last line
  162. {
  163. if (the_range.get_end() - the_range.get_begin() > size_per_line) {
  164. auto blank_num = size_per_line - (the_range.get_end() - start_of_line);
  165. while (blank_num-- > 0) {
  166. *inserter++ = delimiter;
  167. *inserter++ = delimiter;
  168. if (put_delimiters) {
  169. *inserter++ = delimiter;
  170. }
  171. }
  172. }
  173. *inserter++ = delimiter;
  174. *inserter++ = delimiter;
  175. for (auto j = start_of_line; j != the_range.get_end(); j++) {
  176. auto pc = static_cast<unsigned char>(*j);
  177. *inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
  178. }
  179. }
  180. return inserter;
  181. }
  182. // put newline(and position header)
  183. template <typename It>
  184. void put_newline(It inserter, std::size_t pos) const {
  185. #ifdef _WIN32
  186. *inserter++ = '\r';
  187. #endif
  188. *inserter++ = '\n';
  189. if (put_positions) {
  190. spdlog::fmt_lib::format_to(inserter, SPDLOG_FMT_STRING("{:04X}: "), pos);
  191. }
  192. }
  193. };
  194. } // namespace std