Add ConsoleLog class and ILogBackend interface.

This commit is contained in:
XMRig 2017-06-22 14:41:34 +03:00
parent 91ed7e36cd
commit 052290d0e9
7 changed files with 232 additions and 91 deletions

View file

@ -34,109 +34,42 @@
# include "3rdparty/winansi.h"
#endif
#include "interfaces/ILogBackend.h"
#include "log/Log.h"
Log *Log::m_self = nullptr;
void Log::init(bool colors, bool background)
{
if (!m_self) {
m_self = new Log(colors, background);
}
}
void Log::message(Log::Level level, const char* fmt, ...)
{
time_t now = time(nullptr);
tm stime;
va_list args;
va_start(args, fmt);
# ifdef _WIN32
localtime_s(&stime, &now);
# else
localtime_r(&now, &stime);
# endif
va_list ap;
va_start(ap, fmt);
const char* color = nullptr;
if (m_colors) {
switch (level) {
case ERR:
color = kCL_RED;
break;
case WARNING:
color = kCL_YELLOW;
break;
case NOTICE:
color = kCL_WHITE;
break;
case DEBUG:
color = kCL_GRAY;
break;
default:
color = "";
break;
}
for (ILogBackend *backend : m_backends) {
backend->message(level, fmt, args);
}
const size_t len = 64 + strlen(fmt) + 2;
char *buf = static_cast<char *>(alloca(len));
sprintf(buf, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n",
stime.tm_year + 1900,
stime.tm_mon + 1,
stime.tm_mday,
stime.tm_hour,
stime.tm_min,
stime.tm_sec,
m_colors ? color : "",
fmt,
m_colors ? kCL_N : ""
);
uv_mutex_lock(&m_mutex);
vfprintf(stdout, buf, ap);
fflush(stdout);
uv_mutex_unlock(&m_mutex);
va_end(ap);
va_end(args);
}
void Log::text(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
va_list args;
va_start(args, fmt);
const int len = 64 + strlen(fmt) + 2;
char *buf = static_cast<char *>(alloca(len));
for (ILogBackend *backend : m_backends) {
backend->text(fmt, args);
}
sprintf(buf, "%s%s\n", fmt, m_colors ? kCL_N : "");
uv_mutex_lock(&m_mutex);
vfprintf(stdout, buf, ap);
fflush(stdout);
uv_mutex_unlock(&m_mutex);
va_end(ap);
va_end(args);
}
Log::Log(bool colors, bool background) :
m_background(background),
m_colors(colors)
Log::~Log()
{
uv_mutex_init(&m_mutex);
for (auto backend : m_backends) {
delete backend;
}
}