use range containers

pull/2814/head
drakbar 2 years ago
parent 502c48dff1
commit 9eb32c92b9

@ -736,7 +736,7 @@ public:
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
{ {
msg.color_range_start = dest.size(); msg.color_ranges_start.push_back(dest.size());
} }
}; };
@ -749,7 +749,7 @@ public:
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
{ {
msg.color_range_end = dest.size(); msg.color_ranges_end.push_back(dest.size());
} }
}; };
@ -990,10 +990,11 @@ public:
dest.push_back('['); dest.push_back('[');
// wrap the level name with color // wrap the level name with color
msg.color_range_start = dest.size(); msg.color_ranges_start.push_back(dest.size());
// fmt_helper::append_string_view(level::to_c_str(msg.level), dest); // fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
fmt_helper::append_string_view(level::to_string_view(msg.level), dest); fmt_helper::append_string_view(level::to_string_view(msg.level), dest);
msg.color_range_end = dest.size(); msg.color_ranges_end.push_back(dest.size());
dest.push_back(']'); dest.push_back(']');
dest.push_back(' '); dest.push_back(' ');

@ -44,18 +44,31 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg
// If color is not supported in the terminal, log as is instead. // If color is not supported in the terminal, log as is instead.
std::lock_guard<mutex_t> lock(mutex_); std::lock_guard<mutex_t> lock(mutex_);
msg.color_range_start = 0; msg.color_range_start = 0;
msg.color_range_end = 0; msg.color_range_end = 0;
msg.color_ranges_start.clear();
msg.color_ranges_end.clear();
memory_buf_t formatted; memory_buf_t formatted;
formatter_->format(msg, formatted); formatter_->format(msg, formatted);
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) if (should_do_colors_ && msg.color_ranges_start.size() == msg.color_ranges_end.size())
{ {
// before color range size_t before_color_range = 0;
print_range_(formatted, 0, msg.color_range_start); for(int i = 0; i < msg.color_ranges_start.size(); i++) {
// in color range msg.color_range_start = msg.color_ranges_start[i];
print_ccode_(colors_.at(static_cast<size_t>(msg.level))); msg.color_range_end = msg.color_ranges_end[i];
print_range_(formatted, msg.color_range_start, msg.color_range_end);
print_ccode_(reset); // before color range
// after color range print_range_(formatted, before_color_range, msg.color_range_start);
// in color range
print_ccode_(colors_.at(static_cast<size_t>(msg.level)));
print_range_(formatted, msg.color_range_start, msg.color_range_end);
print_ccode_(reset);
// get new location outside color ranges
before_color_range = msg.color_range_end;
}
// after all color ranges
print_range_(formatted, msg.color_range_end, formatted.size()); print_range_(formatted, msg.color_range_end, formatted.size());
} }
else // no color else // no color

@ -58,18 +58,28 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
std::lock_guard<mutex_t> lock(mutex_); std::lock_guard<mutex_t> lock(mutex_);
msg.color_range_start = 0; msg.color_range_start = 0;
msg.color_range_end = 0; msg.color_range_end = 0;
msg.color_ranges_start.clear();
msg.color_ranges_end.clear();
memory_buf_t formatted; memory_buf_t formatted;
formatter_->format(msg, formatted); formatter_->format(msg, formatted);
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) if (should_do_colors_ && msg.color_ranges_start.size() == msg.color_ranges_end.size())
{ {
// before color range size_t before_color_range = 0;
print_range_(formatted, 0, msg.color_range_start); for(int i = 0; i < msg.color_ranges_start.size(); i++) {
// in color range msg.color_range_start = msg.color_ranges_start[i];
auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[static_cast<size_t>(msg.level)])); msg.color_range_end = msg.color_ranges_end[i];
print_range_(formatted, msg.color_range_start, msg.color_range_end);
// reset to orig colors // before color range
::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs); print_range_(formatted, before_color_range, msg.color_range_start);
// in color range
auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[static_cast<size_t>(msg.level)]));
print_range_(formatted, msg.color_range_start, msg.color_range_end);
// reset to orig colors
::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs);
// get new location outside color ranges
before_color_range = msg.color_range_end;
}
print_range_(formatted, msg.color_range_end, formatted.size()); print_range_(formatted, msg.color_range_end, formatted.size());
} }
else // print without colors if color range is invalid (or color is disabled) else // print without colors if color range is invalid (or color is disabled)

Loading…
Cancel
Save