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
@ -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 */