Fix wallet logging initialization (#4680)

* Adjust annoying logs

* Wrap wallet logic in a class
This commit is contained in:
Piotr Wójcik 2024-07-16 12:09:04 +02:00 committed by GitHub
commit fbcabb2fc0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 188 additions and 182 deletions

View file

@ -23,31 +23,34 @@
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
namespace namespace nano
{ {
nano::logger logger{ "wallet_daemon" }; class wallet_daemon final
{
nano::logger logger{ "wallet_daemon" };
void show_error (std::string const & message_a) public:
{ void show_error (std::string const & message_a)
{
logger.critical (nano::log::type::daemon, "{}", message_a); logger.critical (nano::log::type::daemon, "{}", message_a);
QMessageBox message (QMessageBox::Critical, "Error starting Nano", message_a.c_str ()); QMessageBox message (QMessageBox::Critical, "Error starting Nano", message_a.c_str ());
message.setModal (true); message.setModal (true);
message.show (); message.show ();
message.exec (); message.exec ();
} }
void show_help (std::string const & message_a) void show_help (std::string const & message_a)
{ {
QMessageBox message (QMessageBox::NoIcon, "Help", "see <a href=\"https://docs.nano.org/commands/command-line-interface/#launch-options\">launch options</a> "); QMessageBox message (QMessageBox::NoIcon, "Help", "see <a href=\"https://docs.nano.org/commands/command-line-interface/#launch-options\">launch options</a> ");
message.setStyleSheet ("QLabel {min-width: 450px}"); message.setStyleSheet ("QLabel {min-width: 450px}");
message.setDetailedText (message_a.c_str ()); message.setDetailedText (message_a.c_str ());
message.show (); message.show ();
message.exec (); message.exec ();
} }
nano::error write_wallet_config (nano::wallet_config & config_a, std::filesystem::path const & data_path_a) nano::error write_wallet_config (nano::wallet_config & config_a, std::filesystem::path const & data_path_a)
{ {
nano::tomlconfig wallet_config_toml; nano::tomlconfig wallet_config_toml;
auto wallet_path (nano::get_qtwallet_toml_config_path (data_path_a)); auto wallet_path (nano::get_qtwallet_toml_config_path (data_path_a));
config_a.serialize_toml (wallet_config_toml); config_a.serialize_toml (wallet_config_toml);
@ -55,10 +58,10 @@ nano::error write_wallet_config (nano::wallet_config & config_a, std::filesystem
// Write wallet config. If missing, the file is created and permissions are set. // Write wallet config. If missing, the file is created and permissions are set.
wallet_config_toml.write (wallet_path); wallet_config_toml.write (wallet_path);
return wallet_config_toml.get_error (); return wallet_config_toml.get_error ();
} }
nano::error read_wallet_config (nano::wallet_config & config_a, std::filesystem::path const & data_path_a) nano::error read_wallet_config (nano::wallet_config & config_a, std::filesystem::path const & data_path_a)
{ {
nano::tomlconfig wallet_config_toml; nano::tomlconfig wallet_config_toml;
auto wallet_path (nano::get_qtwallet_toml_config_path (data_path_a)); auto wallet_path (nano::get_qtwallet_toml_config_path (data_path_a));
if (!std::filesystem::exists (wallet_path)) if (!std::filesystem::exists (wallet_path))
@ -68,11 +71,10 @@ nano::error read_wallet_config (nano::wallet_config & config_a, std::filesystem:
wallet_config_toml.read (wallet_path); wallet_config_toml.read (wallet_path);
config_a.deserialize_toml (wallet_config_toml); config_a.deserialize_toml (wallet_config_toml);
return wallet_config_toml.get_error (); return wallet_config_toml.get_error ();
} }
}
int run_wallet (QApplication & application, int argc, char * const * argv, std::filesystem::path const & data_path, nano::node_flags const & flags) int run_wallet (QApplication & application, int argc, char * const * argv, std::filesystem::path const & data_path, nano::node_flags const & flags)
{ {
nano::logger::initialize (nano::log_config::daemon_default (), data_path, flags.config_overrides); nano::logger::initialize (nano::log_config::daemon_default (), data_path, flags.config_overrides);
logger.info (nano::log::type::daemon_wallet, "Daemon started (wallet)"); logger.info (nano::log::type::daemon_wallet, "Daemon started (wallet)");
@ -232,6 +234,8 @@ int run_wallet (QApplication & application, int argc, char * const * argv, std::
logger.info (nano::log::type::daemon_wallet, "Daemon exiting (wallet)"); logger.info (nano::log::type::daemon_wallet, "Daemon exiting (wallet)");
return result; return result;
}
};
} }
int main (int argc, char * const * argv) int main (int argc, char * const * argv)
@ -244,6 +248,8 @@ int main (int argc, char * const * argv)
QApplication application (argc, const_cast<char **> (argv)); QApplication application (argc, const_cast<char **> (argv));
nano::wallet_daemon daemon;
try try
{ {
boost::program_options::options_description description ("Command line options"); boost::program_options::options_description description ("Command line options");
@ -262,7 +268,7 @@ int main (int argc, char * const * argv)
} }
catch (boost::program_options::error const & err) catch (boost::program_options::error const & err)
{ {
show_error (err.what ()); daemon.show_error (err.what ());
return 1; return 1;
} }
boost::program_options::notify (vm); boost::program_options::notify (vm);
@ -273,7 +279,7 @@ int main (int argc, char * const * argv)
auto err (nano::network_constants::set_active_network (network->second.as<std::string> ())); auto err (nano::network_constants::set_active_network (network->second.as<std::string> ()));
if (err) if (err)
{ {
show_error (nano::network_constants::active_network_err_msg); daemon.show_error (nano::network_constants::active_network_err_msg);
std::exit (1); std::exit (1);
} }
} }
@ -293,7 +299,7 @@ int main (int argc, char * const * argv)
std::ostringstream outstream; std::ostringstream outstream;
description.print (outstream); description.print (outstream);
std::string helpstring = outstream.str (); std::string helpstring = outstream.str ();
show_help (helpstring); daemon.show_help (helpstring);
return 1; return 1;
} }
else else
@ -316,15 +322,15 @@ int main (int argc, char * const * argv)
{ {
throw std::runtime_error (flags_ec.message ()); throw std::runtime_error (flags_ec.message ());
} }
result = run_wallet (application, argc, argv, data_path, flags); result = daemon.run_wallet (application, argc, argv, data_path, flags);
} }
catch (std::exception const & e) catch (std::exception const & e)
{ {
show_error (boost::str (boost::format ("Exception while running wallet: %1%") % e.what ())); daemon.show_error (boost::str (boost::format ("Exception while running wallet: %1%") % e.what ()));
} }
catch (...) catch (...)
{ {
show_error ("Unknown exception while running wallet"); daemon.show_error ("Unknown exception while running wallet");
} }
} }
} }
@ -332,11 +338,11 @@ int main (int argc, char * const * argv)
} }
catch (std::exception const & e) catch (std::exception const & e)
{ {
show_error (boost::str (boost::format ("Exception while initializing %1%") % e.what ())); daemon.show_error (boost::str (boost::format ("Exception while initializing %1%") % e.what ()));
} }
catch (...) catch (...)
{ {
show_error (boost::str (boost::format ("Unknown exception while initializing"))); daemon.show_error (boost::str (boost::format ("Unknown exception while initializing")));
} }
return 1; return 1;
} }

View file

@ -33,7 +33,7 @@ nano::scheduler::priority::priority (nano::node & node_a, nano::stats & stats_a)
build_region (uint128_t{ 1 } << 116, uint128_t{ 1 } << 120, 2); build_region (uint128_t{ 1 } << 116, uint128_t{ 1 } << 120, 2);
minimums.push_back (uint128_t{ 1 } << 120); minimums.push_back (uint128_t{ 1 } << 120);
node.logger.info (nano::log::type::election_scheduler, "Number of buckets: {}", minimums.size ()); node.logger.debug (nano::log::type::election_scheduler, "Number of buckets: {}", minimums.size ());
for (size_t i = 0u, n = minimums.size (); i < n; ++i) for (size_t i = 0u, n = minimums.size (); i < n; ++i)
{ {