Log subsystem rewritten, to handle both color and not color logs simultaneously and overall simplicity.
This commit is contained in:
parent
0a893c2172
commit
ced25c3fa0
22 changed files with 489 additions and 559 deletions
245
src/base/io/log/Log.cpp
Normal file
245
src/base/io/log/Log.cpp
Normal file
|
@ -0,0 +1,245 @@
|
|||
/* 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 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
|
||||
* 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
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
# include <winsock2.h>
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#include <uv.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
#include "base/io/log/Log.h"
|
||||
#include "base/kernel/interfaces/ILogBackend.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
static const char *colors_map[] = {
|
||||
RED_BOLD_S, // EMERG
|
||||
RED_BOLD_S, // ALERT
|
||||
RED_BOLD_S, // CRIT
|
||||
RED_S, // ERR
|
||||
YELLOW_S, // WARNING
|
||||
WHITE_BOLD_S, // NOTICE
|
||||
nullptr, // INFO
|
||||
# ifdef WIN32
|
||||
BLACK_BOLD_S // DEBUG
|
||||
# else
|
||||
BRIGHT_BLACK_S // DEBUG
|
||||
# endif
|
||||
};
|
||||
|
||||
|
||||
|
||||
class LogPrivate
|
||||
{
|
||||
public:
|
||||
inline LogPrivate() :
|
||||
m_buf()
|
||||
{
|
||||
uv_mutex_init(&m_mutex);
|
||||
}
|
||||
|
||||
|
||||
inline ~LogPrivate()
|
||||
{
|
||||
uv_mutex_destroy(&m_mutex);
|
||||
|
||||
for (ILogBackend *backend : m_backends) {
|
||||
delete backend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline void add(ILogBackend *backend) { m_backends.push_back(backend); }
|
||||
|
||||
|
||||
void print(Log::Level level, const char *fmt, va_list args)
|
||||
{
|
||||
size_t size = 0;
|
||||
size_t offset = 0;
|
||||
|
||||
lock();
|
||||
timestamp(level, size, offset);
|
||||
color(level, size);
|
||||
|
||||
int rc = vsnprintf(m_buf + size, sizeof (m_buf) - offset - 32, fmt, args);
|
||||
if (rc < 0) {
|
||||
return unlock();
|
||||
}
|
||||
|
||||
size += static_cast<size_t>(rc);
|
||||
endl(size);
|
||||
|
||||
std::string txt(m_buf);
|
||||
size_t i;
|
||||
while ((i = txt.find(CSI)) != std::string::npos) {
|
||||
txt.erase(i, txt.find('m', i) - i + 1);
|
||||
}
|
||||
|
||||
if (!m_backends.empty()) {
|
||||
for (ILogBackend *backend : m_backends) {
|
||||
backend->print(level, m_buf, offset, size, true);
|
||||
backend->print(level, txt.c_str(), offset, txt.size(), false);
|
||||
}
|
||||
}
|
||||
else {
|
||||
fputs(txt.c_str(), stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
unlock();
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
inline void lock() { uv_mutex_lock(&m_mutex); }
|
||||
inline void unlock() { uv_mutex_unlock(&m_mutex); }
|
||||
|
||||
|
||||
inline void timestamp(Log::Level level, size_t &size, size_t &offset)
|
||||
{
|
||||
if (level == Log::NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
time_t now = time(nullptr);
|
||||
tm stime;
|
||||
|
||||
# ifdef _WIN32
|
||||
localtime_s(&stime, &now);
|
||||
# else
|
||||
localtime_r(&now, &stime);
|
||||
# endif
|
||||
|
||||
const int rc = snprintf(m_buf, sizeof(m_buf) - 1, "[%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
|
||||
);
|
||||
|
||||
if (rc > 0) {
|
||||
size = offset = static_cast<size_t>(rc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline void color(Log::Level level, size_t &size)
|
||||
{
|
||||
if (level == Log::NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char *color = colors_map[level];
|
||||
if (color == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t s = strlen(color);
|
||||
memcpy(m_buf + size, color, s);
|
||||
|
||||
size += s;
|
||||
}
|
||||
|
||||
|
||||
inline void endl(size_t &size)
|
||||
{
|
||||
# ifdef _WIN32
|
||||
memcpy(m_buf + size, CLEAR "\r\n", 7);
|
||||
size += 6;
|
||||
# else
|
||||
memcpy(m_buf + size, CLEAR "\n", 6);
|
||||
size += 5;
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
char m_buf[4096];
|
||||
std::vector<ILogBackend*> m_backends;
|
||||
uv_mutex_t m_mutex;
|
||||
};
|
||||
|
||||
|
||||
bool Log::colors = true;
|
||||
LogPrivate *Log::d = new LogPrivate();
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
|
||||
void xmrig::Log::add(ILogBackend *backend)
|
||||
{
|
||||
if (d) {
|
||||
d->add(backend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Log::destroy()
|
||||
{
|
||||
delete d;
|
||||
d = nullptr;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Log::print(const char *fmt, ...)
|
||||
{
|
||||
if (!d) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
d->print(NONE, fmt, args);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Log::print(Level level, const char *fmt, ...)
|
||||
{
|
||||
if (!d) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
d->print(level, fmt, args);
|
||||
|
||||
va_end(args);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue