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

Factor out logging #1262

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions rai/node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ add_library (node
common.cpp
lmdb.cpp
lmdb.hpp
logging.cpp
logging.hpp
node.hpp
node.cpp
openclwork.cpp
Expand Down
215 changes: 215 additions & 0 deletions rai/node/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <rai/node/logging.hpp>

rai::logging::logging () :
ledger_logging_value (false),
ledger_duplicate_logging_value (false),
vote_logging_value (false),
network_logging_value (true),
network_message_logging_value (false),
network_publish_logging_value (false),
network_packet_logging_value (false),
network_keepalive_logging_value (false),
network_node_id_handshake_logging_value (false),
node_lifetime_tracing_value (false),
insufficient_work_logging_value (true),
log_rpc_value (true),
bulk_pull_logging_value (false),
work_generation_time_value (true),
log_to_cerr_value (false),
max_size (16 * 1024 * 1024),
rotation_size (4 * 1024 * 1024),
flush (true)
{
}

void rai::logging::init (boost::filesystem::path const & application_path_a)
{
static std::atomic_flag logging_already_added = ATOMIC_FLAG_INIT;
if (!logging_already_added.test_and_set ())
{
boost::log::add_common_attributes ();
if (log_to_cerr ())
{
boost::log::add_console_log (std::cerr, boost::log::keywords::format = "[%TimeStamp%]: %Message%");
}
boost::log::add_file_log (boost::log::keywords::target = application_path_a / "log", boost::log::keywords::file_name = application_path_a / "log" / "log_%Y-%m-%d_%H-%M-%S.%N.log", boost::log::keywords::rotation_size = rotation_size, boost::log::keywords::auto_flush = flush, boost::log::keywords::scan_method = boost::log::sinks::file::scan_method::scan_matching, boost::log::keywords::max_size = max_size, boost::log::keywords::format = "[%TimeStamp%]: %Message%");
}
}

void rai::logging::serialize_json (boost::property_tree::ptree & tree_a) const
{
tree_a.put ("version", "4");
tree_a.put ("ledger", ledger_logging_value);
tree_a.put ("ledger_duplicate", ledger_duplicate_logging_value);
tree_a.put ("vote", vote_logging_value);
tree_a.put ("network", network_logging_value);
tree_a.put ("network_message", network_message_logging_value);
tree_a.put ("network_publish", network_publish_logging_value);
tree_a.put ("network_packet", network_packet_logging_value);
tree_a.put ("network_keepalive", network_keepalive_logging_value);
tree_a.put ("network_node_id_handshake", network_node_id_handshake_logging_value);
tree_a.put ("node_lifetime_tracing", node_lifetime_tracing_value);
tree_a.put ("insufficient_work", insufficient_work_logging_value);
tree_a.put ("log_rpc", log_rpc_value);
tree_a.put ("bulk_pull", bulk_pull_logging_value);
tree_a.put ("work_generation_time", work_generation_time_value);
tree_a.put ("log_to_cerr", log_to_cerr_value);
tree_a.put ("max_size", max_size);
tree_a.put ("rotation_size", rotation_size);
tree_a.put ("flush", flush);
}

bool rai::logging::upgrade_json (unsigned version_a, boost::property_tree::ptree & tree_a)
{
auto result (false);
switch (version_a)
{
case 1:
tree_a.put ("vote", vote_logging_value);
tree_a.put ("version", "2");
result = true;
case 2:
tree_a.put ("rotation_size", "4194304");
tree_a.put ("flush", "true");
tree_a.put ("version", "3");
result = true;
case 3:
tree_a.put ("network_node_id_handshake", "false");
tree_a.put ("version", "4");
result = true;
case 4:
break;
default:
throw std::runtime_error ("Unknown logging_config version");
break;
}
return result;
}

bool rai::logging::deserialize_json (bool & upgraded_a, boost::property_tree::ptree & tree_a)
{
auto result (false);
try
{
auto version_l (tree_a.get_optional<std::string> ("version"));
if (!version_l)
{
tree_a.put ("version", "1");
version_l = "1";
auto work_peers_l (tree_a.get_child_optional ("work_peers"));
if (!work_peers_l)
{
tree_a.add_child ("work_peers", boost::property_tree::ptree ());
}
upgraded_a = true;
}
upgraded_a |= upgrade_json (std::stoull (version_l.get ()), tree_a);
ledger_logging_value = tree_a.get<bool> ("ledger");
ledger_duplicate_logging_value = tree_a.get<bool> ("ledger_duplicate");
vote_logging_value = tree_a.get<bool> ("vote");
network_logging_value = tree_a.get<bool> ("network");
network_message_logging_value = tree_a.get<bool> ("network_message");
network_publish_logging_value = tree_a.get<bool> ("network_publish");
network_packet_logging_value = tree_a.get<bool> ("network_packet");
network_keepalive_logging_value = tree_a.get<bool> ("network_keepalive");
network_node_id_handshake_logging_value = tree_a.get<bool> ("network_node_id_handshake");
node_lifetime_tracing_value = tree_a.get<bool> ("node_lifetime_tracing");
insufficient_work_logging_value = tree_a.get<bool> ("insufficient_work");
log_rpc_value = tree_a.get<bool> ("log_rpc");
bulk_pull_logging_value = tree_a.get<bool> ("bulk_pull");
work_generation_time_value = tree_a.get<bool> ("work_generation_time");
log_to_cerr_value = tree_a.get<bool> ("log_to_cerr");
max_size = tree_a.get<uintmax_t> ("max_size");
rotation_size = tree_a.get<uintmax_t> ("rotation_size", 4194304);
flush = tree_a.get<bool> ("flush", true);
}
catch (std::runtime_error const &)
{
result = true;
}
return result;
}

bool rai::logging::ledger_logging () const
{
return ledger_logging_value;
}

bool rai::logging::ledger_duplicate_logging () const
{
return ledger_logging () && ledger_duplicate_logging_value;
}

bool rai::logging::vote_logging () const
{
return vote_logging_value;
}

bool rai::logging::network_logging () const
{
return network_logging_value;
}

bool rai::logging::network_message_logging () const
{
return network_logging () && network_message_logging_value;
}

bool rai::logging::network_publish_logging () const
{
return network_logging () && network_publish_logging_value;
}

bool rai::logging::network_packet_logging () const
{
return network_logging () && network_packet_logging_value;
}

bool rai::logging::network_keepalive_logging () const
{
return network_logging () && network_keepalive_logging_value;
}

bool rai::logging::network_node_id_handshake_logging () const
{
return network_logging () && network_node_id_handshake_logging_value;
}

bool rai::logging::node_lifetime_tracing () const
{
return node_lifetime_tracing_value;
}

bool rai::logging::insufficient_work_logging () const
{
return network_logging () && insufficient_work_logging_value;
}

bool rai::logging::log_rpc () const
{
return network_logging () && log_rpc_value;
}

bool rai::logging::bulk_pull_logging () const
{
return network_logging () && bulk_pull_logging_value;
}

bool rai::logging::callback_logging () const
{
return network_logging ();
}

bool rai::logging::work_generation_time () const
{
return work_generation_time_value;
}

bool rai::logging::log_to_cerr () const
{
return log_to_cerr_value;
}
56 changes: 56 additions & 0 deletions rai/node/logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <boost/filesystem.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/trivial.hpp>
#include <boost/property_tree/ptree.hpp>
#include <cstdint>

namespace rai
{
class logging
{
public:
logging ();
void serialize_json (boost::property_tree::ptree &) const;
bool deserialize_json (bool &, boost::property_tree::ptree &);
bool upgrade_json (unsigned, boost::property_tree::ptree &);
bool ledger_logging () const;
bool ledger_duplicate_logging () const;
bool vote_logging () const;
bool network_logging () const;
bool network_message_logging () const;
bool network_publish_logging () const;
bool network_packet_logging () const;
bool network_keepalive_logging () const;
bool network_node_id_handshake_logging () const;
bool node_lifetime_tracing () const;
bool insufficient_work_logging () const;
bool log_rpc () const;
bool bulk_pull_logging () const;
bool callback_logging () const;
bool work_generation_time () const;
bool log_to_cerr () const;
void init (boost::filesystem::path const &);

bool ledger_logging_value;
bool ledger_duplicate_logging_value;
bool vote_logging_value;
bool network_logging_value;
bool network_message_logging_value;
bool network_publish_logging_value;
bool network_packet_logging_value;
bool network_keepalive_logging_value;
bool network_node_id_handshake_logging_value;
bool node_lifetime_tracing_value;
bool insufficient_work_logging_value;
bool log_rpc_value;
bool bulk_pull_logging_value;
bool work_generation_time_value;
bool log_to_cerr_value;
bool flush;
uintmax_t max_size;
uintmax_t rotation_size;
boost::log::sources::logger_mt log;
};
}
Loading