Skip to content

Commit

Permalink
INITIALIZATION WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 16, 2024
1 parent acc1eca commit 5e8f105
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 74 deletions.
87 changes: 51 additions & 36 deletions nano/lib/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,47 @@ std::atomic<bool> logging_initialized{ false };

nano::nlogger & nano::default_logger ()
{
static nano::log_config config = nano::log_config::cli_default ();
static nano::nlogger logger{ config };
static nano::nlogger logger;
return logger;
}

void nano::initialize_logging ()
{
debug_assert (!logging_initialized, "initialize_logging must only be called once");

spdlog::set_automatic_registration (false);
spdlog::set_level (spdlog::level::trace);
spdlog::cfg::load_env_levels ();

logging_initialized = true;
}

void nano::release_logging ()
{
logging_initialized = false;

spdlog::shutdown ();
}

/*
* nlogger
*/

nano::nlogger::nlogger (nano::log_config config, std::string identifier) :
config{ std::move (config) },
identifier{ std::move (identifier) }
bool nano::nlogger::global_initialized;
nano::log_config nano::nlogger::global_config;
std::vector<spdlog::sink_ptr> nano::nlogger::global_sinks;

void nano::nlogger::initialize (nano::log_config config)
{
debug_assert (logging_initialized, "initialize_logging must be called before creating a logger");
spdlog::set_automatic_registration (false);
spdlog::set_level (to_spdlog_level (config.default_level));
spdlog::cfg::load_env_levels ();

global_sinks.clear ();

// Console setup
if (config.console.enable)
{
// Only use colors if not writing to cerr
if (!config.console.to_cerr)
{
// Only use colors if not writing to cerr
if (config.console.colors)
{
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt> ();
sinks.push_back (console_sink);
global_sinks.push_back (console_sink);
}
else
{
auto console_sink = std::make_shared<spdlog::sinks::stdout_sink_mt> ();
sinks.push_back (console_sink);
global_sinks.push_back (console_sink);
}
}
else
{
auto cerr_sink = std::make_shared<spdlog::sinks::stderr_sink_mt> ();
sinks.push_back (cerr_sink);
global_sinks.push_back (cerr_sink);
}
}

Expand All @@ -85,22 +72,44 @@ nano::nlogger::nlogger (nano::log_config config, std::string identifier) :
std::filesystem::path log_path{ "log" };
log_path /= filename + ".log";

std::cerr << "Logging to file: " << log_path << std::endl;
std::cout << "Logging to file: " << log_path << std::endl;

// If either max_size or rotation_count is 0, then disable file rotation
if (config.file.max_size == 0 || config.file.rotation_count == 0)
{
std::cerr << "WARNING: File rotation disabled, possibly unlimited log file size" << std::endl;
// TODO: Maybe show a warning to the user about possibly unlimited log file size

auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt> (log_path, true);
sinks.push_back (file_sink);
global_sinks.push_back (file_sink);
}
else
{
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt> (log_path, config.file.max_size, config.file.rotation_count);
sinks.push_back (file_sink);
global_sinks.push_back (file_sink);
}
}

auto logger = std::make_shared<spdlog::logger> ("default", global_sinks.begin (), global_sinks.end ());
spdlog::set_default_logger (logger);

global_initialized = true;
}

void nano::nlogger::flush ()
{
for (auto & sink : global_sinks)
{
sink->flush ();
}
}

/*
* nlogger
*/

nano::nlogger::nlogger (std::string identifier) :
identifier{ std::move (identifier) }
{
}

spdlog::logger & nano::nlogger::get_logger (nano::log::type tag)
Expand All @@ -125,17 +134,17 @@ spdlog::logger & nano::nlogger::get_logger (nano::log::type tag)

std::shared_ptr<spdlog::logger> nano::nlogger::make_logger (nano::log::type tag)
{
auto spd_logger = std::make_shared<spdlog::logger> (std::string{ to_string (tag) }, sinks.begin (), sinks.end ());
auto spd_logger = std::make_shared<spdlog::logger> (std::string{ to_string (tag) }, global_sinks.begin (), global_sinks.end ());

spdlog::initialize_logger (spd_logger);

if (auto it = config.levels.find ({ tag, nano::log::detail::all }); it != config.levels.end ())
if (auto it = global_config.levels.find ({ tag, nano::log::detail::all }); it != global_config.levels.end ())
{
spd_logger->set_level (to_spdlog_level (it->second));
}
else
{
spd_logger->set_level (to_spdlog_level (config.default_level));
spd_logger->set_level (to_spdlog_level (global_config.default_level));
}

return spd_logger;
Expand Down Expand Up @@ -263,7 +272,7 @@ nano::error nano::log_config::deserialize (nano::tomlconfig & toml)
catch (std::invalid_argument const & ex)
{
// Ignore but warn about invalid logger names
nano::log::warn ("Processing log config: {}", ex.what ());
std::cerr << "Problem processing log config: " << ex.what () << std::endl;
}
}
}
Expand Down Expand Up @@ -344,4 +353,10 @@ nano::error nano::read_log_config_toml (const std::filesystem::path & data_path,
}

return error;
}

