Added real graceful exit.

This commit is contained in:
XMRig 2019-03-16 00:44:15 +07:00
parent 1e62943010
commit ba68fb6c53
41 changed files with 391 additions and 303 deletions

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -22,26 +23,42 @@
*/
#include "base/tools/Handle.h"
#include "common/Console.h"
#include "interfaces/IConsoleListener.h"
Console::Console(IConsoleListener *listener)
xmrig::Console::Console(IConsoleListener *listener)
: m_listener(listener)
{
m_tty.data = this;
uv_tty_init(uv_default_loop(), &m_tty, 0, 1);
m_tty = new uv_tty_t;
if (!uv_is_readable(reinterpret_cast<uv_stream_t*>(&m_tty))) {
m_tty->data = this;
uv_tty_init(uv_default_loop(), m_tty, 0, 1);
if (!uv_is_readable(reinterpret_cast<uv_stream_t*>(m_tty))) {
return;
}
uv_tty_set_mode(&m_tty, UV_TTY_MODE_RAW);
uv_read_start(reinterpret_cast<uv_stream_t*>(&m_tty), Console::onAllocBuffer, Console::onRead);
uv_tty_set_mode(m_tty, UV_TTY_MODE_RAW);
uv_read_start(reinterpret_cast<uv_stream_t*>(m_tty), Console::onAllocBuffer, Console::onRead);
}
void Console::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
xmrig::Console::~Console()
{
stop();
}
void xmrig::Console::stop()
{
Handle::close(m_tty);
m_tty = nullptr;
}
void xmrig::Console::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf)
{
auto console = static_cast<Console*>(handle->data);
buf->len = 1;
@ -49,7 +66,7 @@ void Console::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t
}
void Console::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
void xmrig::Console::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
{
if (nread < 0) {
return uv_close(reinterpret_cast<uv_handle_t*>(stream), nullptr);

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -21,13 +22,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CONSOLE_H__
#define __CONSOLE_H__
#ifndef XMRIG_CONSOLE_H
#define XMRIG_CONSOLE_H
#include <uv.h>
namespace xmrig {
class IConsoleListener;
@ -35,6 +40,9 @@ class Console
{
public:
Console(IConsoleListener *listener);
~Console();
void stop();
private:
static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
@ -42,8 +50,11 @@ private:
char m_buf[1];
IConsoleListener *m_listener;
uv_tty_t m_tty;
uv_tty_t *m_tty;
};
#endif /* __CONSOLE_H__ */
} /* namespace xmrig */
#endif /* XMRIG_CONSOLE_H */

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -27,6 +28,7 @@
#include "api/Api.h"
#include "base/tools/Handle.h"
#include "common/api/Httpd.h"
#include "common/api/HttpReply.h"
#include "common/api/HttpRequest.h"
@ -41,14 +43,15 @@ Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) :
m_port(port),
m_daemon(nullptr)
{
uv_timer_init(uv_default_loop(), &m_timer);
m_timer.data = this;
m_timer = new uv_timer_t;
uv_timer_init(uv_default_loop(), m_timer);
m_timer->data = this;
}
Httpd::~Httpd()
{
uv_timer_stop(&m_timer);
stop();
if (m_daemon) {
MHD_stop_daemon(m_daemon);
@ -82,15 +85,22 @@ bool Httpd::start()
}
# if MHD_VERSION >= 0x00093900
uv_timer_start(&m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval);
uv_timer_start(m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval);
# else
uv_timer_start(&m_timer, Httpd::onTimer, kActiveInterval, kActiveInterval);
uv_timer_start(m_timer, Httpd::onTimer, kActiveInterval, kActiveInterval);
# endif
return true;
}
void Httpd::stop()
{
xmrig::Handle::close(m_timer);
m_timer = nullptr;
}
int Httpd::process(xmrig::HttpRequest &req)
{
xmrig::HttpReply reply;
@ -115,11 +125,11 @@ void Httpd::run()
# if MHD_VERSION >= 0x00093900
const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS);
if (m_idle && info->num_connections) {
uv_timer_set_repeat(&m_timer, kActiveInterval);
uv_timer_set_repeat(m_timer, kActiveInterval);
m_idle = false;
}
else if (!m_idle && !info->num_connections) {
uv_timer_set_repeat(&m_timer, kIdleInterval);
uv_timer_set_repeat(m_timer, kIdleInterval);
m_idle = true;
}
# endif

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -21,8 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __HTTPD_H__
#define __HTTPD_H__
#ifndef XMRIG_HTTPD_H
#define XMRIG_HTTPD_H
#include <uv.h>
@ -47,6 +48,7 @@ public:
Httpd(int port, const char *accessToken, bool IPv6, bool restricted);
~Httpd();
bool start();
void stop();
private:
constexpr static const int kIdleInterval = 200;
@ -64,7 +66,7 @@ private:
const char *m_accessToken;
const int m_port;
MHD_Daemon *m_daemon;
uv_timer_t m_timer;
uv_timer_t *m_timer;
};
#endif /* __HTTPD_H__ */
#endif /* XMRIG_HTTPD_H */

