2021-09-01 05:49:58 -06:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
2023-01-04 05:07:55 -07:00
|
|
|
* Copyright (c) 2021-2023 SChernykh <https://github.com/SChernykh>
|
2021-09-01 05:49:58 -06:00
|
|
|
*
|
|
|
|
* 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, version 3.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "uv_util.h"
|
|
|
|
|
|
|
|
namespace p2pool {
|
|
|
|
|
|
|
|
class p2pool_api
|
|
|
|
{
|
|
|
|
public:
|
2021-10-01 17:09:42 -06:00
|
|
|
p2pool_api(const std::string& api_path, const bool local_stats);
|
2021-09-01 05:49:58 -06:00
|
|
|
~p2pool_api();
|
|
|
|
|
|
|
|
enum class Category {
|
2021-09-08 01:57:31 -06:00
|
|
|
GLOBAL,
|
2021-09-01 05:49:58 -06:00
|
|
|
NETWORK,
|
2021-09-01 08:26:56 -06:00
|
|
|
POOL,
|
2021-10-01 17:09:42 -06:00
|
|
|
LOCAL,
|
2021-09-01 05:49:58 -06:00
|
|
|
};
|
|
|
|
|
2022-05-26 10:20:29 -06:00
|
|
|
// cppcheck-suppress functionConst
|
2021-09-01 05:49:58 -06:00
|
|
|
void on_stop();
|
|
|
|
|
|
|
|
template<typename T>
|
2022-06-20 14:37:33 -06:00
|
|
|
void set(Category category, const char* filename, T&& callback) { dump_to_file_async_internal(category, filename, DumpFileCallback<T>(std::move(callback))); }
|
2021-09-01 05:49:58 -06:00
|
|
|
|
|
|
|
private:
|
2021-09-01 08:26:56 -06:00
|
|
|
void create_dir(const std::string& path);
|
|
|
|
|
2021-09-01 05:49:58 -06:00
|
|
|
static void on_dump_to_file(uv_async_t* async) { reinterpret_cast<p2pool_api*>(async->data)->dump_to_file(); }
|
|
|
|
|
|
|
|
struct DumpFileWork {
|
2022-08-24 09:28:53 -06:00
|
|
|
uv_fs_t req;
|
|
|
|
int fd;
|
2021-09-01 05:49:58 -06:00
|
|
|
|
|
|
|
std::string name;
|
2022-08-25 01:52:58 -06:00
|
|
|
std::string tmp_name;
|
2021-09-01 05:49:58 -06:00
|
|
|
std::vector<char> buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DumpFileCallbackBase
|
|
|
|
{
|
|
|
|
virtual ~DumpFileCallbackBase() {}
|
|
|
|
virtual void operator()(log::Stream&) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct DumpFileCallback : public DumpFileCallbackBase
|
|
|
|
{
|
|
|
|
explicit FORCEINLINE DumpFileCallback(T&& callback) : m_callback(std::move(callback)) {}
|
|
|
|
void operator()(log::Stream& s) override { m_callback(s); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
DumpFileCallback& operator=(DumpFileCallback&&) = delete;
|
|
|
|
|
|
|
|
T m_callback;
|
|
|
|
};
|
|
|
|
|
2022-06-20 14:37:33 -06:00
|
|
|
void dump_to_file_async_internal(Category category, const char* filename, DumpFileCallbackBase&& callback);
|
2021-09-01 05:49:58 -06:00
|
|
|
void dump_to_file();
|
|
|
|
static void on_fs_open(uv_fs_t* req);
|
|
|
|
static void on_fs_write(uv_fs_t* req);
|
|
|
|
static void on_fs_close(uv_fs_t* req);
|
2022-08-24 09:28:53 -06:00
|
|
|
static void on_fs_rename(uv_fs_t* req);
|
2023-01-13 06:43:13 -07:00
|
|
|
static void on_fs_error_cleanup(uv_fs_t* req);
|
2021-09-01 05:49:58 -06:00
|
|
|
|
|
|
|
std::string m_apiPath;
|
|
|
|
std::string m_networkPath;
|
2021-09-01 08:26:56 -06:00
|
|
|
std::string m_poolPath;
|
2021-10-01 17:09:42 -06:00
|
|
|
std::string m_localPath;
|
2021-09-01 05:49:58 -06:00
|
|
|
|
|
|
|
uv_mutex_t m_dumpDataLock;
|
2021-10-22 10:18:38 -06:00
|
|
|
unordered_map<std::string, std::vector<char>> m_dumpData;
|
2021-09-01 05:49:58 -06:00
|
|
|
|
|
|
|
uv_async_t m_dumpToFileAsync;
|
2022-08-25 01:52:58 -06:00
|
|
|
|
|
|
|
uint64_t m_counter;
|
2021-09-01 05:49:58 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace p2pool
|