nano::log_config nano::load_log_config (nano::log_config fallback, const std::filesystem::path & data_path, const std::vector<std::string> & config_overrides)
{
auto config = fallback;
return config;
}
16 changes: 11 additions & 5 deletions nano/lib/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class log_config final

nano::error read_log_config_toml (std::filesystem::path const & data_path, nano::log_config & config, std::vector<std::string> const & overrides);

void initialize_logging ();
void release_logging ();
nano::log_config load_log_config (nano::log_config fallback, std::filesystem::path const & data_path, std::vector<std::string> const & config_overrides = std::vector<std::string> ());
}

namespace nano
Expand All @@ -66,11 +65,20 @@ spdlog::level::level_enum to_spdlog_level (nano::log::level);
class nlogger final
{
public:
nlogger (nano::log_config, std::string identifier = "");
nlogger (std::string identifier = "");

// Disallow copies
nlogger (nlogger const &) = delete;

public:
static void initialize (nano::log_config);
static void flush ();

private:
static bool global_initialized;
static nano::log_config global_config;
static std::vector<spdlog::sink_ptr> global_sinks;

public:
template <class... Args>
void log (nano::log::level level, nano::log::type tag, spdlog::format_string_t<Args...> fmt, Args &&... args)
Expand Down Expand Up @@ -109,10 +117,8 @@ class nlogger final
}

private:
const nano::log_config config;
const std::string identifier;

std::vector<spdlog::sink_ptr> sinks;
std::unordered_map<nano::log::type, std::shared_ptr<spdlog::logger>> spd_loggers;
std::shared_mutex mutex;

Expand Down
17 changes: 4 additions & 13 deletions nano/nano_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,16 @@ 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::log::info ("Daemon started");
nano::nlogger::initialize (nano::load_log_config (nano::log_config::daemon_default (), data_path, flags.config_overrides));

nlogger.info (nano::log::type::daemon, "Daemon started");

install_abort_signal_handler ();

std::filesystem::create_directories (data_path);
boost::system::error_code error_chmod;
nano::set_secure_perm_directory (data_path, error_chmod);

nano::log_config log_config = nano::log_config::daemon_default ();
{
auto error = nano::read_log_config_toml (data_path, log_config, flags.config_overrides);
if (error)
{
nano::log::error ("Error reading logging config: {}, using defaults", error.get_message ());
log_config = nano::log_config::daemon_default ();
}
}
nano::nlogger nlogger{ log_config };

std::unique_ptr<nano::thread_runner> runner;
nano::network_params network_params{ nano::network_constants::active_network };
nano::daemon_config config{ data_path, network_params };
Expand Down Expand Up @@ -239,5 +230,5 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag
nlogger.critical (nano::log::type::daemon, "Error deserializing config: {}", error.get_message ());
}

nano::log::info ("Daemon exiting...");
nlogger.info (nano::log::type::daemon, "Daemon exiting");
}
2 changes: 1 addition & 1 deletion nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class address_library_pair
int main (int argc, char * const * argv)
{
nano::set_umask (); // Make sure the process umask is set before any files are created
nano::logging::initialize (nano::logging::config::cli_default ());
nano::nlogger::initialize (nano::log_config::cli_default ());

nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard;

Expand Down
24 changes: 6 additions & 18 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path cons
node_initialized_latch (1),
config (config_a),
network_params{ config.network_params },
nlogger{ config.logging },
nlogger{ "node" },
stats (config.stats_config),
workers{ config.background_threads, nano::thread_role::name::worker },
bootstrap_workers{ config.bootstrap_serving_threads, nano::thread_role::name::bootstrap_worker },
Expand Down Expand Up @@ -1493,29 +1493,17 @@ nano::node_wrapper::node_wrapper (std::filesystem::path const & path_a, std::fil
boost::system::error_code error_chmod;
nano::set_secure_perm_directory (path_a, error_chmod);

nano::log_config log_config = nano::log_config::daemon_default ();
{
auto error = nano::read_log_config_toml (config_path_a, log_config, node_flags_a.config_overrides);
if (error)
{
nano::log::error ("Error reading logging config: {}, using defaults", error.get_message ());
log_config = nano::log_config::daemon_default ();
}
}
nlogger = std::make_shared<nano::nlogger> (log_config);

nano::daemon_config daemon_config{ path_a, network_params };
auto error = nano::read_node_config_toml (config_path_a, daemon_config, node_flags_a.config_overrides);
if (error)
{
if (node_flags_a.config_overrides.empty ())
{
nano::log::error ("Error deserializing node config: {}", error.get_message ());
}
else
std::cerr << "Error deserializing config file";
if (!node_flags_a.config_overrides.empty ())
{
nano::log::error ("Error deserializing node config or --config option: {}", error.get_message ());
std::cerr << " or --config option";
}
std::cerr << "\n"
<< error.get_message () << std::endl;
std::exit (1);
}

Expand Down
2 changes: 1 addition & 1 deletion nano/rpc/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class rpc
return acceptor.local_endpoint ().port ();
}

nano::nlogger nlogger;
nano::nlogger nlogger{ "rpc" };
nano::rpc_config config;
boost::asio::ip::tcp::acceptor acceptor;
boost::asio::io_context & io_ctx;
Expand Down

0 comments on commit 5e8f105

Please sign in to comment.