diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 01afbe6f..ad8f09ed 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -736,7 +736,7 @@ public: 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 { - msg.color_range_end = dest.size(); + msg.color_ranges_end.push_back(dest.size()); } }; @@ -990,10 +990,11 @@ public: dest.push_back('['); // 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_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(' '); diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index c924fc5b..39468cb7 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -44,18 +44,31 @@ SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg // If color is not supported in the terminal, log as is instead. std::lock_guard lock(mutex_); 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; 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 - print_range_(formatted, 0, msg.color_range_start); - // in color range - print_ccode_(colors_.at(static_cast(msg.level))); - print_range_(formatted, msg.color_range_start, msg.color_range_end); - print_ccode_(reset); - // after color range + size_t before_color_range = 0; + for(int i = 0; i < msg.color_ranges_start.size(); i++) { + msg.color_range_start = msg.color_ranges_start[i]; + msg.color_range_end = msg.color_ranges_end[i]; + + // before color range + print_range_(formatted, before_color_range, msg.color_range_start); + + // in color range + print_ccode_(colors_.at(static_cast(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()); } else // no color diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 8311929e..8deee2d9 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -58,18 +58,28 @@ void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) std::lock_guard lock(mutex_); 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; 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 - print_range_(formatted, 0, msg.color_range_start); - // in color range - auto orig_attribs = static_cast(set_foreground_color_(colors_[static_cast(msg.level)])); - print_range_(formatted, msg.color_range_start, msg.color_range_end); - // reset to orig colors - ::SetConsoleTextAttribute(static_cast(out_handle_), orig_attribs); + size_t before_color_range = 0; + for(int i = 0; i < msg.color_ranges_start.size(); i++) { + msg.color_range_start = msg.color_ranges_start[i]; + msg.color_range_end = msg.color_ranges_end[i]; + + // before color range + print_range_(formatted, before_color_range, msg.color_range_start); + // in color range + auto orig_attribs = static_cast(set_foreground_color_(colors_[static_cast(msg.level)])); + print_range_(formatted, msg.color_range_start, msg.color_range_end); + // reset to orig colors + ::SetConsoleTextAttribute(static_cast(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()); } else // print without colors if color range is invalid (or color is disabled)