diff --git a/example/example.cpp b/example/example.cpp index ea6e5f06..0597ec75 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -60,8 +60,8 @@ int main(int, char *[]) // Customize msg format for all messages spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v"); console->info("This an info message with custom format (and custom color range between the '%^' and '%$')"); - console->error("This an error message with custom format (and custom color range between the '%^' and '%$')"); - + console->error("This an error message with custom format (and custom color range between the '%^' and '%$')"); + // Runtime log levels spd::set_level(spd::level::info); // Set global log level to info console->debug("This message should not be displayed!"); diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index d21151db..402fa121 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -25,7 +25,7 @@ class wincolor_sink : public base_sink public: const WORD BOLD = FOREGROUND_INTENSITY; const WORD RED = FOREGROUND_RED; - const WORD GREEN = FOREGROUND_GREEN; + const WORD GREEN = FOREGROUND_GREEN; const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; @@ -60,22 +60,22 @@ public: protected: void _sink_it(const details::log_msg &msg) override { - if (msg.color_range_end > msg.color_range_start) - { - // before color range - _print_range(msg, 0, msg.color_range_start); - - // in color range - auto orig_attribs = set_console_attribs(colors_[msg.level]); - _print_range(msg, msg.color_range_start, msg.color_range_end); - ::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors - // after color range - _print_range(msg, msg.color_range_end, msg.formatted.size()); - } - else // print without colors if color range is invalid - { - _print_range(msg, 0, msg.formatted.size()); - } + if (msg.color_range_end > msg.color_range_start) + { + // before color range + _print_range(msg, 0, msg.color_range_start); + + // in color range + auto orig_attribs = set_console_attribs(colors_[msg.level]); + _print_range(msg, msg.color_range_start, msg.color_range_end); + ::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors + // after color range + _print_range(msg, msg.color_range_end, msg.formatted.size()); + } + else // print without colors if color range is invalid + { + _print_range(msg, 0, msg.formatted.size()); + } } void _flush() override @@ -100,12 +100,12 @@ private: return orig_buffer_info.wAttributes; // return orig attribs } - // print a range of formatted message to console - void _print_range(const details::log_msg &msg, size_t start, size_t end) - { - DWORD size = static_cast(end - start); - WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr); - } + // print a range of formatted message to console + void _print_range(const details::log_msg &msg, size_t start, size_t end) + { + DWORD size = static_cast(end - start); + WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr); + } }; // diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 644cb80e..c6700041 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -7,10 +7,10 @@ static std::string log_to_str(const std::string &msg, const std::shared_ptr(oss); spdlog::logger oss_logger("pattern_tester", oss_sink); oss_logger.set_level(spdlog::level::info); - if (formatter) - { - oss_logger.set_formatter(formatter); - } + if (formatter) + { + oss_logger.set_formatter(formatter); + } oss_logger.info(msg); return oss.str(); } @@ -65,60 +65,60 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") } TEST_CASE("color range test1", "[pattern_formatter]") -{ - auto formatter = std::make_shared("%^%v%$", spdlog::pattern_time_type::local, "\n"); - spdlog::details::log_msg msg; - msg.raw << "Hello"; - formatter->format(msg); - REQUIRE(msg.color_range_start == 0); - REQUIRE(msg.color_range_end == 5); - REQUIRE(log_to_str("hello", formatter) == "hello\n"); +{ + auto formatter = std::make_shared("%^%v%$", spdlog::pattern_time_type::local, "\n"); + spdlog::details::log_msg msg; + msg.raw << "Hello"; + formatter->format(msg); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 5); + REQUIRE(log_to_str("hello", formatter) == "hello\n"); } TEST_CASE("color range test2", "[pattern_formatter]") { - auto formatter = std::make_shared("%^%$", spdlog::pattern_time_type::local, "\n"); - spdlog::details::log_msg msg; - formatter->format(msg); - REQUIRE(msg.color_range_start == 0); - REQUIRE(msg.color_range_end == 0); - REQUIRE(log_to_str("", formatter) == "\n"); + auto formatter = std::make_shared("%^%$", spdlog::pattern_time_type::local, "\n"); + spdlog::details::log_msg msg; + formatter->format(msg); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 0); + REQUIRE(log_to_str("", formatter) == "\n"); } TEST_CASE("color range test3", "[pattern_formatter]") { - auto formatter = std::make_shared("%^***%$"); - spdlog::details::log_msg msg; - formatter->format(msg); - REQUIRE(msg.color_range_start == 0); - REQUIRE(msg.color_range_end == 3); + auto formatter = std::make_shared("%^***%$"); + spdlog::details::log_msg msg; + formatter->format(msg); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 3); } TEST_CASE("color range test4", "[pattern_formatter]") { - auto formatter = std::make_shared("XX%^YYY%$", spdlog::pattern_time_type::local, "\n"); - spdlog::details::log_msg msg; - msg.raw << "ignored"; - formatter->format(msg); - REQUIRE(msg.color_range_start == 2); - REQUIRE(msg.color_range_end == 5); - REQUIRE(log_to_str("ignored", formatter) == "XXYYY\n"); + auto formatter = std::make_shared("XX%^YYY%$", spdlog::pattern_time_type::local, "\n"); + spdlog::details::log_msg msg; + msg.raw << "ignored"; + formatter->format(msg); + REQUIRE(msg.color_range_start == 2); + REQUIRE(msg.color_range_end == 5); + REQUIRE(log_to_str("ignored", formatter) == "XXYYY\n"); } TEST_CASE("color range test5", "[pattern_formatter]") { - auto formatter = std::make_shared("**%^"); - spdlog::details::log_msg msg; - formatter->format(msg); - REQUIRE(msg.color_range_start == 2); - REQUIRE(msg.color_range_end == 0); + auto formatter = std::make_shared("**%^"); + spdlog::details::log_msg msg; + formatter->format(msg); + REQUIRE(msg.color_range_start == 2); + REQUIRE(msg.color_range_end == 0); } TEST_CASE("color range test6", "[pattern_formatter]") { - auto formatter = std::make_shared("**%$"); - spdlog::details::log_msg msg; - formatter->format(msg); - REQUIRE(msg.color_range_start == 0); - REQUIRE(msg.color_range_end == 2); + auto formatter = std::make_shared("**%$"); + spdlog::details::log_msg msg; + formatter->format(msg); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 2); }