Optimize logs architecture.

This commit is contained in:
XMRig 2018-05-19 11:48:15 +07:00
parent adc6adb6d5
commit 0086020b5c
9 changed files with 73 additions and 66 deletions

View file

@ -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);

View file

@ -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:

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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);
}

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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:

View file

@ -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) {

View file

@ -30,20 +30,12 @@
#include <vector>
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, ...)

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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);
}

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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;
};

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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;
};