From d93cea97ec9d1b3a26486c7530dbaaba59852aed Mon Sep 17 00:00:00 2001 From: "Roocks Patrick (MTN PTT / External)" Date: Wed, 1 Dec 2021 15:37:48 +0100 Subject: [PATCH 1/2] Fix usage of ranges and to_hex in the same compile unit When trying to use spdlog/fmt/bin_to_hex.h in the same compile unit as spdlog/fmt/bundled/ranges.h you got a compile error because there was a multiple definitions for iterable classes. This fix renames the begin() and end() getters in dump_info into getBegin()/getEnd() in order to avoid this collision. Added an example of ranges in example.cpp to show that it actually works (an to_hex example was already there) --- example/example.cpp | 11 +++++++++++ include/spdlog/fmt/bin_to_hex.h | 21 +++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 3e33e284..87b00518 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -14,6 +14,7 @@ void rotating_example(); void daily_example(); void async_example(); void binary_example(); +void vector_example(); void stopwatch_example(); void trace_example(); void multi_sink_example(); @@ -73,6 +74,7 @@ int main(int, char *[]) daily_example(); async_example(); binary_example(); + vector_example(); multi_sink_example(); user_defined_example(); err_handler_example(); @@ -188,6 +190,15 @@ void binary_example() // logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20)); } +// Log a vector of numbers + +#include "spdlog/fmt/bundled/ranges.h" +void vector_example() +{ + std::vector vec = {1, 2, 3}; + spdlog::info("Vector example: {}", vec); +} + // Compile time log levels. // define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE) void trace_example() diff --git a/include/spdlog/fmt/bin_to_hex.h b/include/spdlog/fmt/bin_to_hex.h index c6ed08d8..26785d81 100644 --- a/include/spdlog/fmt/bin_to_hex.h +++ b/include/spdlog/fmt/bin_to_hex.h @@ -39,11 +39,12 @@ public: , size_per_line_(size_per_line) {} - It begin() const + // do not use begin() and end() to avoid collision with fmt/ranges + It getBegin() const { return begin_; } - It end() const + It getEnd() const { return end_; } @@ -144,14 +145,14 @@ struct formatter, char> #endif int size_per_line = static_cast(the_range.size_per_line()); - auto start_of_line = the_range.begin(); - for (auto i = the_range.begin(); i != the_range.end(); i++) + auto start_of_line = the_range.getBegin(); + for (auto i = the_range.getBegin(); i != the_range.getEnd(); i++) { auto ch = static_cast(*i); - if (put_newlines && (i == the_range.begin() || i - start_of_line >= size_per_line)) + if (put_newlines && (i == the_range.getBegin() || i - start_of_line >= size_per_line)) { - if (show_ascii && i != the_range.begin()) + if (show_ascii && i != the_range.getBegin()) { *inserter++ = delimiter; *inserter++ = delimiter; @@ -162,7 +163,7 @@ struct formatter, char> } } - put_newline(inserter, static_cast(i - the_range.begin())); + put_newline(inserter, static_cast(i - the_range.getBegin())); // put first byte without delimiter in front of it *inserter++ = hex_chars[(ch >> 4) & 0x0f]; @@ -181,9 +182,9 @@ struct formatter, char> } if (show_ascii) // add ascii to last line { - if (the_range.end() - the_range.begin() > size_per_line) + if (the_range.getEnd() - the_range.getBegin() > size_per_line) { - auto blank_num = size_per_line - (the_range.end() - start_of_line); + auto blank_num = size_per_line - (the_range.getEnd() - start_of_line); while (blank_num-- > 0) { *inserter++ = delimiter; @@ -196,7 +197,7 @@ struct formatter, char> } *inserter++ = delimiter; *inserter++ = delimiter; - for (auto j = start_of_line; j != the_range.end(); j++) + for (auto j = start_of_line; j != the_range.getEnd(); j++) { auto pc = static_cast(*j); *inserter++ = std::isprint(pc) ? static_cast(*j) : '.'; From f304ca3dafac5e3135c5d1323b49a9693d1a9c3e Mon Sep 17 00:00:00 2001 From: "Roocks Patrick (MTN PTT / External)" Date: Wed, 1 Dec 2021 16:37:29 +0100 Subject: [PATCH 2/2] code style fixes --- include/spdlog/fmt/bin_to_hex.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/spdlog/fmt/bin_to_hex.h b/include/spdlog/fmt/bin_to_hex.h index 26785d81..14908310 100644 --- a/include/spdlog/fmt/bin_to_hex.h +++ b/include/spdlog/fmt/bin_to_hex.h @@ -40,11 +40,11 @@ public: {} // do not use begin() and end() to avoid collision with fmt/ranges - It getBegin() const + It get_begin() const { return begin_; } - It getEnd() const + It get_end() const { return end_; } @@ -145,14 +145,14 @@ struct formatter, char> #endif int size_per_line = static_cast(the_range.size_per_line()); - auto start_of_line = the_range.getBegin(); - for (auto i = the_range.getBegin(); i != the_range.getEnd(); i++) + auto start_of_line = the_range.get_begin(); + for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) { auto ch = static_cast(*i); - if (put_newlines && (i == the_range.getBegin() || i - start_of_line >= size_per_line)) + if (put_newlines && (i == the_range.get_begin() || i - start_of_line >= size_per_line)) { - if (show_ascii && i != the_range.getBegin()) + if (show_ascii && i != the_range.get_begin()) { *inserter++ = delimiter; *inserter++ = delimiter; @@ -163,7 +163,7 @@ struct formatter, char> } } - put_newline(inserter, static_cast(i - the_range.getBegin())); + put_newline(inserter, static_cast(i - the_range.get_begin())); // put first byte without delimiter in front of it *inserter++ = hex_chars[(ch >> 4) & 0x0f]; @@ -182,9 +182,9 @@ struct formatter, char> } if (show_ascii) // add ascii to last line { - if (the_range.getEnd() - the_range.getBegin() > size_per_line) + if (the_range.get_end() - the_range.get_begin() > size_per_line) { - auto blank_num = size_per_line - (the_range.getEnd() - start_of_line); + auto blank_num = size_per_line - (the_range.get_end() - start_of_line); while (blank_num-- > 0) { *inserter++ = delimiter; @@ -197,7 +197,7 @@ struct formatter, char> } *inserter++ = delimiter; *inserter++ = delimiter; - for (auto j = start_of_line; j != the_range.getEnd(); j++) + for (auto j = start_of_line; j != the_range.get_end(); j++) { auto pc = static_cast(*j); *inserter++ = std::isprint(pc) ? static_cast(*j) : '.';