Correctly reset colors in FileLog.

This commit is contained in:
XMRig 2018-05-19 13:06:49 +07:00
parent 0086020b5c
commit 5019493332
9 changed files with 45 additions and 35 deletions

View file

@ -97,7 +97,7 @@ void ConsoleLog::message(Level level, const char* fmt, va_list args)
void ConsoleLog::text(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); print(args);
} }

View file

@ -48,7 +48,7 @@ private:
bool isWritable() const; bool isWritable() const;
void print(va_list args); void print(va_list args);
char m_buf[512]; char m_buf[kBufferSize];
char m_fmt[256]; char m_fmt[256];
uv_buf_t m_uvBuf; uv_buf_t m_uvBuf;
uv_stream_t *m_stream; uv_stream_t *m_stream;

View file

@ -30,9 +30,13 @@
#include "common/log/FileLog.h" #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; uv_fs_t req;
m_file = uv_fs_open(uv_default_loop(), &req, fileName, O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr); 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); localtime_r(&now, &stime);
# endif # endif
char *buf = new char[512]; const bool isColors = m_controller->config()->isColors();
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);
size = vsnprintf(buf + size, 512 - size - 1, fmt, args) + size; snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s",
buf[size] = '\n'; 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) void FileLog::onWrite(uv_fs_t *req)
{ {
delete [] static_cast<char *>(req->data); delete [] static_cast<char *>(req->data);

View file

@ -31,10 +31,15 @@
#include "interfaces/ILogBackend.h" #include "interfaces/ILogBackend.h"
namespace xmrig {
class Controller;
}
class FileLog : public ILogBackend class FileLog : public ILogBackend
{ {
public: public:
FileLog(const char *fileName); FileLog(xmrig::Controller *controller, const char *fileName);
void message(Level 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; void text(const char* fmt, va_list args) override;
@ -44,7 +49,9 @@ private:
void write(char *data, size_t size); void write(char *data, size_t size);
char m_fmt[256];
int m_file; int m_file;
xmrig::Controller *m_controller;
}; };
#endif /* __FILELOG_H__ */ #endif /* __FILELOG_H__ */

View file

@ -37,11 +37,15 @@ Log *Log::m_self = nullptr;
static const char *colors[5] = { static const char *colors[5] = {
Log::kCL_RED, /* ERR */ "\x1B[0;31m", /* ERR */
Log::kCL_YELLOW, /* WARNING */ "\x1B[0;33m", /* WARNING */
Log::kCL_WHITE, /* NOTICE */ "\x1B[1;37m", /* NOTICE */
"", /* INFO */ "", /* INFO */
Log::kCL_GRAY /* DEBUG */ # ifdef WIN32
"\x1B[1;30m" /* DEBUG */
# else
"\x1B[90m" /* DEBUG */
# endif
}; };

View file

@ -36,17 +36,6 @@
class Log class Log
{ {
public: 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 Log* i() { assert(m_self != nullptr); return m_self; }
static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); } static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); }
static inline void init() { if (!m_self) { new Log(); } } static inline void init() { if (!m_self) { new Log(); } }

View file

@ -37,11 +37,11 @@ SysLog::SysLog()
void SysLog::message(Level level, const char *fmt, va_list args) void SysLog::message(Level level, const char *fmt, va_list args)
{ {
vsyslog(level, fmt, args); vsyslog(static_cast<int>(level), fmt, args);
} }
void SysLog::text(const char *fmt, va_list args) void SysLog::text(const char *fmt, va_list args)
{ {
message(LOG_INFO, fmt, args); vsyslog(LOG_INFO, fmt, args);
} }

View file

@ -110,7 +110,7 @@ int xmrig::Controller::init(int argc, char **argv)
} }
if (config()->logFile()) { if (config()->logFile()) {
Log::add(new FileLog(config()->logFile())); Log::add(new FileLog(this, config()->logFile()));
} }
# ifdef HAVE_SYSLOG_H # ifdef HAVE_SYSLOG_H

View file

@ -39,6 +39,8 @@ public:
DEBUG DEBUG
}; };
constexpr static const size_t kBufferSize = 512;
virtual ~ILogBackend() {} virtual ~ILogBackend() {}
virtual void message(Level level, const char* fmt, va_list args) = 0; virtual void message(Level level, const char* fmt, va_list args) = 0;