From a8a6b7fd02b0238ff5c5121460caa36cdfbc2e7b Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Mon, 14 Jul 2025 00:21:04 +0200 Subject: [PATCH] Refactored log coloring --- cmake/flags.cmake | 4 ++-- src/console_commands.cpp | 26 ++++++++++---------------- src/log.h | 20 ++++++++++++++------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 7b8abcf..ded8c3e 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -24,7 +24,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU) endif() if (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 15)) - set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-error=cpp") + set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-cpp") endif() if (DISABLE_WARNINGS) @@ -116,7 +116,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) set(WARNING_FLAGS "-Wall -Wextra -Wno-unused-function -Wno-undefined-internal -Wno-unknown-warning-option -Wno-nan-infinity-disabled -Wunreachable-code-aggressive -Wmissing-prototypes -Wmissing-variable-declarations -Werror") if (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 20)) - set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-error=cpp") + set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-cpp") endif() if (DISABLE_WARNINGS) diff --git a/src/console_commands.cpp b/src/console_commands.cpp index 9b4386f..0ea9755 100644 --- a/src/console_commands.cpp +++ b/src/console_commands.cpp @@ -274,23 +274,17 @@ static void do_status(p2pool *m_pool, const char * /* args */) comments.push_back("No stratum connections"); } - char buf[64] = {}; + auto health_color = [](int health) { + if (health >= 8) { + return log::LightGreen::value; + } + if (health >= 5) { + return log::LightYellow::value; + } + return log::LightRed::value; + }; - log::Stream s(buf); - - if (node_health >= 8) { - s << log::LightGreen(); - } - else if (node_health >= 5) { - s << log::LightYellow(); - } - else { - s << log::LightRed(); - } - - s << "Node health: " << std::max(node_health, 0) << "/10"; - - LOGINFO(0, log::const_buf(buf, s.m_pos)); + LOGINFO(0, health_color(node_health) << "Node health: " << std::max(node_health, 0) << "/10"); for (const char* comment : comments) { LOGINFO(0, log::LightYellow() << comment); diff --git a/src/log.h b/src/log.h index a5d167e..5c4c8c5 100644 --- a/src/log.h +++ b/src/log.h @@ -130,7 +130,7 @@ struct Writer : public Stream }; #define COLOR_ENTRY(x, s) \ -struct x{}; \ +struct x{ static constexpr const char value[] = s; }; \ template<> struct Stream::Entry { static FORCEINLINE void put(x&&, Stream* wrapper) { wrapper->writeBuf(s, sizeof(s) - 1); } }; COLOR_ENTRY(NoColor, "\x1b[0m") @@ -527,11 +527,19 @@ template<> struct log::Stream::Entry { static NOINLINE void put(const ra template<> struct log::Stream::Entry { static NOINLINE void put(const Wallet& w, Stream* wrapper); }; namespace { - template void apply_severity(log::Stream&); - - template<> FORCEINLINE void apply_severity(log::Stream& s) { s << log::NoColor(); } - template<> FORCEINLINE void apply_severity(log::Stream& s) { s << log::Yellow(); } - template<> FORCEINLINE void apply_severity(log::Stream& s) { s << log::Red(); } + template + FORCEINLINE void apply_severity(log::Stream& s) + { + if constexpr (severity == log::Severity::Info) { + s << log::NoColor::value; + } + else if constexpr (severity == log::Severity::Warning) { + s << log::Yellow::value; + } + else if constexpr (severity == log::Severity::Error) { + s << log::Red::value; + } + } } #define CONCAT(a, b) CONCAT2(a, b)