From 1bfbc97c7d64882c830890e8139f3fa7dec52b53 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 23 Jun 2017 01:38:47 +0300 Subject: [PATCH] Add FileLog class. --- CMakeLists.txt | 2 + src/log/FileLog.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++++ src/log/FileLog.h | 50 +++++++++++++++++++++ src/log/SysLog.cpp | 40 +++++++++++++++++ src/log/SysLog.h | 40 +++++++++++++++++ 5 files changed, 235 insertions(+) create mode 100644 src/log/FileLog.cpp create mode 100644 src/log/FileLog.h create mode 100644 src/log/SysLog.cpp create mode 100644 src/log/SysLog.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 57def78c9..2c9d95f8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ set(HEADERS src/interfaces/ILogBackend.h src/interfaces/IWorker.h src/log/ConsoleLog.h + src/log/FileLog.h src/log/Log.h src/Mem.h src/net/Client.h @@ -48,6 +49,7 @@ set(HEADERS_CRYPTO set(SOURCES src/App.cpp src/log/ConsoleLog.cpp + src/log/FileLog.cpp src/log/Log.cpp src/Mem.cpp src/net/Client.cpp diff --git a/src/log/FileLog.cpp b/src/log/FileLog.cpp new file mode 100644 index 000000000..b08cd2038 --- /dev/null +++ b/src/log/FileLog.cpp @@ -0,0 +1,103 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * 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 . + */ + + +#include +#include +#include +#include + + +#ifdef WIN32 +# include +# include +# include "3rdparty/winansi.h" +#endif + + +#include "log/FileLog.h" + + +FileLog::FileLog(const char *fileName) +{ + uv_fs_t req; + m_file = uv_fs_open(uv_default_loop(), &req, fileName, O_CREAT | O_APPEND, 0644, nullptr); + uv_fs_req_cleanup(&req); +} + + +void FileLog::message(int level, const char* fmt, va_list args) +{ + if (m_file < 0) { + return; + } + + time_t now = time(nullptr); + tm stime; + +# ifdef _WIN32 + localtime_s(&stime, &now); +# else + localtime_r(&now, &stime); +# endif + + char *buf = static_cast(malloc(512)); + int size = snprintf(buf, 23, "[%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); + + size = vsnprintf(buf + size, 512 - size - 1, fmt, args) + size; + buf[size] = '\n'; + + write(buf, size + 1); +} + + +void FileLog::text(const char* fmt, va_list args) +{ + message(0, fmt, args); +} + + + +void FileLog::onWrite(uv_fs_t *req) +{ + free(req->data); + + uv_fs_req_cleanup(req); + free(req); +} + + +void FileLog::write(char *data, size_t size) +{ + uv_buf_t buf = uv_buf_init(data, size); + uv_fs_t *req = static_cast(malloc(sizeof(uv_fs_t))); + req->data = buf.base; + + uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, 0, FileLog::onWrite); +} diff --git a/src/log/FileLog.h b/src/log/FileLog.h new file mode 100644 index 000000000..2b3ca5d4b --- /dev/null +++ b/src/log/FileLog.h @@ -0,0 +1,50 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * 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 . + */ + +#ifndef __FILELOG_H__ +#define __FILELOG_H__ + + +#include + + +#include "interfaces/ILogBackend.h" + + +class FileLog : public ILogBackend +{ +public: + FileLog(const char *fileName); + + void message(int level, const char* fmt, va_list args) override; + void text(const char* fmt, va_list args) override; + +private: + static void onWrite(uv_fs_t *req); + + void write(char *data, size_t size); + + int m_file; +}; + +#endif /* __FILELOG_H__ */ diff --git a/src/log/SysLog.cpp b/src/log/SysLog.cpp new file mode 100644 index 000000000..88d2e55a3 --- /dev/null +++ b/src/log/SysLog.cpp @@ -0,0 +1,40 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * 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 . + */ + + +#include "log/SysLog.h" + + +SysLog::SysLog() +{ +} + + +void SysLog::message(int level, const char* fmt, va_list args) +{ +} + + +void SysLog::text(const char* fmt, va_list args) +{ +} diff --git a/src/log/SysLog.h b/src/log/SysLog.h new file mode 100644 index 000000000..5a27fe74b --- /dev/null +++ b/src/log/SysLog.h @@ -0,0 +1,40 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * 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 . + */ + +#ifndef __SYSLOG_BACKEND_H__ +#define __SYSLOG_BACKEND_H__ + + +#include "interfaces/ILogBackend.h" + + +class SysLog : public ILogBackend +{ +public: + SysLog(); + + void message(int level, const char* fmt, va_list args) override; + void text(const char* fmt, va_list args) override; +}; + +#endif /* __SYSLOG_BACKEND_H__ */