Refactored log coloring

This commit is contained in:
SChernykh
2025-07-14 00:21:04 +02:00
parent a37362570d
commit a8a6b7fd02
3 changed files with 26 additions and 24 deletions

View File

@@ -24,7 +24,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
endif() endif()
if (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 15)) 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() endif()
if (DISABLE_WARNINGS) 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") 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)) 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() endif()
if (DISABLE_WARNINGS) if (DISABLE_WARNINGS)

View File

@@ -274,23 +274,17 @@ static void do_status(p2pool *m_pool, const char * /* args */)
comments.push_back("No stratum connections"); comments.push_back("No stratum connections");
} }
char buf[64] = {}; auto health_color = [](int health) {
if (health >= 8) {
log::Stream s(buf); return log::LightGreen::value;
if (node_health >= 8) {
s << log::LightGreen();
} }
else if (node_health >= 5) { if (health >= 5) {
s << log::LightYellow(); return log::LightYellow::value;
}
else {
s << log::LightRed();
} }
return log::LightRed::value;
};
s << "Node health: " << std::max(node_health, 0) << "/10"; LOGINFO(0, health_color(node_health) << "Node health: " << std::max(node_health, 0) << "/10");
LOGINFO(0, log::const_buf(buf, s.m_pos));
for (const char* comment : comments) { for (const char* comment : comments) {
LOGINFO(0, log::LightYellow() << comment); LOGINFO(0, log::LightYellow() << comment);

View File

@@ -130,7 +130,7 @@ struct Writer : public Stream
}; };
#define COLOR_ENTRY(x, s) \ #define COLOR_ENTRY(x, s) \
struct x{}; \ struct x{ static constexpr const char value[] = s; }; \
template<> struct Stream::Entry<x> { static FORCEINLINE void put(x&&, Stream* wrapper) { wrapper->writeBuf(s, sizeof(s) - 1); } }; template<> struct Stream::Entry<x> { static FORCEINLINE void put(x&&, Stream* wrapper) { wrapper->writeBuf(s, sizeof(s) - 1); } };
COLOR_ENTRY(NoColor, "\x1b[0m") COLOR_ENTRY(NoColor, "\x1b[0m")
@@ -527,11 +527,19 @@ template<> struct log::Stream::Entry<raw_ip> { static NOINLINE void put(const ra
template<> struct log::Stream::Entry<Wallet> { static NOINLINE void put(const Wallet& w, Stream* wrapper); }; template<> struct log::Stream::Entry<Wallet> { static NOINLINE void put(const Wallet& w, Stream* wrapper); };
namespace { namespace {
template<log::Severity severity> void apply_severity(log::Stream&); template<log::Severity severity>
FORCEINLINE void apply_severity(log::Stream& s)
template<> FORCEINLINE void apply_severity<log::Severity::Info>(log::Stream& s) { s << log::NoColor(); } {
template<> FORCEINLINE void apply_severity<log::Severity::Warning>(log::Stream& s) { s << log::Yellow(); } if constexpr (severity == log::Severity::Info) {
template<> FORCEINLINE void apply_severity<log::Severity::Error>(log::Stream& s) { s << log::Red(); } 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) #define CONCAT(a, b) CONCAT2(a, b)