Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging overhaul #4375

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions nano/lib/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,47 @@ uint32_t nano::test_scan_wallet_reps_delay ()
auto test_env = nano::get_env_or_default ("NANO_TEST_WALLET_SCAN_REPS_DELAY", "900000"); // 15 minutes by default
return boost::lexical_cast<uint32_t> (test_env);
}

nano::tomlconfig nano::load_toml_file (const std::filesystem::path & config_filename, const std::filesystem::path & data_path, const std::vector<std::string> & config_overrides)
{
std::stringstream config_overrides_stream;
for (auto const & entry : config_overrides)
{
config_overrides_stream << entry << std::endl;
}
config_overrides_stream << std::endl;

auto try_load_toml = [&config_overrides_stream] (auto toml_config_path) -> std::optional<nano::tomlconfig> {
// 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))
{
nano::tomlconfig toml;
auto error = toml.read (config_overrides_stream, toml_config_path);
if (error)
{
throw std::runtime_error (error.get_message ());
}
return toml;
}
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "; toml" isn't needed as the assignment expression will yield the assigned value

{
return *toml;
}
if (auto toml = try_load_toml (data_path / config_filename); toml)
{
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)
{
throw std::runtime_error (error.get_message ());
}
return toml;
}
22 changes: 22 additions & 0 deletions nano/lib/config.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <nano/lib/tomlconfig.hpp>

#include <boost/config.hpp>
#include <boost/version.hpp>

Expand Down Expand Up @@ -404,4 +406,24 @@ bool is_sanitizer_build ();

/** Set the active network to the dev network */
void force_nano_dev_network ();

/**
* Attempt to read a configuration file from current working directory, or if not found, the nano root directory.
* Returns empty tomlconfig if nothing is found.
*/
nano::tomlconfig load_toml_file (const std::filesystem::path & config_filename, const std::filesystem::path & data_path, const std::vector<std::string> & config_overrides);

template <typename T>
T load_config_file (T fallback, const std::filesystem::path & config_filename, const std::filesystem::path & data_path, const std::vector<std::string> & config_overrides)
{
auto toml = load_toml_file (config_filename, data_path, config_overrides);

T config = fallback;
auto error = config.deserialize_toml (toml);
if (error)
{
throw std::runtime_error (error.get_message ());
}
return config;
}
}