Fixed possible out of order write to log file.
This commit is contained in:
parent
4a8e7510e1
commit
6f8ffb7660
2 changed files with 36 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
||||||
/* XMRig
|
/* XMRig
|
||||||
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
|
||||||
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -37,9 +37,6 @@ static void fsWriteCallback(uv_fs_t *req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *kNewLine = "\n";
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace xmrig
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,11 +47,24 @@ bool xmrig::FileLogWriter::open(const char *fileName)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uv_fs_t req;
|
uv_fs_t req{};
|
||||||
m_file = uv_fs_open(uv_default_loop(), &req, Env::expand(fileName), O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr);
|
const auto path = Env::expand(fileName);
|
||||||
|
m_file = uv_fs_open(uv_default_loop(), &req, path, O_CREAT | O_WRONLY, 0644, nullptr);
|
||||||
|
|
||||||
|
if (req.result < 0 || !isOpen()) {
|
||||||
|
uv_fs_req_cleanup(&req);
|
||||||
|
m_file = -1;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
uv_fs_req_cleanup(&req);
|
uv_fs_req_cleanup(&req);
|
||||||
|
|
||||||
return isOpen();
|
uv_fs_stat(uv_default_loop(), &req, path, nullptr);
|
||||||
|
m_pos = req.statbuf.st_size;
|
||||||
|
uv_fs_req_cleanup(&req);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,7 +80,8 @@ bool xmrig::FileLogWriter::write(const char *data, size_t size)
|
||||||
auto req = new uv_fs_t;
|
auto req = new uv_fs_t;
|
||||||
req->data = buf.base;
|
req->data = buf.base;
|
||||||
|
|
||||||
uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, -1, fsWriteCallback);
|
uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, m_pos, fsWriteCallback);
|
||||||
|
m_pos += size;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -78,9 +89,9 @@ bool xmrig::FileLogWriter::write(const char *data, size_t size)
|
||||||
|
|
||||||
bool xmrig::FileLogWriter::writeLine(const char *data, size_t size)
|
bool xmrig::FileLogWriter::writeLine(const char *data, size_t size)
|
||||||
{
|
{
|
||||||
uv_buf_t buf[2] = {
|
const uv_buf_t buf[2] = {
|
||||||
uv_buf_init(new char[size], size),
|
uv_buf_init(new char[size], size),
|
||||||
uv_buf_init(const_cast<char *>(kNewLine), 1)
|
uv_buf_init(m_endl, sizeof(m_endl) - 1)
|
||||||
};
|
};
|
||||||
|
|
||||||
memcpy(buf[0].base, data, size);
|
memcpy(buf[0].base, data, size);
|
||||||
|
@ -88,7 +99,8 @@ bool xmrig::FileLogWriter::writeLine(const char *data, size_t size)
|
||||||
auto req = new uv_fs_t;
|
auto req = new uv_fs_t;
|
||||||
req->data = buf[0].base;
|
req->data = buf[0].base;
|
||||||
|
|
||||||
uv_fs_write(uv_default_loop(), req, m_file, buf, 2, -1, fsWriteCallback);
|
uv_fs_write(uv_default_loop(), req, m_file, buf, 2, m_pos, fsWriteCallback);
|
||||||
|
m_pos += (buf[0].len + buf[1].len);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/* XMRig
|
/* XMRig
|
||||||
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
|
||||||
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
@ -33,13 +34,21 @@ public:
|
||||||
FileLogWriter(const char *fileName) { open(fileName); }
|
FileLogWriter(const char *fileName) { open(fileName); }
|
||||||
|
|
||||||
inline bool isOpen() const { return m_file >= 0; }
|
inline bool isOpen() const { return m_file >= 0; }
|
||||||
|
inline int64_t pos() const { return m_pos; }
|
||||||
|
|
||||||
bool open(const char *fileName);
|
bool open(const char *fileName);
|
||||||
bool write(const char *data, size_t size);
|
bool write(const char *data, size_t size);
|
||||||
bool writeLine(const char *data, size_t size);
|
bool writeLine(const char *data, size_t size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_file = -1;
|
# ifdef XMRIG_OS_WIN
|
||||||
|
char m_endl[3] = "\r\n";
|
||||||
|
# else
|
||||||
|
char m_endl[2] = "\n";
|
||||||
|
# endif
|
||||||
|
|
||||||
|
int m_file = -1;
|
||||||
|
int64_t m_pos = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue