mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Boost log integration. (#37)
This commit is contained in:
88
src/conf.cpp
88
src/conf.cpp
@@ -1,6 +1,7 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <unordered_set>
|
||||
#include <experimental/filesystem>
|
||||
#include <sodium.h>
|
||||
#include <rapidjson/document.h>
|
||||
@@ -69,7 +70,7 @@ int create_contract()
|
||||
{
|
||||
if (std::experimental::filesystem::exists(ctx.contractDir))
|
||||
{
|
||||
std::cerr << "Contract dir already exists. Cannot create contract at the same location.\n";
|
||||
std::cout << "Contract dir already exists. Cannot create contract at the same location.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -92,6 +93,13 @@ int create_contract()
|
||||
cfg.pubmaxsize = 65536;
|
||||
cfg.pubmaxcpm = 100;
|
||||
|
||||
#ifndef NDEBUG
|
||||
cfg.loglevel = "debug";
|
||||
#else
|
||||
cfg.loglevel = "warn";
|
||||
#endif
|
||||
cfg.loggers.emplace("console");
|
||||
|
||||
//Save the default settings into the config file.
|
||||
if (save_config() != 0)
|
||||
return -1;
|
||||
@@ -132,12 +140,12 @@ int load_config()
|
||||
rapidjson::Document d;
|
||||
if (d.ParseStream(isw).HasParseError())
|
||||
{
|
||||
std::cerr << "Invalid config file format. Parser error at position " << d.GetErrorOffset() << std::endl;
|
||||
std::cout << "Invalid config file format. Parser error at position " << d.GetErrorOffset() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
else if (is_schema_valid(d) != 0)
|
||||
{
|
||||
std::cerr << "Invalid config file format.\n";
|
||||
std::cout << "Invalid config file format.\n";
|
||||
return -1;
|
||||
}
|
||||
ifs.close();
|
||||
@@ -146,7 +154,7 @@ int load_config()
|
||||
std::string_view cfgversion = util::getsv(d["version"]);
|
||||
if (cfgversion.empty())
|
||||
{
|
||||
std::cerr << "Contract config version missing.\n";
|
||||
std::cout << "Contract config version missing.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -154,14 +162,14 @@ int load_config()
|
||||
int verresult = util::version_compare(std::string(cfgversion), std::string(util::MIN_CONTRACT_VERSION));
|
||||
if (verresult == -1)
|
||||
{
|
||||
std::cerr << "Contract version too old. Minimum "
|
||||
std::cout << "Contract version too old. Minimum "
|
||||
<< util::MIN_CONTRACT_VERSION << " required. "
|
||||
<< cfgversion << " found.\n";
|
||||
return -1;
|
||||
}
|
||||
else if (verresult == -2)
|
||||
{
|
||||
std::cerr << "Malformed version string.\n";
|
||||
std::cout << "Malformed version string.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -187,6 +195,11 @@ int load_config()
|
||||
cfg.pubmaxsize = d["pubmaxsize"].GetInt();
|
||||
cfg.pubmaxcpm = d["pubmaxcpm"].GetInt();
|
||||
|
||||
cfg.loglevel = d["loglevel"].GetString();
|
||||
cfg.loggers.clear();
|
||||
for (auto &v : d["loggers"].GetArray())
|
||||
cfg.loggers.emplace(v.GetString());
|
||||
|
||||
// Convert the hex keys to binary and keep for later use.
|
||||
if (hexpair_to_bin() != 0)
|
||||
return -1;
|
||||
@@ -237,6 +250,16 @@ int save_config()
|
||||
d.AddMember("pubmaxsize", cfg.pubmaxsize, allocator);
|
||||
d.AddMember("pubmaxcpm", cfg.pubmaxcpm, allocator);
|
||||
|
||||
d.AddMember("loglevel", rapidjson::StringRef(cfg.loglevel.data()), allocator);
|
||||
rapidjson::Value loggers(rapidjson::kArrayType);
|
||||
for (const std::string &logger : cfg.loggers)
|
||||
{
|
||||
rapidjson::Value v;
|
||||
v.SetString(rapidjson::StringRef(logger.data()), allocator);
|
||||
loggers.PushBack(v, allocator);
|
||||
}
|
||||
d.AddMember("loggers", loggers, allocator);
|
||||
|
||||
// Write the json doc to file.
|
||||
|
||||
std::ofstream ofs(ctx.configFile);
|
||||
@@ -245,7 +268,7 @@ int save_config()
|
||||
rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(osw);
|
||||
if (!d.Accept(writer))
|
||||
{
|
||||
std::cerr << "Writing to config file failed. " << ctx.configFile << std::endl;
|
||||
std::cout << "Writing to config file failed. " << ctx.configFile << std::endl;
|
||||
return -1;
|
||||
}
|
||||
ofs.close();
|
||||
@@ -265,7 +288,7 @@ int binpair_to_hex()
|
||||
reinterpret_cast<const unsigned char *>(cfg.pubkey.data()),
|
||||
cfg.pubkey.length()) != 0)
|
||||
{
|
||||
std::cerr << "Error encoding public key bytes.\n";
|
||||
std::cout << "Error encoding public key bytes.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -274,7 +297,7 @@ int binpair_to_hex()
|
||||
reinterpret_cast<const unsigned char *>(cfg.seckey.data()),
|
||||
cfg.seckey.length()) != 0)
|
||||
{
|
||||
std::cerr << "Error encoding secret key bytes.\n";
|
||||
std::cout << "Error encoding secret key bytes.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -294,7 +317,7 @@ int hexpair_to_bin()
|
||||
cfg.pubkey.length(),
|
||||
cfg.pubkeyhex) != 0)
|
||||
{
|
||||
std::cerr << "Error decoding hex public key.\n";
|
||||
std::cout << "Error decoding hex public key.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -304,10 +327,10 @@ int hexpair_to_bin()
|
||||
cfg.seckey.length(),
|
||||
cfg.seckeyhex) != 0)
|
||||
{
|
||||
std::cerr << "Error decoding hex secret key.\n";
|
||||
std::cout << "Error decoding hex secret key.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -322,22 +345,41 @@ int validate_config()
|
||||
// We also check for key pair validity as well in the below code.
|
||||
if (cfg.pubkeyhex.empty() || cfg.seckeyhex.empty())
|
||||
{
|
||||
std::cerr << "Signing keys missing. Run with 'rekey' to generate new keys.\n";
|
||||
std::cout << "Signing keys missing. Run with 'rekey' to generate new keys.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Other required fields.
|
||||
if (cfg.binary.empty() || cfg.listenip.empty() ||
|
||||
cfg.peerport == 0 || cfg.roundtime == 0 || cfg.pubport == 0 || cfg.pubmaxsize == 0 || cfg.pubmaxcpm == 0)
|
||||
cfg.peerport == 0 || cfg.roundtime == 0 || cfg.pubport == 0 || cfg.pubmaxsize == 0 || cfg.pubmaxcpm == 0 ||
|
||||
cfg.loglevel.empty() || cfg.loggers.empty())
|
||||
{
|
||||
std::cerr << "Required configuration fields missing at " << ctx.configFile << std::endl;
|
||||
std::cout << "Required configuration fields missing at " << ctx.configFile << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Log settings
|
||||
const std::unordered_set<std::string> valid_loglevels({"debug", "info", "warn", "error"});
|
||||
if (valid_loglevels.count(cfg.loglevel) != 1)
|
||||
{
|
||||
std::cout << "Invalid loglevel configured. Valid values: debug|info|warn|error\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
const std::unordered_set<std::string> valid_loggers({"console", "file"});
|
||||
for (const std::string &logger : cfg.loggers)
|
||||
{
|
||||
if (valid_loggers.count(logger) != 1)
|
||||
{
|
||||
std::cout << "Invalid logger. Valid values: console|file\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Check whether the contract binary actually exists.
|
||||
if (!std::experimental::filesystem::exists(cfg.binary))
|
||||
{
|
||||
std::cerr << "Contract binary does not exist: " << cfg.binary << std::endl;
|
||||
std::cout << "Contract binary does not exist: " << cfg.binary << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -346,7 +388,7 @@ int validate_config()
|
||||
std::string sighex = crypto::sign_hex(msg, cfg.seckeyhex);
|
||||
if (crypto::verify_hex(msg, sighex, cfg.pubkeyhex) != 0)
|
||||
{
|
||||
std::cerr << "Invalid signing keys. Run with 'rekey' to generate new keys.\n";
|
||||
std::cout << "Invalid signing keys. Run with 'rekey' to generate new keys.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -366,7 +408,7 @@ int validate_contract_dir_paths()
|
||||
{
|
||||
if (!std::experimental::filesystem::exists(dir))
|
||||
{
|
||||
std::cerr << dir << " does not exist.\n";
|
||||
std::cout << dir << " does not exist.\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -385,7 +427,8 @@ int is_schema_valid(rapidjson::Document &d)
|
||||
"{"
|
||||
"\"type\": \"object\","
|
||||
"\"required\": [ \"version\", \"pubkeyhex\", \"seckeyhex\", \"binary\", \"binargs\", \"listenip\""
|
||||
", \"peers\", \"unl\", \"peerport\", \"roundtime\", \"pubport\", \"pubmaxsize\", \"pubmaxcpm\" ],"
|
||||
", \"peers\", \"unl\", \"peerport\", \"roundtime\", \"pubport\", \"pubmaxsize\", \"pubmaxcpm\""
|
||||
", \"loglevel\", \"loggers\" ],"
|
||||
"\"properties\": {"
|
||||
"\"version\": { \"type\": \"string\" },"
|
||||
"\"pubkeyhex\": { \"type\": \"string\" },"
|
||||
@@ -405,7 +448,12 @@ int is_schema_valid(rapidjson::Document &d)
|
||||
"\"roundtime\": { \"type\": \"integer\" },"
|
||||
"\"pubport\": { \"type\": \"integer\" },"
|
||||
"\"pubmaxsize\": { \"type\": \"integer\" },"
|
||||
"\"pubmaxcpm\": { \"type\": \"integer\" }"
|
||||
"\"pubmaxcpm\": { \"type\": \"integer\" },"
|
||||
"\"loglevel\": { \"type\": \"string\" },"
|
||||
"\"loggers\": {"
|
||||
"\"type\": \"array\","
|
||||
"\"items\": { \"type\": \"string\" }"
|
||||
"},"
|
||||
"}"
|
||||
"}";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user