Skip to content

Commit

Permalink
Refactor and simplify color handling (generate always, strip once ins…
Browse files Browse the repository at this point in the history
…tead of isColor() checking everywhere and duplication)
  • Loading branch information
Spudz76 committed Mar 24, 2019
1 parent c68451b commit 1a2768a
Show file tree
Hide file tree
Showing 17 changed files with 227 additions and 160 deletions.
18 changes: 9 additions & 9 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -149,15 +149,15 @@ 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;

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;
Expand All @@ -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;

Expand All @@ -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:
Expand Down
65 changes: 23 additions & 42 deletions src/Summary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}


Expand All @@ -70,40 +63,30 @@ static void print_gpu(xmrig::Config *config)
constexpr size_t byteToMiB = 1024u * 1024u;
for (const xmrig::IThread *t : config->threads()) {
auto thread = static_cast<const CudaThread *>(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
);
}
}


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"));
}


Expand All @@ -119,5 +102,3 @@ void Summary::print(xmrig::Controller *controller)
print_commands(controller->config());
}



26 changes: 7 additions & 19 deletions src/base/net/Pools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
9 changes: 3 additions & 6 deletions src/common/config/CommonConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down
25 changes: 21 additions & 4 deletions src/common/log/BasicLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -72,15 +85,19 @@ 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);
}


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;
}

Expand Down
1 change: 1 addition & 0 deletions src/common/log/BasicLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
29 changes: 23 additions & 6 deletions src/common/log/ConsoleLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -77,18 +92,16 @@ 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,
stime.tm_mday,
stime.tm_hour,
stime.tm_min,
stime.tm_sec,
Log::colorByLevel(level, isColors),
Log::colorByLevel(level),
fmt,
Log::endl(isColors)
Log::endl()
);

print(args);
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/common/log/ConsoleLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Loading

0 comments on commit 1a2768a

Please sign in to comment.