From 1a2768aed40b4fa427a3467ffd84aa86e02954f2 Mon Sep 17 00:00:00 2001 From: Tony Butler Date: Sat, 23 Mar 2019 11:12:38 -0600 Subject: [PATCH] Refactor and simplify color handling (generate always, strip once instead of isColor() checking everywhere and duplication) --- src/App.cpp | 18 ++++----- src/Summary.cpp | 65 +++++++++++------------------- src/base/net/Pools.cpp | 26 ++++-------- src/common/config/CommonConfig.cpp | 9 ++--- src/common/log/BasicLog.cpp | 25 ++++++++++-- src/common/log/BasicLog.h | 1 + src/common/log/ConsoleLog.cpp | 29 ++++++++++--- src/common/log/ConsoleLog.h | 1 + src/common/log/FileLog.cpp | 33 ++++++++++----- src/common/log/FileLog.h | 3 +- src/common/log/Log.cpp | 24 ++++------- src/common/log/Log.h | 59 ++++++++++++++++++++------- src/common/log/SysLog.cpp | 20 ++++++++- src/common/log/SysLog.h | 4 ++ src/net/Network.cpp | 17 +++----- src/workers/Hashrate.cpp | 3 +- src/workers/Workers.cpp | 50 ++++++++++++++--------- 17 files changed, 227 insertions(+), 160 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index 505a3a98..43033c33 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -93,19 +93,19 @@ int xmrig::App::exec() background(); if (!CryptoNight::init(m_controller->config()->algorithm().algo())) { - LOG_ERR("\"%s\" hash self-test failed.", m_controller->config()->algorithm().name()); + LOG_ERR(RED_BOLD("\"%s\" hash self-test failed."), m_controller->config()->algorithm().name()); return 1; } Summary::print(m_controller); if (m_controller->config()->threads().empty()) { - LOG_ERR("No CUDA device found!"); + LOG_ERR(RED_BOLD("No CUDA device found!")); return 1; } if (m_controller->config()->isDryRun()) { - LOG_NOTICE("OK"); + LOG_NOTICE(GREEN_BOLD("OK")); return 0; } @@ -149,7 +149,7 @@ void xmrig::App::onConsoleCommand(char command) case 'p': case 'P': if (Workers::isEnabled()) { - LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;33mpaused\x1B[0m, press \x1B[01;35mr\x1B[0m to resume" : "paused, press 'r' to resume"); + LOG_INFO(YELLOW("paused") ", press '" MAGENTA_BOLD("r") "' to resume"); Workers::setEnabled(false); } break; @@ -157,7 +157,7 @@ void xmrig::App::onConsoleCommand(char command) case 'r': case 'R': if (!Workers::isEnabled()) { - LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;32mresumed" : "resumed"); + LOG_INFO(GREEN_BOLD("resumed")); Workers::setEnabled(true); } break; @@ -168,7 +168,7 @@ void xmrig::App::onConsoleCommand(char command) break; case 3: - LOG_WARN("Ctrl+C received, exiting"); + LOG_WARN("Ctrl+C received, exiting" CLEAR); close(); break; @@ -183,15 +183,15 @@ void xmrig::App::onSignal(int signum) switch (signum) { case SIGHUP: - LOG_WARN("SIGHUP received, exiting"); + LOG_WARN("SIGHUP received, exiting" CLEAR); break; case SIGTERM: - LOG_WARN("SIGTERM received, exiting"); + LOG_WARN("SIGTERM received, exiting" CLEAR); break; case SIGINT: - LOG_WARN("SIGINT received, exiting"); + LOG_WARN("SIGINT received, exiting" CLEAR); break; default: diff --git a/src/Summary.cpp b/src/Summary.cpp index 769cd314..523ed4b7 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -40,28 +40,21 @@ static void print_cpu(xmrig::Config *config) { - if (config->isColors()) { - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") WHITE_BOLD("%s %sx64 %sAES"), - "CPU", - xmrig::Cpu::info()->brand(), - xmrig::Cpu::info()->isX64() ? "\x1B[1;32m" : "\x1B[1;31m-", - xmrig::Cpu::info()->hasAES() ? "\x1B[1;32m" : "\x1B[1;31m-"); - } - else { - Log::i()->text(" * %-13s%s %sx64 %sAES", "CPU", xmrig::Cpu::info()->brand(), xmrig::Cpu::info()->isX64() ? "" : "-", xmrig::Cpu::info()->hasAES() ? "" : "-"); - } + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s") " %s %s", + "CPU", + xmrig::Cpu::info()->brand(), + xmrig::Cpu::info()->isX64() ? GREEN_BOLD("x64") : RED_BOLD("-x64"), + xmrig::Cpu::info()->hasAES() ? GREEN_BOLD("AES") : RED_BOLD("-AES")); } static void print_algo(xmrig::Config *config) { - Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s, %sdonate=%d%%") - : " * %-13s%s, %sdonate=%d%%", + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s, %sdonate=%d%%"), "ALGO", config->algorithm().name(), - config->isColors() && config->donateLevel() == 0 ? "\x1B[1;31m" : "", - config->donateLevel() - ); + config->donateLevel() == 0 ? RED_BOLD_S : "", + config->donateLevel()); } @@ -70,24 +63,18 @@ static void print_gpu(xmrig::Config *config) constexpr size_t byteToMiB = 1024u * 1024u; for (const xmrig::IThread *t : config->threads()) { auto thread = static_cast(t); - Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("GPU #%-8zu") YELLOW("PCI:%04x:%02x:%02x") GREEN(" %s @ %d/%d MHz") " \x1B[1;30m%dx%d %dx%d arch:%d%d SMX:%d MEM:%zu/%zu MiB" - : " * GPU #%-8zuPCI:%04x:%02x:%02x %s @ %d/%d MHz %dx%d %dx%d arch:%d%d SMX:%d MEM:%zu/%zu MiB", + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("GPU #%-8zu") + YELLOW("PCI:%04x:%02x:%02x") + GREEN(" %s @ %d/%d MHz") " " + BLACK_BOLD("%dx%d %dx%d arch:%d%d SMX:%d MEM:%zu/%zu MiB"), thread->index(), - thread->pciDomainID(), - thread->pciBusID(), - thread->pciDeviceID(), - thread->name(), - thread->clockRate() / 1000, - thread->memoryClockRate() / 1000, - thread->threads(), - thread->blocks(), - thread->bfactor(), - thread->bsleep(), - thread->arch()[0], - thread->arch()[1], + thread->pciDomainID(), thread->pciBusID(), thread->pciDeviceID(), + thread->name(), thread->clockRate() / 1000, thread->memoryClockRate() / 1000, + thread->threads(), thread->blocks(), + thread->bfactor(), thread->bsleep(), + thread->arch()[0], thread->arch()[1], thread->smx(), - thread->memoryFree() / byteToMiB, - thread->memoryTotal() / byteToMiB + thread->memoryFree() / byteToMiB, thread->memoryTotal() / byteToMiB ); } } @@ -95,15 +82,11 @@ static void print_gpu(xmrig::Config *config) static void print_commands(xmrig::Config *config) { - if (config->isColors()) { - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("COMMANDS ") MAGENTA_BOLD("h") WHITE_BOLD("ashrate, ") - WHITE_BOLD("h") MAGENTA_BOLD("e") WHITE_BOLD("alth, ") - MAGENTA_BOLD("p") WHITE_BOLD("ause, ") - MAGENTA_BOLD("r") WHITE_BOLD("esume")); - } - else { - Log::i()->text(" * COMMANDS 'h' hashrate, 'e' health, 'p' pause, 'r' resume"); - } + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("COMMANDS '") + MAGENTA_BOLD("h") WHITE_BOLD("' hashrate, '") + MAGENTA_BOLD("e") WHITE_BOLD("' health, '") + MAGENTA_BOLD("p") WHITE_BOLD("' pause, '") + MAGENTA_BOLD("r") WHITE_BOLD("' resume")); } @@ -119,5 +102,3 @@ void Summary::print(xmrig::Controller *controller) print_commands(controller->config()); } - - diff --git a/src/base/net/Pools.cpp b/src/base/net/Pools.cpp index e3b86aca..b5c0098b 100644 --- a/src/base/net/Pools.cpp +++ b/src/base/net/Pools.cpp @@ -158,25 +158,13 @@ void xmrig::Pools::print() const { size_t i = 1; for (const Pool &pool : m_data) { - if (Log::colors) { - const int color = pool.isEnabled() ? (pool.isTLS() ? 32 : 36) : 31; - - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "\x1B[1;%dm%s\x1B[0m variant " WHITE_BOLD("%s"), - i, - color, - pool.url(), - pool.algorithm().variantName() - ); - } - else { - Log::i()->text(" * POOL #%-7zu%s%s variant=%s %s", - i, - pool.isEnabled() ? "" : "-", - pool.url(), - pool.algorithm().variantName(), - pool.isTLS() ? "TLS" : "" - ); - } + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "%s%s" CLEAR " variant=" WHITE_BOLD("%s") "%s", + i, + pool.isEnabled() ? (pool.isTLS() ? GREEN_BOLD_S : CYAN_BOLD_S) : RED_BOLD_S "-", + pool.url(), + pool.algorithm().variantName(), + pool.isTLS() ? " " GREEN_BOLD_S "TLS" : "" + ); i++; } diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 36d156a3..0fa2d6d8 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -95,8 +95,7 @@ void xmrig::CommonConfig::printAPI() return; } - Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN("%s:") CYAN_BOLD("%d") - : " * %-13s%s:%d", + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN("%s:") CYAN_BOLD("%d"), "API BIND", isApiIPv6() ? "[::]" : "0.0.0.0", apiPort()); # endif } @@ -120,8 +119,7 @@ void xmrig::CommonConfig::printVersions() snprintf(buf, sizeof buf, "MSVC/%d", MSVC_VERSION); # endif - Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%s/%s") WHITE_BOLD(" %s") - : " * %-13s%s/%s %s", + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%s/%s") WHITE_BOLD(" %s"), "ABOUT", APP_NAME, APP_VERSION, buf); # if defined(XMRIG_AMD_PROJECT) @@ -159,8 +157,7 @@ void xmrig::CommonConfig::printVersions() length += snprintf(buf + length, (sizeof buf) - length, "microhttpd/%s ", MHD_get_version()); # endif - Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13slibuv/%s %s") - : " * %-13slibuv/%s %s", + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13slibuv/%s %s"), "LIBS", uv_version_string(), buf); } diff --git a/src/common/log/BasicLog.cpp b/src/common/log/BasicLog.cpp index cb4defcd..89ff452e 100644 --- a/src/common/log/BasicLog.cpp +++ b/src/common/log/BasicLog.cpp @@ -43,6 +43,19 @@ BasicLog::BasicLog() } +void BasicLog::stripColor() +{ + // strip ANSI CSI sequences + std::string txt(m_fmt); + std::string::size_type i,j; + while ((i = txt.find(CSI)) != std::string::npos){ + j = txt.find('m',i); + txt.erase(i, j-i+1); + } + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s", txt.c_str()); +} + + void BasicLog::message(Level level, const char* fmt, va_list args) { time_t now = time(nullptr); @@ -61,9 +74,9 @@ void BasicLog::message(Level level, const char* fmt, va_list args) stime.tm_hour, stime.tm_min, stime.tm_sec, - Log::colorByLevel(level, false), + Log::colorByLevel(level), fmt, - Log::endl(false) + Log::endl() ); print(args); @@ -72,7 +85,7 @@ void BasicLog::message(Level level, const char* fmt, va_list args) void BasicLog::text(const char* fmt, va_list args) { - snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(false)); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl()); print(args); } @@ -80,7 +93,11 @@ void BasicLog::text(const char* fmt, va_list args) void BasicLog::print(va_list args) { - if (vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args) <= 0) { + char buf[kBufferSize]; + vsnprintf(buf, sizeof(buf) - 1, m_fmt, args); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s", buf); + BasicLog::stripColor(); + if (snprintf(m_buf, sizeof(m_buf) - 1, "%s", m_fmt) <= 0) { return; } diff --git a/src/common/log/BasicLog.h b/src/common/log/BasicLog.h index 523538e9..adc07580 100644 --- a/src/common/log/BasicLog.h +++ b/src/common/log/BasicLog.h @@ -47,6 +47,7 @@ class BasicLog : public ILogBackend private: bool isWritable() const; void print(va_list args); + void stripColor(); char m_buf[kBufferSize]; char m_fmt[256]; diff --git a/src/common/log/ConsoleLog.cpp b/src/common/log/ConsoleLog.cpp index b10812a6..317fbe3c 100644 --- a/src/common/log/ConsoleLog.cpp +++ b/src/common/log/ConsoleLog.cpp @@ -66,6 +66,21 @@ ConsoleLog::ConsoleLog(xmrig::Controller *controller) : } +void ConsoleLog::stripColor() +{ + if (! m_controller->config()->isColors()) { + // strip ANSI CSI sequences + std::string txt(m_fmt); + std::string::size_type i, j; + while ((i = txt.find(CSI)) != std::string::npos) { + j = txt.find('m', i); + txt.erase(i, j-i+1); + } + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s", txt.c_str()); + } +} + + void ConsoleLog::message(Level level, const char* fmt, va_list args) { time_t now = time(nullptr); @@ -77,8 +92,6 @@ void ConsoleLog::message(Level level, const char* fmt, va_list args) localtime_r(&now, &stime); # endif - const bool isColors = m_controller->config()->isColors(); - snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", stime.tm_year + 1900, stime.tm_mon + 1, @@ -86,9 +99,9 @@ void ConsoleLog::message(Level level, const char* fmt, va_list args) stime.tm_hour, stime.tm_min, stime.tm_sec, - Log::colorByLevel(level, isColors), + Log::colorByLevel(level), fmt, - Log::endl(isColors) + Log::endl() ); print(args); @@ -97,7 +110,7 @@ void ConsoleLog::message(Level level, const char* fmt, va_list args) void ConsoleLog::text(const char* fmt, va_list args) { - snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(m_controller->config()->isColors())); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl()); print(args); } @@ -116,7 +129,11 @@ bool ConsoleLog::isWritable() const void ConsoleLog::print(va_list args) { - m_uvBuf.len = vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args); + char buf[kBufferSize]; + vsnprintf(buf, sizeof(buf) - 1, m_fmt, args); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s", buf); + ConsoleLog::stripColor(); + m_uvBuf.len = snprintf(m_buf, sizeof(m_buf) - 1, "%s", m_fmt); if (m_uvBuf.len <= 0) { return; } diff --git a/src/common/log/ConsoleLog.h b/src/common/log/ConsoleLog.h index bac09a53..94c519d0 100644 --- a/src/common/log/ConsoleLog.h +++ b/src/common/log/ConsoleLog.h @@ -47,6 +47,7 @@ class ConsoleLog : public ILogBackend private: bool isWritable() const; void print(va_list args); + void stripColor(); char m_buf[kBufferSize]; char m_fmt[256]; diff --git a/src/common/log/FileLog.cpp b/src/common/log/FileLog.cpp index 9134c7c7..a9f1d74b 100644 --- a/src/common/log/FileLog.cpp +++ b/src/common/log/FileLog.cpp @@ -44,6 +44,21 @@ FileLog::FileLog(xmrig::Controller *controller, const char *fileName) : } +void FileLog::stripColor() +{ + if (! m_controller->config()->isColors()) { + // strip ANSI CSI sequences + std::string txt(m_fmt); + std::string::size_type i,j; + while ((i = txt.find(CSI)) != std::string::npos){ + j = txt.find('m',i); + txt.erase(i, j-i+1); + } + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s", txt.c_str()); + } +} + + void FileLog::message(Level level, const char* fmt, va_list args) { if (m_file < 0) { @@ -59,8 +74,6 @@ void FileLog::message(Level level, const char* fmt, va_list args) localtime_r(&now, &stime); # endif - const bool isColors = m_controller->config()->isColors(); - snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", stime.tm_year + 1900, stime.tm_mon + 1, @@ -68,13 +81,15 @@ void FileLog::message(Level level, const char* fmt, va_list args) stime.tm_hour, stime.tm_min, stime.tm_sec, - Log::colorByLevel(level, isColors), + Log::colorByLevel(level), fmt, - Log::endl(isColors) + Log::endl() ); - char *buf = new char[kBufferSize]; - const int size = vsnprintf(buf, kBufferSize - 1, m_fmt, args); + vsnprintf(buf, kBufferSize - 1, m_fmt, args); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s", buf); + FileLog::stripColor(); + const int size = snprintf(buf, kBufferSize - 1, "%s", m_fmt); write(buf, size); } @@ -95,10 +110,10 @@ void FileLog::onWrite(uv_fs_t *req) } -void FileLog::write(char *data, size_t size) +void FileLog::write(char *data, const unsigned int size) { - uv_buf_t buf = uv_buf_init(data, (unsigned int) size); - uv_fs_t *req = new uv_fs_t; + uv_buf_t buf = uv_buf_init(data, size); + auto *req = new uv_fs_t; req->data = buf.base; uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, -1, FileLog::onWrite); diff --git a/src/common/log/FileLog.h b/src/common/log/FileLog.h index 8a58d4e4..4ea6abc5 100644 --- a/src/common/log/FileLog.h +++ b/src/common/log/FileLog.h @@ -47,7 +47,8 @@ class FileLog : public ILogBackend private: static void onWrite(uv_fs_t *req); - void write(char *data, size_t size); + void write(char *data, const unsigned int size); + void stripColor(); char m_fmt[256]; int m_file; diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp index 07e4f299..015325aa 100644 --- a/src/common/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -40,14 +40,14 @@ bool Log::colors = true; static const char *color[5] = { - "\x1B[0;31m", /* ERR */ - "\x1B[0;33m", /* WARNING */ - "\x1B[1;37m", /* NOTICE */ + RED_S, /* ERR */ + YELLOW_S, /* WARNING */ + WHITE_BOLD_S, /* NOTICE */ "", /* INFO */ # ifdef WIN32 - "\x1B[1;30m" /* DEBUG */ + BLACK_BOLD_S /* DEBUG */ # else - "\x1B[90m" /* DEBUG */ + BRIGHT_BLACK_S /* DEBUG */ # endif }; @@ -92,23 +92,15 @@ void Log::text(const char* fmt, ...) } -const char *Log::colorByLevel(ILogBackend::Level level, bool isColors) +const char *Log::colorByLevel(ILogBackend::Level level) { - if (!isColors) { - return ""; - } - return color[level]; } -const char *Log::endl(bool isColors) +const char *Log::endl() { -# ifdef _WIN32 - return isColors ? "\x1B[0m\r\n" : "\r\n"; -# else - return isColors ? "\x1B[0m\n" : "\n"; -# endif + return CLEAR ENDL; } diff --git a/src/common/log/Log.h b/src/common/log/Log.h index c32edddd..7e8d4c6e 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -45,8 +45,8 @@ class Log void message(ILogBackend::Level level, const char* fmt, ...); void text(const char* fmt, ...); - static const char *colorByLevel(ILogBackend::Level level, bool isColors = true); - static const char *endl(bool isColors = true); + static const char *colorByLevel(ILogBackend::Level level); + static const char *endl(); static void defaultInit(); static bool colors; @@ -68,19 +68,48 @@ class Log }; -#define RED_BOLD(x) "\x1B[1;31m" x "\x1B[0m" -#define RED(x) "\x1B[0;31m" x "\x1B[0m" -#define GREEN_BOLD(x) "\x1B[1;32m" x "\x1B[0m" -#define GREEN(x) "\x1B[0;32m" x "\x1B[0m" -#define YELLOW(x) "\x1B[0;33m" x "\x1B[0m" -#define YELLOW_BOLD(x) "\x1B[1;33m" x "\x1B[0m" -#define MAGENTA_BOLD(x) "\x1B[1;35m" x "\x1B[0m" -#define MAGENTA(x) "\x1B[0;35m" x "\x1B[0m" -#define CYAN_BOLD(x) "\x1B[1;36m" x "\x1B[0m" -#define CYAN(x) "\x1B[0;36m" x "\x1B[0m" -#define WHITE_BOLD(x) "\x1B[1;37m" x "\x1B[0m" -#define WHITE(x) "\x1B[0;37m" x "\x1B[0m" -#define GRAY(x) "\x1B[1;30m" x "\x1B[0m" +# ifdef _WIN32 +#define ENDL "\r\n" +# else +#define ENDL "\n" +# endif + +#define CSI "\x1B[" // Control Sequence Introducer (ANSI spec name) +#define CLEAR CSI "0m" // all attributes off +#define BRIGHT_BLACK_S CSI "90m" // somewhat MD.GRAY +#define BLACK_S CSI "0;30m" +#define BLACK_BOLD_S CSI "1;30m" // another name for GRAY +#define RED_S CSI "0;31m" +#define RED_BOLD_S CSI "1;31m" +#define GREEN_S CSI "0;32m" +#define GREEN_BOLD_S CSI "1;32m" +#define YELLOW_S CSI "0;33m" +#define YELLOW_BOLD_S CSI "1;33m" +#define BLUE_S CSI "0;34m" +#define BLUE_BOLD_S CSI "1;34m" +#define MAGENTA_S CSI "0;35m" +#define MAGENTA_BOLD_S CSI "1;35m" +#define CYAN_S CSI "0;36m" +#define CYAN_BOLD_S CSI "1;36m" +#define WHITE_S CSI "0;37m" // another name for LT.GRAY +#define WHITE_BOLD_S CSI "1;37m" // actually white +//color wrappings +#define BLACK(x) BLACK_S x CLEAR +#define BLACK_BOLD(x) BLACK_BOLD_S x CLEAR +#define RED(x) RED_S x CLEAR +#define RED_BOLD(x) RED_BOLD_S x CLEAR +#define GREEN(x) GREEN_S x CLEAR +#define GREEN_BOLD(x) GREEN_BOLD_S x CLEAR +#define YELLOW(x) YELLOW_S x CLEAR +#define YELLOW_BOLD(x) YELLOW_BOLD_S x CLEAR +#define BLUE(x) BLUE_S x CLEAR +#define BLUE_BOLD(x) BLUE_BOLD_S x CLEAR +#define MAGENTA(x) MAGENTA_S x CLEAR +#define MAGENTA_BOLD(x) MAGENTA_BOLD_S x CLEAR +#define CYAN(x) CYAN_S x CLEAR +#define CYAN_BOLD(x) CYAN_BOLD_S x CLEAR +#define WHITE(x) WHITE_S x CLEAR +#define WHITE_BOLD(x) WHITE_BOLD_S x CLEAR #define LOG_ERR(x, ...) Log::i()->message(ILogBackend::ERR, x, ##__VA_ARGS__) diff --git a/src/common/log/SysLog.cpp b/src/common/log/SysLog.cpp index bcb96394..c6205b5d 100644 --- a/src/common/log/SysLog.cpp +++ b/src/common/log/SysLog.cpp @@ -26,6 +26,7 @@ #include "common/log/SysLog.h" +#include "common/log/Log.h" #include "version.h" @@ -35,9 +36,26 @@ SysLog::SysLog() } +void SysLog::stripColor() +{ + // strip ANSI CSI sequences + std::string txt(m_fmt); + std::string::size_type i, j; + while ((i = txt.find(CSI)) != std::string::npos) { + j = txt.find('m', i); + txt.erase(i, j-i+1); + } + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s", txt.c_str()); +} + + void SysLog::message(Level level, const char *fmt, va_list args) { - vsyslog(static_cast(level), fmt, args); + char buf[kBufferSize]; + vsnprintf(buf, sizeof(buf) - 1, m_fmt, args); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s", buf); + SysLog::stripColor(); + vsyslog(static_cast(level), m_fmt, args); } diff --git a/src/common/log/SysLog.h b/src/common/log/SysLog.h index 5cfeefcd..61d9c753 100644 --- a/src/common/log/SysLog.h +++ b/src/common/log/SysLog.h @@ -35,6 +35,10 @@ class SysLog : public ILogBackend void message(Level level, const char *fmt, va_list args) override; void text(const char *fmt, va_list args) override; + void stripColor(); + +private: + char m_fmt[256]; }; #endif /* __SYSLOG_BACKEND_H__ */ diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 34714c8a..37aded2e 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -94,13 +94,12 @@ void xmrig::Network::onActive(IStrategy *strategy, Client *client) m_state.setPool(client->host(), client->port(), client->ip()); const char *tlsVersion = client->tlsVersion(); - LOG_INFO(isColors() ? WHITE_BOLD("use pool ") CYAN_BOLD("%s:%d ") GREEN_BOLD("%s") " \x1B[1;30m%s " - : "use pool %s:%d %s %s", + LOG_INFO(WHITE_BOLD("use pool ") CYAN_BOLD("%s:%d ") GREEN_BOLD("%s") " " BLACK_BOLD_S "%s", client->host(), client->port(), tlsVersion ? tlsVersion : "", client->ip()); const char *fingerprint = client->tlsFingerprint(); if (fingerprint != nullptr) { - LOG_INFO("%sfingerprint (SHA-256): \"%s\"", isColors() ? "\x1B[1;30m" : "", fingerprint); + LOG_INFO(BLACK_BOLD_S "fingerprint (SHA-256): \"%s\"", fingerprint); } } @@ -162,13 +161,11 @@ void xmrig::Network::onResultAccepted(IStrategy *, Client *, const SubmitResult m_state.add(result, error); if (error) { - LOG_INFO(isColors() ? "\x1B[1;31mrejected\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[1;37m%u\x1B[0m \x1B[31m\"%s\"\x1B[0m \x1B[1;30m(%" PRIu64 " ms)" - : "rejected (%" PRId64 "/%" PRId64 ") diff %u \"%s\" (%" PRIu64 " ms)", + LOG_INFO(RED_BOLD("rejected") " (%" PRId64 "/%" PRId64 ") diff " WHITE_BOLD("%u") " " RED("\"%s\"") " " RED_BOLD("(%" PRIu64 " ms)"), m_state.accepted, m_state.rejected, result.diff, error, result.elapsed); } else { - LOG_INFO(isColors() ? "\x1B[1;32maccepted\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[1;37m%u\x1B[0m \x1B[1;30m(%" PRIu64 " ms)" - : "accepted (%" PRId64 "/%" PRId64 ") diff %u (%" PRIu64 " ms)", + LOG_INFO(GREEN_BOLD("accepted") " (%" PRId64 "/%" PRId64 ") diff " WHITE_BOLD("%u") " " BLACK_BOLD("(%" PRIu64 " ms)"), m_state.accepted, m_state.rejected, result.diff, result.elapsed); } } @@ -183,13 +180,11 @@ bool xmrig::Network::isColors() const void xmrig::Network::setJob(Client *client, const Job &job, bool donate) { if (job.height()) { - LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") " height " WHITE_BOLD("%" PRIu64) - : "new job from %s:%d diff %d algo %s height %" PRIu64, + LOG_INFO(MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") " height " WHITE_BOLD("%" PRIu64), client->host(), client->port(), job.diff(), job.algorithm().shortName(), job.height()); } else { - LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") - : "new job from %s:%d diff %d algo %s", + LOG_INFO(MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s"), client->host(), client->port(), job.diff(), job.algorithm().shortName()); } diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 40267dc9..2b4bf66e 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -150,8 +150,7 @@ void Hashrate::print() const char num3[8] = { 0 }; char num4[8] = { 0 }; - LOG_INFO(m_controller->config()->isColors() ? WHITE_BOLD("speed") " 10s/60s/15m " CYAN_BOLD("%s") CYAN(" %s %s ") CYAN_BOLD("H/s") " max " CYAN_BOLD("%s H/s") - : "speed 10s/60s/15m %s %s %s H/s max %s H/s", + LOG_INFO(WHITE_BOLD("speed") " 10s/60s/15m " CYAN_BOLD("%s") " " CYAN("%s %s") " " CYAN_BOLD("H/s") " max " CYAN_BOLD("%s H/s"), format(calc(ShortInterval), num1, sizeof(num1)), format(calc(MediumInterval), num2, sizeof(num2)), format(calc(LargeInterval), num3, sizeof(num3)), diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 3ffda598..e58ebb50 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -104,22 +104,20 @@ void Workers::printHashrate(bool detail) } if (detail) { - const bool isColors = m_controller->config()->isColors(); char num1[8] = { 0 }; char num2[8] = { 0 }; char num3[8] = { 0 }; - Log::i()->text("%s| THREAD | GPU | 10s H/s | 60s H/s | 15m H/s | NAME", isColors ? "\x1B[1;37m" : ""); + Log::i()->text(WHITE_BOLD_S "| THREAD | GPU | 10s H/s | 60s H/s | 15m H/s | NAME"); size_t i = 0; for (const xmrig::IThread *t : m_controller->config()->threads()) { auto thread = static_cast(t); - Log::i()->text("| %6zu | %3zu | %7s | %7s | %7s | %s%s", + Log::i()->text("| %6zu | %3zu | %7s | %7s | %7s | " BLACK_BOLD_S "%s", i, thread->index(), Hashrate::format(m_hashrate->calc(i, Hashrate::ShortInterval), num1, sizeof num1), Hashrate::format(m_hashrate->calc(i, Hashrate::MediumInterval), num2, sizeof num2), Hashrate::format(m_hashrate->calc(i, Hashrate::LargeInterval), num3, sizeof num3), - isColors ? "\x1B[1;30m" : "", thread->name() ); @@ -130,6 +128,18 @@ void Workers::printHashrate(bool detail) m_hashrate->print(); } +const std::string _spf(const char * const fmt, ...) +{ + va_list args = nullptr, copy = nullptr; + va_start(args, fmt); + va_copy(copy, args); + const auto len = static_cast(std::vsnprintf(nullptr, 0, fmt, copy)); + va_end(copy); + std::vector str(len + 1); + std::vsnprintf(str.data(), str.size(), fmt, args); + va_end(args); + return std::string(str.data(), len); +} void Workers::printHealth() { @@ -147,25 +157,27 @@ void Workers::printHealth() const uint32_t temp = health.temperature; - if (health.clock && health.clock) { - if (m_controller->config()->isColors()) { - LOG_INFO("\x1B[00;35mGPU #%d: \x1B[01m%u\x1B[00;35m/\x1B[01m%u MHz\x1B[00;35m \x1B[01m%uW\x1B[00;35m %s%uC\x1B[00;35m FAN \x1B[01m%u%%", - thread->index(), health.clock, health.memClock, health.power / 1000, (temp < 45 ? "\x1B[01;32m" : (temp > 65 ? "\x1B[01;31m" : "\x1B[01;33m")), temp, health.fanSpeed); - } - else { - LOG_INFO(" * GPU #%d: %u/%u MHz %uW %uC FAN %u%%", thread->index(), health.clock, health.memClock, health.power / 1000, health.temperature, health.fanSpeed); - } + if (health.clock && health.memClock) { + LOG_INFO(" * " MAGENTA("GPU #%d:") " " + MAGENTA_BOLD("%u") MAGENTA("/") MAGENTA_BOLD("%u MHz") " " + MAGENTA_BOLD("%uW") " " + "%s%uC%s " + MAGENTA("FAN") " " MAGENTA_BOLD_S "%u%%", + thread->index(), + health.clock, health.memClock, + health.power / 1000, + (temp < 45 ? GREEN_BOLD_S : (temp > 65 ? RED_BOLD_S : YELLOW_BOLD_S)), temp, CLEAR, + health.fanSpeed); continue; } - if (m_controller->config()->isColors()) { - LOG_INFO("\x1B[00;35mGPU #%d: %s%uC\x1B[00;35m FAN \x1B[01m%u%%", - thread->index(), (temp < 45 ? "\x1B[01;32m" : (temp > 65 ? "\x1B[01;31m" : "\x1B[01;33m")), temp, health.fanSpeed); - } - else { - LOG_INFO(" * GPU #%d: %uC FAN %u%%", thread->index(), health.temperature, health.fanSpeed); - } + LOG_INFO(" * " MAGENTA("GPU #%d:") " " + "%s%uC%s " + MAGENTA("FAN") " " MAGENTA_BOLD_S "%u%%", + thread->index(), + (temp < 45 ? GREEN_BOLD_S : (temp > 65 ? RED_BOLD_S : YELLOW_BOLD_S)), temp, CLEAR, + health.fanSpeed); } }