View file

@ -4,8 +4,9 @@
* 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 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -21,17 +22,23 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ICONSOLELISTENER_H__
#define __ICONSOLELISTENER_H__
#ifndef XMRIG_ICONSOLELISTENER_H
#define XMRIG_ICONSOLELISTENER_H
namespace xmrig {
class IConsoleListener
{
public:
virtual ~IConsoleListener() {}
virtual ~IConsoleListener() = default;
virtual void onConsoleCommand(char command) = 0;
};
#endif // __ICONSOLELISTENER_H__
} /* namespace xmrig */
#endif // XMRIG_ICONSOLELISTENER_H

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -21,14 +22,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ILOGBACKEND_H__
#define __ILOGBACKEND_H__
#ifndef XMRIG_ILOGBACKEND_H
#define XMRIG_ILOGBACKEND_H
#include <stdarg.h>
#include <stddef.h>
namespace xmrig {
class ILogBackend
{
public:
@ -46,11 +50,14 @@ public:
constexpr static const size_t kBufferSize = 512;
# endif
virtual ~ILogBackend() {}
virtual ~ILogBackend() = default;
virtual void message(Level level, const char* fmt, va_list args) = 0;
virtual void text(const char* fmt, va_list args) = 0;
};
#endif // __ILOGBACKEND_H__
} /* namespace xmrig */
#endif // XMRIG_ILOGBACKEND_H

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -38,12 +39,12 @@
#include "common/log/Log.h"
BasicLog::BasicLog()
xmrig::BasicLog::BasicLog()
{
}
void BasicLog::message(Level level, const char* fmt, va_list args)
void xmrig::BasicLog::message(Level level, const char* fmt, va_list args)
{
time_t now = time(nullptr);
tm stime;
@ -70,7 +71,7 @@ void BasicLog::message(Level level, const char* fmt, va_list args)
}
void BasicLog::text(const char* fmt, va_list args)
void xmrig::BasicLog::text(const char* fmt, va_list args)
{
snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(false));
@ -78,7 +79,7 @@ void BasicLog::text(const char* fmt, va_list args)
}
void BasicLog::print(va_list args)
void xmrig::BasicLog::print(va_list args)
{
if (vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args) <= 0) {
return;

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -21,8 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __BASICLOG_H__
#define __BASICLOG_H__
#ifndef XMRIG_BASICLOG_H
#define XMRIG_BASICLOG_H
#include <uv.h>
@ -32,8 +33,6 @@
namespace xmrig {
class Controller;
}
class BasicLog : public ILogBackend
@ -52,4 +51,7 @@ private:
char m_fmt[256];
};
#endif /* __BASICLOG_H__ */
} /* namespace xmrig */
#endif /* XMRIG_BASICLOG_H */

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -34,24 +35,24 @@
#endif
#include "base/tools/Handle.h"
#include "common/log/ConsoleLog.h"
#include "common/log/Log.h"
#include "core/Config.h"
#include "core/Controller.h"
ConsoleLog::ConsoleLog(xmrig::Controller *controller) :
m_stream(nullptr),
m_controller(controller)
xmrig::ConsoleLog::ConsoleLog() :
m_stream(nullptr)
{
if (uv_tty_init(uv_default_loop(), &m_tty, 1, 0) < 0) {
m_tty = new uv_tty_t;
if (uv_tty_init(uv_default_loop(), m_tty, 1, 0) < 0) {
Log::colors = false;
return;
}
uv_tty_set_mode(&m_tty, UV_TTY_MODE_NORMAL);
uv_tty_set_mode(m_tty, UV_TTY_MODE_NORMAL);
m_uvBuf.base = m_buf;
m_stream = reinterpret_cast<uv_stream_t*>(&m_tty);
m_stream = reinterpret_cast<uv_stream_t*>(m_tty);
# ifdef WIN32
HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
@ -66,7 +67,13 @@ ConsoleLog::ConsoleLog(xmrig::Controller *controller) :
}
void ConsoleLog::message(Level level, const char* fmt, va_list args)
xmrig::ConsoleLog::~ConsoleLog()
{
Handle::close(m_tty);
}
void xmrig::ConsoleLog::message(Level level, const char* fmt, va_list args)
{
time_t now = time(nullptr);
tm stime;
@ -77,8 +84,6 @@ void ConsoleLog::message(Level level, const char* fmt, va_list args)
localtime_r(&now, &stime);
# endif
const bool isColors = m_controller->config()->isColors();
snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s",
stime.tm_year + 1900,
stime.tm_mon + 1,
@ -86,24 +91,24 @@ void ConsoleLog::message(Level level, const char* fmt, va_list args)
stime.tm_hour,
stime.tm_min,
stime.tm_sec,
Log::colorByLevel(level, isColors),
Log::colorByLevel(level, Log::colors),
fmt,
Log::endl(isColors)
Log::endl(Log::colors)
);
print(args);
}
void ConsoleLog::text(const char* fmt, va_list args)
void xmrig::ConsoleLog::text(const char* fmt, va_list args)
{
snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(m_controller->config()->isColors()));
snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(Log::colors));
print(args);
}
bool ConsoleLog::isWritable() const
bool xmrig::ConsoleLog::isWritable() const
{
if (!m_stream || uv_is_writable(m_stream) != 1) {
return false;
@ -114,7 +119,7 @@ bool ConsoleLog::isWritable() const
}
void ConsoleLog::print(va_list args)
void xmrig::ConsoleLog::print(va_list args)
{
m_uvBuf.len = vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args);
if (m_uvBuf.len <= 0) {

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -21,8 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CONSOLELOG_H__
#define __CONSOLELOG_H__
#ifndef XMRIG_CONSOLELOG_H
#define XMRIG_CONSOLELOG_H
#include <uv.h>
@ -32,15 +33,15 @@
namespace xmrig {
class Controller;
}
class ConsoleLog : public ILogBackend
{
public:
ConsoleLog(xmrig::Controller *controller);
ConsoleLog();
~ConsoleLog() override;
protected:
void message(Level level, const char *fmt, va_list args) override;
void text(const char *fmt, va_list args) override;
@ -52,8 +53,11 @@ private:
char m_fmt[256];
uv_buf_t m_uvBuf;
uv_stream_t *m_stream;
uv_tty_t m_tty;
xmrig::Controller *m_controller;
uv_tty_t *m_tty;
};
#endif /* __CONSOLELOG_H__ */
} /* namespace xmrig */
#endif /* XMRIG_CONSOLELOG_H */

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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,12 +32,9 @@
#include "common/log/FileLog.h"
#include "common/log/Log.h"
#include "core/Config.h"
#include "core/Controller.h"
FileLog::FileLog(xmrig::Controller *controller, const char *fileName) :
m_controller(controller)
xmrig::FileLog::FileLog(const char *fileName)
{
uv_fs_t req;
m_file = uv_fs_open(uv_default_loop(), &req, fileName, O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr);
@ -44,7 +42,7 @@ FileLog::FileLog(xmrig::Controller *controller, const char *fileName) :
}
void FileLog::message(Level level, const char* fmt, va_list args)
void xmrig::FileLog::message(Level level, const char* fmt, va_list args)
{
if (m_file < 0) {
return;
@ -59,8 +57,6 @@ void FileLog::message(Level level, const char* fmt, va_list args)
localtime_r(&now, &stime);
# endif
const bool isColors = m_controller->config()->isColors();
snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s",
stime.tm_year + 1900,
stime.tm_mon + 1,
@ -68,9 +64,9 @@ void FileLog::message(Level level, const char* fmt, va_list args)
stime.tm_hour,
stime.tm_min,
stime.tm_sec,
Log::colorByLevel(level, isColors),
Log::colorByLevel(level, Log::colors),
fmt,
Log::endl(isColors)
Log::endl(Log::colors)
);
char *buf = new char[kBufferSize];
@ -80,13 +76,13 @@ void FileLog::message(Level level, const char* fmt, va_list args)
}
void FileLog::text(const char* fmt, va_list args)
void xmrig::FileLog::text(const char* fmt, va_list args)
{
message(INFO, fmt, args);
}
void FileLog::onWrite(uv_fs_t *req)
void xmrig::FileLog::onWrite(uv_fs_t *req)
{
delete [] static_cast<char *>(req->data);
@ -95,7 +91,7 @@ void FileLog::onWrite(uv_fs_t *req)
}
void FileLog::write(char *data, size_t size)
void xmrig::FileLog::write(char *data, size_t size)
{
uv_buf_t buf = uv_buf_init(data, (unsigned int) size);
uv_fs_t *req = new uv_fs_t;

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -21,8 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __FILELOG_H__
#define __FILELOG_H__
#ifndef XMRIG_FILELOG_H
#define XMRIG_FILELOG_H
#include <uv.h>
@ -32,14 +33,12 @@
namespace xmrig {
class Controller;
}
class FileLog : public ILogBackend
{
public:
FileLog(xmrig::Controller *controller, const char *fileName);
FileLog(const char *fileName);
void message(Level level, const char* fmt, va_list args) override;
void text(const char* fmt, va_list args) override;
@ -51,7 +50,10 @@ private:
char m_fmt[256];
int m_file;
xmrig::Controller *m_controller;
};
#endif /* __FILELOG_H__ */
} /* namespace xmrig */
#endif /* XMRIG_FILELOG_H */

View file

@ -35,6 +35,8 @@
#include "common/log/Log.h"
namespace xmrig {
Log *Log::m_self = nullptr;
bool Log::colors = true;
@ -51,8 +53,10 @@ static const char *color[5] = {
# endif
};
} /* namespace xmrig */
void Log::message(ILogBackend::Level level, const char* fmt, ...)
void xmrig::Log::message(ILogBackend::Level level, const char* fmt, ...)
{
uv_mutex_lock(&m_mutex);
@ -72,7 +76,7 @@ void Log::message(ILogBackend::Level level, const char* fmt, ...)
}
void Log::text(const char* fmt, ...)
void xmrig::Log::text(const char* fmt, ...)
{
uv_mutex_lock(&m_mutex);
@ -92,7 +96,7 @@ void Log::text(const char* fmt, ...)
}
const char *Log::colorByLevel(ILogBackend::Level level, bool isColors)
const char *xmrig::Log::colorByLevel(ILogBackend::Level level, bool isColors)
{
if (!isColors) {
return "";
@ -102,7 +106,7 @@ const char *Log::colorByLevel(ILogBackend::Level level, bool isColors)
}
const char *Log::endl(bool isColors)
const char *xmrig::Log::endl(bool isColors)
{
# ifdef _WIN32
return isColors ? "\x1B[0m\r\n" : "\r\n";
@ -112,7 +116,7 @@ const char *Log::endl(bool isColors)
}
void Log::defaultInit()
void xmrig::Log::defaultInit()
{
m_self = new Log();
@ -120,8 +124,10 @@ void Log::defaultInit()
}
Log::~Log()
xmrig::Log::~Log()
{
m_self = nullptr;
for (auto backend : m_backends) {
delete backend;
}

View file

@ -34,6 +34,9 @@
#include "common/interfaces/ILogBackend.h"
namespace xmrig {
class Log
{
public:
@ -68,6 +71,9 @@ private:
};
} /* namespace xmrig */
#define RED_BOLD(x) "\x1B[1;31m" x "\x1B[0m"
#define RED(x) "\x1B[0;31m" x "\x1B[0m"
#define GREEN_BOLD(x) "\x1B[1;32m" x "\x1B[0m"
@ -83,20 +89,20 @@ private:
#define GRAY(x) "\x1B[1;30m" x "\x1B[0m"
#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__)
#define LOG_ERR(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::ERR, x, ##__VA_ARGS__)
#define LOG_WARN(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::WARNING, x, ##__VA_ARGS__)
#define LOG_NOTICE(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::NOTICE, x, ##__VA_ARGS__)
#define LOG_INFO(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::INFO, x, ##__VA_ARGS__)
#ifdef APP_DEBUG
# define LOG_DEBUG(x, ...) Log::i()->message(ILogBackend::DEBUG, x, ##__VA_ARGS__)
# define LOG_DEBUG(x, ...) xmrig::Log::i()->message(xmrig::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(ILogBackend::ERR, x, ##__VA_ARGS__)
# define LOG_DEBUG_WARN(x, ...) Log::i()->message(ILogBackend::WARNING, x, ##__VA_ARGS__)
# define LOG_DEBUG_ERR(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::ERR, x, ##__VA_ARGS__)
# define LOG_DEBUG_WARN(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::WARNING, x, ##__VA_ARGS__)
#else
# define LOG_DEBUG_ERR(x, ...)
# define LOG_DEBUG_WARN(x, ...)

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -29,19 +30,19 @@
#include "version.h"
SysLog::SysLog()
xmrig::SysLog::SysLog()
{
openlog(APP_ID, LOG_PID, LOG_USER);
}
void SysLog::message(Level level, const char *fmt, va_list args)
void xmrig::SysLog::message(Level level, const char *fmt, va_list args)
{
vsyslog(static_cast<int>(level), fmt, args);
}
void SysLog::text(const char *fmt, va_list args)
void xmrig::SysLog::text(const char *fmt, va_list args)
{
vsyslog(LOG_INFO, fmt, args);
}

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 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
@ -21,13 +22,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SYSLOG_H__
#define __SYSLOG_H__
#ifndef XMRIG_SYSLOG_H
#define XMRIG_SYSLOG_H
#include "common/interfaces/ILogBackend.h"
namespace xmrig {
class SysLog : public ILogBackend
{
public:
@ -37,4 +41,8 @@ public:
void text(const char *fmt, va_list args) override;
};
#endif /* __SYSLOG_BACKEND_H__ */
} /* namespace xmrig */
#endif /* XMRIG_SYSLOG_H */