2021-09-01 11:49:58 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
|
|
|
* Copyright (c) 2021 SChernykh <https://github.com/SChernykh>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace p2pool {
|
|
|
|
|
|
|
|
class p2pool_api
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit p2pool_api(const std::string& api_path);
|
|
|
|
~p2pool_api();
|
|
|
|
|
|
|
|
enum class Category {
|
2021-09-08 07:57:31 +00:00
|
|
|
GLOBAL,
|
2021-09-01 11:49:58 +00:00
|
|
|
NETWORK,
|
2021-09-01 14:26:56 +00:00
|
|
|
POOL,
|
2021-09-01 11:49:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void on_stop();
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void set(const Category& category, const char* filename, T&& callback) { dump_to_file_async_internal(category, filename, DumpFileCallback<T>(std::move(callback))); }
|
|
|
|
|
|
|
|
private:
|
2021-09-01 14:26:56 +00:00
|
|
|
void create_dir(const std::string& path);
|
|
|
|
|
2021-09-01 11:49:58 +00:00
|
|
|
static void on_dump_to_file(uv_async_t* async) { reinterpret_cast<p2pool_api*>(async->data)->dump_to_file(); }
|
|
|
|
|
|
|
|
struct DumpFileWork {
|
|
|
|
uv_fs_t open_req;
|
|
|
|
uv_fs_t write_req;
|
|
|
|
uv_fs_t close_req;
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
void dump_to_file_async_internal(const Category& category, const char* filename, DumpFileCallbackBase&& callback);
|
|
|
|
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);
|
|
|
|
|
|
|
|
std::string m_apiPath;
|
|
|
|
std::string m_networkPath;
|
2021-09-01 14:26:56 +00:00
|
|
|
std::string m_poolPath;
|
2021-09-01 11:49:58 +00:00
|
|
|
|
|
|
|
uv_mutex_t m_dumpDataLock;
|
|
|
|
std::unordered_map<std::string, std::vector<char>> m_dumpData;
|
|
|
|
|
|
|
|
uv_async_t m_dumpToFileAsync;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace p2pool
|