Skip to content

Commit

Permalink
Merge pull request #2195 from patrickroocks/v1.x-fix-ranges-and-to-hex
Browse files Browse the repository at this point in the history
Fix usage of ranges and to_hex in the same compile unit
  • Loading branch information
gabime committed Dec 1, 2021
2 parents cabbe65 + f304ca3 commit 0c611af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
11 changes: 11 additions & 0 deletions example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<int> 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()
Expand Down
21 changes: 11 additions & 10 deletions include/spdlog/fmt/bin_to_hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ class dump_info
, size_per_line_(size_per_line)
{}

It begin() const
// do not use begin() and end() to avoid collision with fmt/ranges
It get_begin() const
{
return begin_;
}
It end() const
It get_end() const
{
return end_;
}
Expand Down Expand Up @@ -144,14 +145,14 @@ struct formatter<spdlog::details::dump_info<T>, char>
#endif

int size_per_line = static_cast<int>(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.get_begin();
for (auto i = the_range.get_begin(); i != the_range.get_end(); i++)
{
auto ch = static_cast<unsigned char>(*i);

if (put_newlines && (i == the_range.begin() || 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.begin())
if (show_ascii && i != the_range.get_begin())
{
*inserter++ = delimiter;
*inserter++ = delimiter;
Expand All @@ -162,7 +163,7 @@ struct formatter<spdlog::details::dump_info<T>, char>
}
}

put_newline(inserter, static_cast<size_t>(i - the_range.begin()));
put_newline(inserter, static_cast<size_t>(i - the_range.get_begin()));

// put first byte without delimiter in front of it
*inserter++ = hex_chars[(ch >> 4) & 0x0f];
Expand All @@ -181,9 +182,9 @@ struct formatter<spdlog::details::dump_info<T>, char>
}
if (show_ascii) // add ascii to last line
{
if (the_range.end() - the_range.begin() > size_per_line)
if (the_range.get_end() - the_range.get_begin() > size_per_line)
{
auto blank_num = size_per_line - (the_range.end() - start_of_line);
auto blank_num = size_per_line - (the_range.get_end() - start_of_line);
while (blank_num-- > 0)
{
*inserter++ = delimiter;
Expand All @@ -196,7 +197,7 @@ struct formatter<spdlog::details::dump_info<T>, 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.get_end(); j++)
{
auto pc = static_cast<unsigned char>(*j);
*inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
Expand Down

0 comments on commit 0c611af

Please sign in to comment.