From e8355e1a1ca7b239d3015ea98f204a0adcb374f6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 8 Mar 2020 02:43:16 +0700 Subject: [PATCH] Sync changes with proxy. --- src/base/io/log/FileLogWriter.cpp | 43 ++++++++++++++++++++++++++----- src/base/io/log/FileLogWriter.h | 1 + 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/base/io/log/FileLogWriter.cpp b/src/base/io/log/FileLogWriter.cpp index 3386929a7..8ae31273c 100644 --- a/src/base/io/log/FileLogWriter.cpp +++ b/src/base/io/log/FileLogWriter.cpp @@ -25,6 +25,24 @@ #include +namespace xmrig { + + +static void fsWriteCallback(uv_fs_t *req) +{ + delete [] static_cast(req->data); + + uv_fs_req_cleanup(req); + delete req; +} + + +static const char *kNewLine = "\n"; + + +} // namespace xmrig + + bool xmrig::FileLogWriter::open(const char *fileName) { assert(fileName != nullptr); @@ -52,12 +70,25 @@ bool xmrig::FileLogWriter::write(const char *data, size_t 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; - }); + uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, -1, fsWriteCallback); + + return true; +} + + +bool xmrig::FileLogWriter::writeLine(const char *data, size_t size) +{ + uv_buf_t buf[2] = { + uv_buf_init(new char[size], size), + uv_buf_init(const_cast(kNewLine), 1) + }; + + memcpy(buf[0].base, data, size); + + auto req = new uv_fs_t; + req->data = buf[0].base; + + uv_fs_write(uv_default_loop(), req, m_file, buf, 2, -1, fsWriteCallback); return true; } diff --git a/src/base/io/log/FileLogWriter.h b/src/base/io/log/FileLogWriter.h index f451f2b86..5dca4c58c 100644 --- a/src/base/io/log/FileLogWriter.h +++ b/src/base/io/log/FileLogWriter.h @@ -36,6 +36,7 @@ public: bool open(const char *fileName); bool write(const char *data, size_t size); + bool writeLine(const char *data, size_t size); private: int m_file = -1;