diff --git a/nano/core_test/core_test_main.cc b/nano/core_test/core_test_main.cc index 51e8e81669..f165199a83 100644 --- a/nano/core_test/core_test_main.cc +++ b/nano/core_test/core_test_main.cc @@ -19,7 +19,7 @@ void force_nano_dev_network (); GTEST_API_ int main (int argc, char ** argv) { - nano::nlogger::initialize_for_tests (nano::load_log_config (nano::log_config::tests_default ())); + nano::nlogger::initialize_for_tests (nano::log_config::tests_default ()); nano::set_file_descriptor_limit (OPEN_FILE_DESCRIPTORS_LIMIT); nano::force_nano_dev_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; diff --git a/nano/lib/config.cpp b/nano/lib/config.cpp index f722b0e66a..d8b3a60d74 100644 --- a/nano/lib/config.cpp +++ b/nano/lib/config.cpp @@ -386,40 +386,29 @@ nano::tomlconfig nano::load_toml_file (const std::filesystem::path & config_file } config_overrides_stream << std::endl; - auto try_load_toml = [&config_overrides_stream] (auto toml_config_path) -> std::optional { - // Make sure we don't create an empty toml file if it doesn't exist. Running without a toml file is the default. - if (std::filesystem::exists (toml_config_path)) + // Make sure we don't create an empty toml file if it doesn't exist. Running without a toml file is the default. + auto toml_config_path = data_path / config_filename; + if (std::filesystem::exists (toml_config_path)) + { + nano::tomlconfig toml; + auto error = toml.read (config_overrides_stream, toml_config_path); + if (error) { - nano::tomlconfig toml; - auto error = toml.read (config_overrides_stream, toml_config_path); - if (error) - { - throw std::runtime_error (error.get_message ()); - } - return toml; + throw std::runtime_error (error.get_message ()); } - return std::nullopt; - }; - - // First try to load config from the current working directory, then from the node data directory - if (auto toml = try_load_toml (config_filename); toml) - { - nano::default_logger ().info (nano::log::type::config, "Config for `{}` loaded from current working directory", config_filename.string ()); - return *toml; - } - if (auto toml = try_load_toml (data_path / config_filename); toml) - { - nano::default_logger ().info (nano::log::type::config, "Config for `{}` loaded from node data directory ({})", config_filename.string (), data_path.string ()); - return *toml; + nano::default_logger ().info (nano::log::type::config, "Config for `{}` loaded from node data directory: ", config_filename.string (), toml_config_path.string ()); + return toml; } - - // If no config was found, return an empty config with overrides applied - nano::tomlconfig toml; - auto error = toml.read (config_overrides_stream); - if (error) + else { - throw std::runtime_error (error.get_message ()); + // If no config was found, return an empty config with overrides applied + nano::tomlconfig toml; + auto error = toml.read (config_overrides_stream); + if (error) + { + throw std::runtime_error (error.get_message ()); + } + nano::default_logger ().info (nano::log::type::config, "Config for `{}` not found, using default configuration", config_filename.string ()); + return toml; } - nano::default_logger ().info (nano::log::type::config, "Config for `{}` not found, using default configuration", config_filename.string ()); - return toml; } diff --git a/nano/lib/logging.cpp b/nano/lib/logging.cpp index 342f402e35..31969bbc69 100644 --- a/nano/lib/logging.cpp +++ b/nano/lib/logging.cpp @@ -34,10 +34,10 @@ std::function nano::n return std::string{ to_string (tag) }; } }; -void nano::nlogger::initialize (nano::log_config config) +void nano::nlogger::initialize (nano::log_config fallback, std::filesystem::path data_path, std::vector const & config_overrides) { - initialize_common (config); - + auto config = nano::load_log_config (std::move (fallback), data_path, config_overrides); + initialize_common (config, data_path); global_initialized = true; } @@ -47,6 +47,7 @@ namespace /// Takes a qualified identifier in the form `node_identifier::tag` and splits it into a pair of `identifier` and `tag` /// It is a limitation of spldlog that we cannot attach additional data to the logger, so we have to encode the node identifier in the logger name /// @returns + std::pair split_qualified_identifier (std::string_view qualified_identifier) { auto pos = qualified_identifier.find ("::"); @@ -94,15 +95,17 @@ class tag_formatter_flag : public spdlog::custom_flag_formatter }; } -void nano::nlogger::initialize_for_tests (nano::log_config config) +void nano::nlogger::initialize_for_tests (nano::log_config fallback) { - initialize_common (config); + auto config = nano::load_log_config (std::move (fallback), /* load log config from current workdir */ {}); + initialize_common (config, /* store log file in current workdir */ {}); // Use tag and identifier as the logger name, since multiple nodes may be running in the same process global_name_formatter = [] (nano::log::type tag, std::string identifier) { return fmt::format ("{}::{}", identifier, to_string (tag)); }; + // Setup formatter to include information about node identifier `[%i]` and tag `[%n]` auto formatter = std::make_unique (); formatter->add_flag ('i'); formatter->add_flag ('n'); @@ -110,14 +113,13 @@ void nano::nlogger::initialize_for_tests (nano::log_config config) for (auto & sink : global_sinks) { - // Make deep copy of formatter for each sink sink->set_formatter (formatter->clone ()); } global_initialized = true; } -void nano::nlogger::initialize_common (nano::log_config const & config) +void nano::nlogger::initialize_common (nano::log_config const & config, std::filesystem::path data_path) { global_config = config; @@ -160,8 +162,8 @@ void nano::nlogger::initialize_common (nano::log_config const & config) auto filename = fmt::format ("log_{:%Y-%m-%d_%H-%M}-{:%S}", fmt::localtime (time), now.time_since_epoch ()); std::replace (filename.begin (), filename.end (), '.', '_'); // Replace millisecond dot separator with underscore - std::filesystem::path log_path{ "log" }; - log_path /= filename + ".log"; + std::filesystem::path log_path{ data_path / "log" / (filename + ".log") }; + log_path = std::filesystem::absolute (log_path); std::cerr << "Logging to file: " << log_path.string () << std::endl; @@ -287,6 +289,7 @@ nano::log_config nano::log_config::tests_default () { log_config config{}; config.default_level = nano::log::level::off; + config.file.enable = false; return config; } diff --git a/nano/lib/logging.hpp b/nano/lib/logging.hpp index 9906b45f28..1c23d49ba2 100644 --- a/nano/lib/logging.hpp +++ b/nano/lib/logging.hpp @@ -76,8 +76,8 @@ class nlogger final nlogger (nlogger const &) = delete; public: - static void initialize (nano::log_config); - static void initialize_for_tests (nano::log_config); + static void initialize (nano::log_config fallback, std::filesystem::path data_path = {}, std::vector const & config_overrides = std::vector ()); + static void initialize_for_tests (nano::log_config fallback); static void flush (); private: @@ -86,7 +86,7 @@ class nlogger final static std::vector global_sinks; static std::function global_name_formatter; - static void initialize_common (nano::log_config const &); + static void initialize_common (nano::log_config const &, std::filesystem::path data_path); public: template diff --git a/nano/load_test/entry.cpp b/nano/load_test/entry.cpp index 3fe7134648..a159d05cd8 100644 --- a/nano/load_test/entry.cpp +++ b/nano/load_test/entry.cpp @@ -491,7 +491,7 @@ account_info account_info_rpc (boost::asio::io_context & ioc, tcp::resolver::res /** This launches a node and fires a lot of send/recieve RPC requests at it (configurable), then other nodes are tested to make sure they observe these blocks as well. */ int main (int argc, char * const * argv) { - nano::nlogger::initialize_for_tests (nano::load_log_config (nano::log_config::tests_default ())); + nano::nlogger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); boost::program_options::options_description description ("Command line options"); diff --git a/nano/nano_node/daemon.cpp b/nano/nano_node/daemon.cpp index 4f2f8dffe1..728cd5ad0d 100644 --- a/nano/nano_node/daemon.cpp +++ b/nano/nano_node/daemon.cpp @@ -63,7 +63,7 @@ constexpr std::size_t OPEN_FILE_DESCRIPTORS_LIMIT = 16384; void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flags const & flags) { - nano::nlogger::initialize (nano::load_log_config (nano::log_config::daemon_default (), data_path, flags.config_overrides)); + nano::nlogger::initialize (nano::log_config::daemon_default (), data_path, flags.config_overrides); nlogger.info (nano::log::type::daemon, "Daemon started"); diff --git a/nano/nano_wallet/entry.cpp b/nano/nano_wallet/entry.cpp index 04e55e14d2..3c863c06f1 100644 --- a/nano/nano_wallet/entry.cpp +++ b/nano/nano_wallet/entry.cpp @@ -74,7 +74,7 @@ nano::error read_wallet_config (nano::wallet_config & config_a, std::filesystem: int run_wallet (QApplication & application, int argc, char * const * argv, std::filesystem::path const & data_path, nano::node_flags const & flags) { - nano::nlogger::initialize (nano::load_log_config (nano::log_config::daemon_default (), data_path, flags.config_overrides)); + nano::nlogger::initialize (nano::log_config::daemon_default (), data_path, flags.config_overrides); nlogger.info (nano::log::type::daemon_wallet, "Daemon started (wallet)"); diff --git a/nano/qt_test/entry.cpp b/nano/qt_test/entry.cpp index 3ac2f817f1..a34cb347dd 100644 --- a/nano/qt_test/entry.cpp +++ b/nano/qt_test/entry.cpp @@ -17,7 +17,7 @@ void force_nano_dev_network (); int main (int argc, char ** argv) { - nano::nlogger::initialize_for_tests (nano::load_log_config (nano::log_config::tests_default ())); + nano::nlogger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; QApplication application (argc, argv); diff --git a/nano/rpc_test/entry.cpp b/nano/rpc_test/entry.cpp index 06b87f9370..be70e1183e 100644 --- a/nano/rpc_test/entry.cpp +++ b/nano/rpc_test/entry.cpp @@ -15,7 +15,7 @@ void force_nano_dev_network (); int main (int argc, char ** argv) { - nano::nlogger::initialize_for_tests (nano::load_log_config (nano::log_config::tests_default ())); + nano::nlogger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); nano::set_use_memory_pools (false); nano::node_singleton_memory_pool_purge_guard cleanup_guard; diff --git a/nano/slow_test/entry.cpp b/nano/slow_test/entry.cpp index cec8cabce2..5e3ff3ea5a 100644 --- a/nano/slow_test/entry.cpp +++ b/nano/slow_test/entry.cpp @@ -14,7 +14,7 @@ void force_nano_dev_network (); int main (int argc, char ** argv) { - nano::nlogger::initialize_for_tests (nano::load_log_config (nano::log_config::tests_default ())); + nano::nlogger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; testing::InitGoogleTest (&argc, argv);