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

@ -10,7 +10,9 @@ set(HEADERS
src/Cpu.h
src/interfaces/IClientListener.h
src/interfaces/IJobResultListener.h
src/interfaces/ILogBackend.h
src/interfaces/IWorker.h
src/log/ConsoleLog.h
src/log/Log.h
src/Mem.h
src/net/Client.h
@ -45,6 +47,7 @@ set(HEADERS_CRYPTO
set(SOURCES
src/App.cpp
src/log/ConsoleLog.cpp
src/log/Log.cpp
src/Mem.cpp
src/net/Client.cpp

View file

@ -27,9 +27,10 @@
#include "App.h"
#include "log/Log.h"
#include "Cpu.h"
#include "crypto/CryptoNight.h"
#include "log/ConsoleLog.h"
#include "log/Log.h"
#include "Mem.h"
#include "net/Network.h"
#include "Options.h"
@ -51,7 +52,11 @@ App::App(int argc, char **argv) :
Cpu::init();
m_options = Options::parse(argc, argv);
Log::init(m_options->colors(), m_options->background());
Log::init();
if (!m_options->background()) {
Log::add(new ConsoleLog(m_options->colors()));
}
m_network = new Network(m_options);

View file

@ -0,0 +1,41 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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>
*
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ILOGBACKEND_H__
#define __ILOGBACKEND_H__
#include <stdarg.h>
class ILogBackend
{
public:
virtual ~ILogBackend() {}
virtual void message(int level, const char* fmt, va_list args) = 0;
virtual void text(const char* fmt, va_list args) = 0;
};
#endif // __ILOGBACKEND_H__

112
src/log/ConsoleLog.cpp Normal file
View file

@ -0,0 +1,112 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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>
*
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef WIN32
# include <winsock2.h>
# include <malloc.h>
# include "3rdparty/winansi.h"
#endif
#include "log/ConsoleLog.h"
#include "log/Log.h"
ConsoleLog::ConsoleLog(bool colors) :
m_colors(colors)
{
}
void ConsoleLog::message(int level, const char* fmt, va_list args)
{
time_t now = time(nullptr);
tm stime;
# ifdef _WIN32
localtime_s(&stime, &now);
# else
localtime_r(&now, &stime);
# endif
const char* color = nullptr;
if (m_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;
}
}
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 ? Log::kCL_N : ""
);
vfprintf(stdout, buf, args);
fflush(stdout);
}
void ConsoleLog::text(const char* fmt, va_list args)
{
const int len = 64 + strlen(fmt) + 2;
char *buf = static_cast<char *>(alloca(len));
sprintf(buf, "%s%s\n", fmt, m_colors ? Log::kCL_N : "");
vfprintf(stdout, buf, args);
fflush(stdout);
}

43
src/log/ConsoleLog.h Normal file
View file

@ -0,0 +1,43 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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>
*
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CONSOLELOG_H__
#define __CONSOLELOG_H__
#include "interfaces/ILogBackend.h"
class ConsoleLog : public ILogBackend
{
public:
ConsoleLog(bool colors);
void message(int level, const char* fmt, va_list args) override;
void text(const char* fmt, va_list args) override;
private:
bool m_colors;
};
#endif /* __CONSOLELOG_H__ */

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

View file

@ -26,6 +26,10 @@
#include <uv.h>
#include <vector>
class ILogBackend;
class Log
@ -50,19 +54,19 @@ public:
constexpr static const char* kCL_GRAY = "\x1B[90m";
# endif
static inline Log* i() { return m_self; }
static void init(bool colors, bool background);
static inline Log* i() { return m_self; }
static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); }
static inline void init() { if (!m_self) { m_self = new Log();} }
void message(Level level, const char* fmt, ...);
void text(const char* fmt, ...);
private:
Log(bool colors, bool background);
inline Log() {}
~Log();
bool m_background;
bool m_colors;
static Log *m_self;
uv_mutex_t m_mutex;
std::vector<ILogBackend*> m_backends;
};