Refactored log coloring
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
20
src/log.h
20
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<x> { 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<raw_ip> { static NOINLINE void put(const ra
|
||||
template<> struct log::Stream::Entry<Wallet> { static NOINLINE void put(const Wallet& w, Stream* wrapper); };
|
||||
|
||||
namespace {
|
||||
template<log::Severity severity> void apply_severity(log::Stream&);
|
||||
|
||||
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(); }
|
||||
template<> FORCEINLINE void apply_severity<log::Severity::Error>(log::Stream& s) { s << log::Red(); }
|
||||
template<log::Severity severity>
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user