From 0086020b5c6c0199e88edd06df42af5743bbff20 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 19 May 2018 11:48:15 +0700 Subject: [PATCH 1/3] Optimize logs architecture. --- src/common/log/ConsoleLog.cpp | 35 +++++------------------------------ src/common/log/ConsoleLog.h | 2 +- src/common/log/FileLog.cpp | 8 ++++---- src/common/log/FileLog.h | 6 +++--- src/common/log/Log.cpp | 31 ++++++++++++++++++++++++++++++- src/common/log/Log.h | 29 ++++++++++++----------------- src/common/log/SysLog.cpp | 6 +++--- src/common/log/SysLog.h | 6 +++--- src/interfaces/ILogBackend.h | 16 ++++++++++++---- 9 files changed, 73 insertions(+), 66 deletions(-) diff --git a/src/common/log/ConsoleLog.cpp b/src/common/log/ConsoleLog.cpp index b7d187d12..a1b6648dc 100644 --- a/src/common/log/ConsoleLog.cpp +++ b/src/common/log/ConsoleLog.cpp @@ -66,7 +66,7 @@ ConsoleLog::ConsoleLog(xmrig::Controller *controller) : } -void ConsoleLog::message(int level, const char* fmt, va_list args) +void ConsoleLog::message(Level level, const char* fmt, va_list args) { time_t now = time(nullptr); tm stime; @@ -77,43 +77,18 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) localtime_r(&now, &stime); # endif - const char* color = nullptr; - const bool colors = m_controller->config()->isColors(); + const bool isColors = m_controller->config()->isColors(); - if (colors) { - switch (level) { - case Log::ERR: - color = Log::kCL_RED; - break; - - case Log::WARNING: - color = Log::kCL_YELLOW; - break; - - case Log::NOTICE: - color = Log::kCL_WHITE; - break; - - case Log::DEBUG: - color = Log::kCL_GRAY; - break; - - default: - color = ""; - break; - } - } - - snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n", + 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, - colors ? color : "", + Log::colorByLevel(level, isColors), fmt, - colors ? Log::kCL_N : "" + Log::endl(isColors) ); print(args); diff --git a/src/common/log/ConsoleLog.h b/src/common/log/ConsoleLog.h index 6649be841..a3b0a33a8 100644 --- a/src/common/log/ConsoleLog.h +++ b/src/common/log/ConsoleLog.h @@ -41,7 +41,7 @@ class ConsoleLog : public ILogBackend public: ConsoleLog(xmrig::Controller *controller); - void message(int level, const char *fmt, va_list args) override; + void message(Level level, const char *fmt, va_list args) override; void text(const char *fmt, va_list args) override; private: diff --git a/src/common/log/FileLog.cpp b/src/common/log/FileLog.cpp index cc7188cbd..367f0f687 100644 --- a/src/common/log/FileLog.cpp +++ b/src/common/log/FileLog.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,7 +40,7 @@ FileLog::FileLog(const char *fileName) } -void FileLog::message(int level, const char* fmt, va_list args) +void FileLog::message(Level level, const char* fmt, va_list args) { if (m_file < 0) { return; @@ -73,7 +73,7 @@ void FileLog::message(int level, const char* fmt, va_list args) void FileLog::text(const char* fmt, va_list args) { - message(0, fmt, args); + message(INFO, fmt, args); } diff --git a/src/common/log/FileLog.h b/src/common/log/FileLog.h index 2b3ca5d4b..545056bb6 100644 --- a/src/common/log/FileLog.h +++ b/src/common/log/FileLog.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,7 +36,7 @@ class FileLog : public ILogBackend public: FileLog(const char *fileName); - void message(int level, const char* fmt, va_list args) override; + void message(Level level, const char* fmt, va_list args) override; void text(const char* fmt, va_list args) override; private: diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp index 03237f03a..6144e4f17 100644 --- a/src/common/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -36,7 +36,16 @@ Log *Log::m_self = nullptr; -void Log::message(Log::Level level, const char* fmt, ...) +static const char *colors[5] = { + Log::kCL_RED, /* ERR */ + Log::kCL_YELLOW, /* WARNING */ + Log::kCL_WHITE, /* NOTICE */ + "", /* INFO */ + Log::kCL_GRAY /* DEBUG */ +}; + + +void Log::message(ILogBackend::Level level, const char* fmt, ...) { uv_mutex_lock(&m_mutex); @@ -76,6 +85,26 @@ void Log::text(const char* fmt, ...) } +const char *Log::colorByLevel(ILogBackend::Level level, bool isColors) +{ + if (!isColors) { + return ""; + } + + return colors[level]; +} + + +const char *Log::endl(bool isColors) +{ +# ifdef _WIN32 + return isColors ? "\x1B[0m\r\n" : "\r\n"; +# else + return isColors ? "\x1B[0m\n" : "\n"; +# endif +} + + Log::~Log() { for (auto backend : m_backends) { diff --git a/src/common/log/Log.h b/src/common/log/Log.h index 31600dc68..6d4959efc 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -30,20 +30,12 @@ #include -class ILogBackend; +#include "interfaces/ILogBackend.h" class Log { public: - enum Level { - ERR, - WARNING, - NOTICE, - INFO, - DEBUG - }; - constexpr static const char* kCL_N = "\x1B[0m"; constexpr static const char* kCL_RED = "\x1B[31m"; constexpr static const char* kCL_YELLOW = "\x1B[33m"; @@ -60,9 +52,12 @@ public: static inline void init() { if (!m_self) { new Log(); } } static inline void release() { assert(m_self != nullptr); delete m_self; } - void message(Level level, const char* fmt, ...); + 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); + private: inline Log() { assert(m_self == nullptr); @@ -92,20 +87,20 @@ private: #define WHITE(x) "\x1B[0;37m" x "\x1B[0m" -#define LOG_ERR(x, ...) Log::i()->message(Log::ERR, x, ##__VA_ARGS__) -#define LOG_WARN(x, ...) Log::i()->message(Log::WARNING, x, ##__VA_ARGS__) -#define LOG_NOTICE(x, ...) Log::i()->message(Log::NOTICE, x, ##__VA_ARGS__) -#define LOG_INFO(x, ...) Log::i()->message(Log::INFO, x, ##__VA_ARGS__) +#define LOG_ERR(x, ...) Log::i()->message(ILogBackend::ERR, x, ##__VA_ARGS__) +#define LOG_WARN(x, ...) Log::i()->message(ILogBackend::WARNING, x, ##__VA_ARGS__) +#define LOG_NOTICE(x, ...) Log::i()->message(ILogBackend::NOTICE, x, ##__VA_ARGS__) +#define LOG_INFO(x, ...) Log::i()->message(ILogBackend::INFO, x, ##__VA_ARGS__) #ifdef APP_DEBUG -# define LOG_DEBUG(x, ...) Log::i()->message(Log::DEBUG, x, ##__VA_ARGS__) +# define LOG_DEBUG(x, ...) Log::i()->message(ILogBackend::DEBUG, x, ##__VA_ARGS__) #else # define LOG_DEBUG(x, ...) #endif #if defined(APP_DEBUG) || defined(APP_DEVEL) -# define LOG_DEBUG_ERR(x, ...) Log::i()->message(Log::ERR, x, ##__VA_ARGS__) -# define LOG_DEBUG_WARN(x, ...) Log::i()->message(Log::WARNING, x, ##__VA_ARGS__) +# define LOG_DEBUG_ERR(x, ...) Log::i()->message(ILogBackend::ERR, x, ##__VA_ARGS__) +# define LOG_DEBUG_WARN(x, ...) Log::i()->message(ILogBackend::WARNING, x, ##__VA_ARGS__) #else # define LOG_DEBUG_ERR(x, ...) # define LOG_DEBUG_WARN(x, ...) diff --git a/src/common/log/SysLog.cpp b/src/common/log/SysLog.cpp index 70879c33b..5eb4dcd45 100644 --- a/src/common/log/SysLog.cpp +++ b/src/common/log/SysLog.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ SysLog::SysLog() } -void SysLog::message(int level, const char *fmt, va_list args) +void SysLog::message(Level level, const char *fmt, va_list args) { vsyslog(level, fmt, args); } diff --git a/src/common/log/SysLog.h b/src/common/log/SysLog.h index 38de1a6a5..1e49a48bb 100644 --- a/src/common/log/SysLog.h +++ b/src/common/log/SysLog.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +33,7 @@ class SysLog : public ILogBackend public: SysLog(); - void message(int level, const char *fmt, va_list args) override; + void message(Level level, const char *fmt, va_list args) override; void text(const char *fmt, va_list args) override; }; diff --git a/src/interfaces/ILogBackend.h b/src/interfaces/ILogBackend.h index 458b504c4..b6db72a89 100644 --- a/src/interfaces/ILogBackend.h +++ b/src/interfaces/ILogBackend.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,10 +31,18 @@ class ILogBackend { public: + enum Level { + ERR, + WARNING, + NOTICE, + INFO, + DEBUG + }; + virtual ~ILogBackend() {} - virtual void message(int level, const char* fmt, va_list args) = 0; - virtual void text(const char* fmt, va_list args) = 0; + virtual void message(Level level, const char* fmt, va_list args) = 0; + virtual void text(const char* fmt, va_list args) = 0; }; From 5019493332ae66adbc616f99fc85ef32938aa1e2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 19 May 2018 13:06:49 +0700 Subject: [PATCH 2/3] Correctly reset colors in FileLog. --- src/common/log/ConsoleLog.cpp | 2 +- src/common/log/ConsoleLog.h | 2 +- src/common/log/FileLog.cpp | 34 +++++++++++++++++++++------------- src/common/log/FileLog.h | 9 ++++++++- src/common/log/Log.cpp | 14 +++++++++----- src/common/log/Log.h | 11 ----------- src/common/log/SysLog.cpp | 4 ++-- src/core/Controller.cpp | 2 +- src/interfaces/ILogBackend.h | 2 ++ 9 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/common/log/ConsoleLog.cpp b/src/common/log/ConsoleLog.cpp index a1b6648dc..6cf61980b 100644 --- a/src/common/log/ConsoleLog.cpp +++ b/src/common/log/ConsoleLog.cpp @@ -97,7 +97,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\n", fmt, m_controller->config()->isColors() ? Log::kCL_N : ""); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(m_controller->config()->isColors())); print(args); } diff --git a/src/common/log/ConsoleLog.h b/src/common/log/ConsoleLog.h index a3b0a33a8..81813c816 100644 --- a/src/common/log/ConsoleLog.h +++ b/src/common/log/ConsoleLog.h @@ -48,7 +48,7 @@ private: bool isWritable() const; void print(va_list args); - char m_buf[512]; + char m_buf[kBufferSize]; char m_fmt[256]; uv_buf_t m_uvBuf; uv_stream_t *m_stream; diff --git a/src/common/log/FileLog.cpp b/src/common/log/FileLog.cpp index 367f0f687..9134c7c75 100644 --- a/src/common/log/FileLog.cpp +++ b/src/common/log/FileLog.cpp @@ -30,9 +30,13 @@ #include "common/log/FileLog.h" +#include "common/log/Log.h" +#include "core/Config.h" +#include "core/Controller.h" -FileLog::FileLog(const char *fileName) +FileLog::FileLog(xmrig::Controller *controller, const char *fileName) : + m_controller(controller) { uv_fs_t req; m_file = uv_fs_open(uv_default_loop(), &req, fileName, O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr); @@ -55,19 +59,24 @@ void FileLog::message(Level level, const char* fmt, va_list args) localtime_r(&now, &stime); # endif - char *buf = new char[512]; - int size = snprintf(buf, 23, "[%d-%02d-%02d %02d:%02d:%02d] ", - stime.tm_year + 1900, - stime.tm_mon + 1, - stime.tm_mday, - stime.tm_hour, - stime.tm_min, - stime.tm_sec); + const bool isColors = m_controller->config()->isColors(); - size = vsnprintf(buf + size, 512 - size - 1, fmt, args) + size; - buf[size] = '\n'; + 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), + fmt, + Log::endl(isColors) + ); - write(buf, size + 1); + char *buf = new char[kBufferSize]; + const int size = vsnprintf(buf, kBufferSize - 1, m_fmt, args); + + write(buf, size); } @@ -77,7 +86,6 @@ void FileLog::text(const char* fmt, va_list args) } - void FileLog::onWrite(uv_fs_t *req) { delete [] static_cast(req->data); diff --git a/src/common/log/FileLog.h b/src/common/log/FileLog.h index 545056bb6..2c25f0932 100644 --- a/src/common/log/FileLog.h +++ b/src/common/log/FileLog.h @@ -31,10 +31,15 @@ #include "interfaces/ILogBackend.h" +namespace xmrig { + class Controller; +} + + class FileLog : public ILogBackend { public: - FileLog(const char *fileName); + FileLog(xmrig::Controller *controller, const char *fileName); void message(Level level, const char* fmt, va_list args) override; void text(const char* fmt, va_list args) override; @@ -44,7 +49,9 @@ private: void write(char *data, size_t size); + char m_fmt[256]; int m_file; + xmrig::Controller *m_controller; }; #endif /* __FILELOG_H__ */ diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp index 6144e4f17..763eb25b0 100644 --- a/src/common/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -37,11 +37,15 @@ Log *Log::m_self = nullptr; static const char *colors[5] = { - Log::kCL_RED, /* ERR */ - Log::kCL_YELLOW, /* WARNING */ - Log::kCL_WHITE, /* NOTICE */ - "", /* INFO */ - Log::kCL_GRAY /* DEBUG */ + "\x1B[0;31m", /* ERR */ + "\x1B[0;33m", /* WARNING */ + "\x1B[1;37m", /* NOTICE */ + "", /* INFO */ +# ifdef WIN32 + "\x1B[1;30m" /* DEBUG */ +# else + "\x1B[90m" /* DEBUG */ +# endif }; diff --git a/src/common/log/Log.h b/src/common/log/Log.h index 6d4959efc..c4803e3a0 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -36,17 +36,6 @@ class Log { public: - constexpr static const char* kCL_N = "\x1B[0m"; - constexpr static const char* kCL_RED = "\x1B[31m"; - constexpr static const char* kCL_YELLOW = "\x1B[33m"; - constexpr static const char* kCL_WHITE = "\x1B[01;37m"; - -# ifdef WIN32 - constexpr static const char* kCL_GRAY = "\x1B[01;30m"; -# else - constexpr static const char* kCL_GRAY = "\x1B[90m"; -# endif - static inline Log* i() { assert(m_self != nullptr); return m_self; } static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); } static inline void init() { if (!m_self) { new Log(); } } diff --git a/src/common/log/SysLog.cpp b/src/common/log/SysLog.cpp index 5eb4dcd45..bcb963943 100644 --- a/src/common/log/SysLog.cpp +++ b/src/common/log/SysLog.cpp @@ -37,11 +37,11 @@ SysLog::SysLog() void SysLog::message(Level level, const char *fmt, va_list args) { - vsyslog(level, fmt, args); + vsyslog(static_cast(level), fmt, args); } void SysLog::text(const char *fmt, va_list args) { - message(LOG_INFO, fmt, args); + vsyslog(LOG_INFO, fmt, args); } diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index b1e03f32e..ac64f173b 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -110,7 +110,7 @@ int xmrig::Controller::init(int argc, char **argv) } if (config()->logFile()) { - Log::add(new FileLog(config()->logFile())); + Log::add(new FileLog(this, config()->logFile())); } # ifdef HAVE_SYSLOG_H diff --git a/src/interfaces/ILogBackend.h b/src/interfaces/ILogBackend.h index b6db72a89..305549054 100644 --- a/src/interfaces/ILogBackend.h +++ b/src/interfaces/ILogBackend.h @@ -39,6 +39,8 @@ public: DEBUG }; + constexpr static const size_t kBufferSize = 512; + virtual ~ILogBackend() {} virtual void message(Level level, const char* fmt, va_list args) = 0; From 2b3f7f43ceb22a18316d424d31eca949743faf0c Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 19 May 2018 13:47:27 +0700 Subject: [PATCH 3/3] Fix Linux build. --- src/interfaces/ILogBackend.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/interfaces/ILogBackend.h b/src/interfaces/ILogBackend.h index 305549054..6e3f5c1c0 100644 --- a/src/interfaces/ILogBackend.h +++ b/src/interfaces/ILogBackend.h @@ -26,6 +26,7 @@ #include +#include class ILogBackend