From ead441f5db8322bad6f25ec4f956ed357b315929 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 23 Feb 2020 06:52:23 +0700 Subject: [PATCH] Optimize file log. --- src/base/base.cmake | 2 + src/base/io/log/FileLogWriter.cpp | 63 ++++++++++++++++++++++++++++ src/base/io/log/FileLogWriter.h | 48 +++++++++++++++++++++ src/base/io/log/backends/FileLog.cpp | 31 +++----------- src/base/io/log/backends/FileLog.h | 13 ++---- 5 files changed, 123 insertions(+), 34 deletions(-) create mode 100644 src/base/io/log/FileLogWriter.cpp create mode 100644 src/base/io/log/FileLogWriter.h diff --git a/src/base/base.cmake b/src/base/base.cmake index 1426deb57..33ec9954f 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -6,6 +6,7 @@ set(HEADERS_BASE src/base/io/json/JsonRequest.h src/base/io/log/backends/ConsoleLog.h src/base/io/log/backends/FileLog.h + src/base/io/log/FileLogWriter.h src/base/io/log/Log.h src/base/io/Watcher.h src/base/kernel/Base.h @@ -66,6 +67,7 @@ set(SOURCES_BASE src/base/io/json/JsonRequest.cpp src/base/io/log/backends/ConsoleLog.cpp src/base/io/log/backends/FileLog.cpp + src/base/io/log/FileLogWriter.cpp src/base/io/log/Log.cpp src/base/io/Watcher.cpp src/base/kernel/Base.cpp diff --git a/src/base/io/log/FileLogWriter.cpp b/src/base/io/log/FileLogWriter.cpp new file mode 100644 index 000000000..3386929a7 --- /dev/null +++ b/src/base/io/log/FileLogWriter.cpp @@ -0,0 +1,63 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 "base/io/log/FileLogWriter.h" +#include "base/kernel/Env.h" + + +#include +#include + + +bool xmrig::FileLogWriter::open(const char *fileName) +{ + assert(fileName != nullptr); + if (!fileName) { + return false; + } + + uv_fs_t req; + m_file = uv_fs_open(uv_default_loop(), &req, Env::expand(fileName), O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr); + uv_fs_req_cleanup(&req); + + return isOpen(); +} + + +bool xmrig::FileLogWriter::write(const char *data, size_t size) +{ + if (!isOpen()) { + return false; + } + + uv_buf_t buf = uv_buf_init(new char[size], size); + memcpy(buf.base, data, size); + + auto req = new uv_fs_t; + req->data = buf.base; + + uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, -1, [](uv_fs_t *req) { + delete [] static_cast(req->data); + + uv_fs_req_cleanup(req); + delete req; + }); + + return true; +} diff --git a/src/base/io/log/FileLogWriter.h b/src/base/io/log/FileLogWriter.h new file mode 100644 index 000000000..f451f2b86 --- /dev/null +++ b/src/base/io/log/FileLogWriter.h @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 XMRIG_FILELOGWRITER_H +#define XMRIG_FILELOGWRITER_H + + +#include + + +namespace xmrig { + + +class FileLogWriter +{ +public: + FileLogWriter() = default; + FileLogWriter(const char *fileName) { open(fileName); } + + inline bool isOpen() const { return m_file >= 0; } + + bool open(const char *fileName); + bool write(const char *data, size_t size); + +private: + int m_file = -1; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_FILELOGWRITER_H */ diff --git a/src/base/io/log/backends/FileLog.cpp b/src/base/io/log/backends/FileLog.cpp index 47fad1ed8..2d52d8124 100644 --- a/src/base/io/log/backends/FileLog.cpp +++ b/src/base/io/log/backends/FileLog.cpp @@ -6,8 +6,8 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2019 Spudz76 - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 @@ -25,44 +25,25 @@ #include "base/io/log/backends/FileLog.h" -#include "base/kernel/Env.h" #include #include -#include -xmrig::FileLog::FileLog(const char *fileName) +xmrig::FileLog::FileLog(const char *fileName) : + m_writer(fileName) { - uv_fs_t req; - m_file = uv_fs_open(uv_default_loop(), &req, Env::expand(fileName), O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr); - uv_fs_req_cleanup(&req); } void xmrig::FileLog::print(int, const char *line, size_t, size_t size, bool colors) { - if (m_file < 0 || colors) { + if (!m_writer.isOpen() || colors) { return; } assert(strlen(line) == size); - uv_buf_t buf = uv_buf_init(new char[size], size); - memcpy(buf.base, line, size); - - auto req = new uv_fs_t; - req->data = buf.base; - - uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, -1, FileLog::onWrite); -} - - -void xmrig::FileLog::onWrite(uv_fs_t *req) -{ - delete [] static_cast(req->data); - - uv_fs_req_cleanup(req); - delete req; + m_writer.write(line, size); } diff --git a/src/base/io/log/backends/FileLog.h b/src/base/io/log/backends/FileLog.h index 188a99aaa..95e9881a6 100644 --- a/src/base/io/log/backends/FileLog.h +++ b/src/base/io/log/backends/FileLog.h @@ -6,8 +6,8 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2019 Spudz76 - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 @@ -27,9 +27,7 @@ #define XMRIG_FILELOG_H -typedef struct uv_fs_s uv_fs_t; - - +#include "base/io/log/FileLogWriter.h" #include "base/kernel/interfaces/ILogBackend.h" @@ -41,14 +39,11 @@ class FileLog : public ILogBackend public: FileLog(const char *fileName); - protected: void print(int level, const char *line, size_t offset, size_t size, bool colors) override; private: - static void onWrite(uv_fs_t *req); - - int m_file; + FileLogWriter m_writer; };