From d1e8669d8ae43d3f3a63e72621f9d75e2ccc5ed3 Mon Sep 17 00:00:00 2001 From: drakbar Date: Sun, 16 Jul 2023 18:28:20 -0500 Subject: [PATCH] revert old commits --- example/example.cpp | 2 +- include/spdlog/details/log_msg.h | 3 --- include/spdlog/pattern_formatter-inl.h | 8 +++---- include/spdlog/sinks/ansicolor_sink-inl.h | 26 ++++++----------------- include/spdlog/sinks/wincolor_sink-inl.h | 26 +++++++---------------- tests/test_pattern_formatter.cpp | 24 ++++++++++----------- 6 files changed, 32 insertions(+), 57 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index c3592c03..d6609ed5 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -52,7 +52,7 @@ int main(int, char *[]) spdlog::debug("This message should be displayed.."); // Customize msg format for all loggers - spdlog::set_pattern("[%^%H:%M:%S %z%$] [%^%L%$] [thread %t] %^%v%$"); + spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v"); spdlog::info("This an info message with custom format"); spdlog::set_pattern("%+"); // back to default format spdlog::set_level(spdlog::level::info); diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index eb284760..fed51abd 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -5,7 +5,6 @@ #include #include -#include namespace spdlog { namespace details { @@ -26,8 +25,6 @@ struct SPDLOG_API log_msg // wrapping the formatted text with color (updated by pattern_formatter). mutable size_t color_range_start{0}; mutable size_t color_range_end{0}; - mutable std::vectorcolor_ranges_start = {}; - mutable std::vectorcolor_ranges_end = {}; source_loc source; string_view_t payload; diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index ad8f09ed..796eda99 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_ranges_start.push_back(dest.size()); + msg.color_range_start = dest.size(); } }; @@ -749,7 +749,7 @@ public: void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - msg.color_ranges_end.push_back(dest.size()); + msg.color_range_end = dest.size(); } }; @@ -990,11 +990,11 @@ public: dest.push_back('['); // wrap the level name with color - msg.color_ranges_start.push_back(dest.size()); + msg.color_range_start = 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_ranges_end.push_back(dest.size()); + msg.color_range_end = 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 39468cb7..de253461 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -45,29 +45,17 @@ SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg std::lock_guard lock(mutex_); msg.color_range_start = 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_ranges_start.size() == msg.color_ranges_end.size()) + if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { - 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; - } + // 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 all color ranges print_range_(formatted, msg.color_range_end, formatted.size()); } diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 8deee2d9..12aa69c3 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -59,27 +59,17 @@ 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_ranges_start.clear(); - msg.color_ranges_end.clear(); memory_buf_t formatted; formatter_->format(msg, formatted); - if (should_do_colors_ && msg.color_ranges_start.size() == msg.color_ranges_end.size()) + if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { - 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; - } + // 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); print_range_(formatted, msg.color_range_end, formatted.size()); } else // print without colors if color range is invalid (or color is disabled) diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 106cc35d..c81ec315 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -70,8 +70,8 @@ TEST_CASE("color range test1", "[pattern_formatter]") std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, spdlog::string_view_t(buf.data(), buf.size())); formatter->format(msg, formatted); - REQUIRE(msg.color_ranges_start[0] == 0); - REQUIRE(msg.color_ranges_end[0] == 5); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 5); REQUIRE(log_to_str("hello", "%^%v%$", spdlog::pattern_time_type::local, "\n") == "hello\n"); } @@ -82,8 +82,8 @@ TEST_CASE("color range test2", "[pattern_formatter]") spdlog::details::log_msg msg(logger_name, spdlog::level::info, ""); memory_buf_t formatted; formatter->format(msg, formatted); - REQUIRE(msg.color_ranges_start[0] == 0); - REQUIRE(msg.color_ranges_end[0] == 0); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 0); REQUIRE(log_to_str("", "%^%$", spdlog::pattern_time_type::local, "\n") == "\n"); } @@ -94,8 +94,8 @@ TEST_CASE("color range test3", "[pattern_formatter]") spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored"); memory_buf_t formatted; formatter->format(msg, formatted); - REQUIRE(msg.color_ranges_start[0] == 0); - REQUIRE(msg.color_ranges_end[0] == 3); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 3); } TEST_CASE("color range test4", "[pattern_formatter]") @@ -106,8 +106,8 @@ TEST_CASE("color range test4", "[pattern_formatter]") memory_buf_t formatted; formatter->format(msg, formatted); - REQUIRE(msg.color_ranges_start[0] == 2); - REQUIRE(msg.color_ranges_end[0] == 5); + REQUIRE(msg.color_range_start == 2); + REQUIRE(msg.color_range_end == 5); REQUIRE(log_to_str("ignored", "XX%^YYY%$", spdlog::pattern_time_type::local, "\n") == "XXYYY\n"); } @@ -118,8 +118,8 @@ TEST_CASE("color range test5", "[pattern_formatter]") spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored"); memory_buf_t formatted; formatter->format(msg, formatted); - REQUIRE(msg.color_ranges_start[0] == 2); - REQUIRE(msg.color_ranges_end.size() == 0); + REQUIRE(msg.color_range_start[0] == 2); + REQUIRE(msg.color_range_end == 0); } TEST_CASE("color range test6", "[pattern_formatter]") @@ -129,8 +129,8 @@ TEST_CASE("color range test6", "[pattern_formatter]") spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored"); memory_buf_t formatted; formatter->format(msg, formatted); - REQUIRE(msg.color_ranges_start.size() == 0); - REQUIRE(msg.color_ranges_end[0] == 2); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end[0] == 2); } //