Skip to content

Commit

Permalink
Introduce new logger
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 9, 2024
1 parent 3f2913b commit f0667d7
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ add_library(
locks.hpp
locks.cpp
logger_mt.hpp
logging.hpp
logging.cpp
logging_enums.hpp
logging_enums.cpp
memory.hpp
memory.cpp
numbers.hpp
Expand Down
107 changes: 107 additions & 0 deletions nano/lib/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <nano/lib/logging.hpp>
#include <nano/lib/utility.hpp>

#include <spdlog/cfg/env.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>

namespace
{
std::atomic<bool> initialized{ false };
}

void nano::initialize_logging (nano::log::preset preset)
{
spdlog::set_automatic_registration (false);

switch (preset)
{
case log::preset::cli:
{
// Only display critical errors when using node CLI
spdlog::set_level (spdlog::level::critical);
}
break;
case log::preset::daemon:
{
spdlog::set_level (spdlog::level::info);
}
break;
case log::preset::tests:
{
// Avoid cluttering the test runner output
spdlog::set_level (spdlog::level::critical);
}
break;
}

spdlog::cfg::load_env_levels ();

// auto logger = spdlog::basic_logger_mt ("default", "nano_log.txt");
auto logger = spdlog::stdout_color_mt ("default");
spdlog::set_default_logger (logger);

initialized = true;
}

/*
* nlogger
*/

nano::nlogger::nlogger ()
{
}

spdlog::logger & nano::nlogger::get_logger (nano::log::tag tag)
{
// This is a two-step process to avoid locking the mutex in the common case
{
std::shared_lock slock{ mutex };

if (auto it = spd_loggers.find (tag); it != spd_loggers.end ())
{
return *it->second;
}
}
// Not found, create a new logger
{
std::unique_lock lock{ mutex };

auto [it2, inserted] = spd_loggers.emplace (tag, make_logger (tag));
return *it2->second;
}
}

std::shared_ptr<spdlog::logger> nano::nlogger::make_logger (nano::log::tag tag)
{
debug_assert (initialized.load (), "nano::initialize_logging must be called before using nano::nlogger");

auto const & sinks = spdlog::default_logger ()->sinks ();
auto spd_logger = std::make_shared<spdlog::logger> (std::string{ nano::to_string (tag) }, sinks.begin (), sinks.end ());
spdlog::initialize_logger (spd_logger);
return spd_logger;
}

spdlog::level::level_enum nano::nlogger::to_spd_level (nano::log::level level)
{
switch (level)
{
case nano::log::level::off:
return spdlog::level::off;
case nano::log::level::critical:
return spdlog::level::critical;
case nano::log::level::error:
return spdlog::level::err;
case nano::log::level::warn:
return spdlog::level::warn;
case nano::log::level::info:
return spdlog::level::info;
case nano::log::level::debug:
return spdlog::level::debug;
case nano::log::level::trace:
return spdlog::level::trace;
default:
debug_assert (false);
return spdlog::level::off;
}
}
68 changes: 68 additions & 0 deletions nano/lib/logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#pragma once

#include <nano/lib/logging_enums.hpp>

#include <initializer_list>
#include <memory>
#include <shared_mutex>
#include <sstream>

#include <spdlog/spdlog.h>

namespace nano
{
class nlogger
{
public:
nlogger ();

public:
template <class... Args>
void log (nano::log::level level, nano::log::tag tag, spdlog::format_string_t<Args...> fmt, Args &&... args)
{
get_logger (tag).log (to_spd_level (level), fmt, std::forward<Args> (args)...);
}

template <class... Args>
void debug (nano::log::tag tag, spdlog::format_string_t<Args...> fmt, Args &&... args)
{
get_logger (tag).debug (fmt, std::forward<Args> (args)...);
}

template <class... Args>
void info (nano::log::tag tag, spdlog::format_string_t<Args...> fmt, Args &&... args)
{
get_logger (tag).info (fmt, std::forward<Args> (args)...);
}

template <class... Args>
void warn (nano::log::tag tag, spdlog::format_string_t<Args...> fmt, Args &&... args)
{
get_logger (tag).warn (fmt, std::forward<Args> (args)...);
}

template <class... Args>
void error (nano::log::tag tag, spdlog::format_string_t<Args...> fmt, Args &&... args)
{
get_logger (tag).error (fmt, std::forward<Args> (args)...);
}

template <class... Args>
void critical (nano::log::tag tag, spdlog::format_string_t<Args...> fmt, Args &&... args)
{
get_logger (tag).critical (fmt, std::forward<Args> (args)...);
}

private:
std::unordered_map<nano::log::tag, std::shared_ptr<spdlog::logger>> spd_loggers;
std::shared_mutex mutex;

private:
spdlog::logger & get_logger (nano::log::tag tag);
std::shared_ptr<spdlog::logger> make_logger (nano::log::tag tag);

static spdlog::level::level_enum to_spd_level (nano::log::level);
};

void initialize_logging (nano::log::preset = nano::log::preset::cli);
}
22 changes: 22 additions & 0 deletions nano/lib/logging_enums.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#define MAGIC_ENUM_RANGE_MIN 0
#define MAGIC_ENUM_RANGE_MAX 256

#include <nano/lib/logging_enums.hpp>
#include <nano/lib/utility.hpp>

#include <magic_enum.hpp>

std::string_view nano::to_string (nano::log::tag tag)
{
return magic_enum::enum_name (tag);
}

std::string_view nano::to_string (nano::log::detail detail)
{
return magic_enum::enum_name (detail);
}

std::string_view nano::to_string (nano::log::level level)
{
return magic_enum::enum_name (level);
}
127 changes: 127 additions & 0 deletions nano/lib/logging_enums.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#pragma once

#include <cstdint>
#include <string_view>

namespace nano::log
{
enum class level
{
trace,
debug,
info,
warn,
error,
critical,
off,
};

enum class tag
{
all = 0, // reserved

generic,
node,
daemon,
wallet,
qt,
rpc,
rpc_connection,
rpc_callbacks,
rpc_request,
ipc,
ipc_server,
websocket,
tls,
active_transactions,
election,
blockprocessor,
network,
channel,
socket,
socket_server,
tcp,
tcp_server,
prunning,
conf_processor_bounded,
conf_processor_unbounded,
distributed_work,
epoch_upgrader,
opencl_work,
upnp,
repcrawler,
lmdb,
rocksdb,
txn_tracker,
gap_cache,
vote_processor,
bulk_pull_client,
bulk_pull_server,
bulk_pull_account_client,
bulk_pull_account_server,
bulk_push_client,
bulk_push_server,
frontier_req_client,
frontier_req_server,
bootstrap,
bootstrap_lazy,
bootstrap_legacy,
};

enum class category
{
all = 0, // reserved

work_generation,
};

enum class detail
{
all = 0, // reserved

// node
process_confirmed,

// active_transactions
active_started,
active_stopped,

// election
election_confirmed,
election_expired,

// blockprocessor
block_processed,

// vote_processor
vote_processed,

// network
message_received,
message_sent,
message_dropped,

// bulk pull/push
pulled_block,
sending_block,
sending_pending,
sending_frontier,
requesting_account_or_head,
requesting_pending,

};

enum class preset
{
cli,
daemon,
tests,
};
}

namespace nano
{
std::string_view to_string (nano::log::tag);
std::string_view to_string (nano::log::detail);
std::string_view to_string (nano::log::level);
}

0 comments on commit f0667d7

Please sign in to comment.