Kagome
Polkadot Runtime Engine in C++17
logger.cpp
Go to the documentation of this file.
1 
6 #include <boost/assert.hpp>
7 #include <iostream>
8 #include <libp2p/log/logger.hpp>
9 #include <soralog/impl/sink_to_console.hpp>
10 
11 #include "log/logger.hpp"
12 #include "log/profiling_logger.hpp"
13 
15  using E = kagome::log::Error;
16  switch (e) {
17  case E::WRONG_LEVEL:
18  return "Unknown level";
19  case E::WRONG_GROUP:
20  return "Unknown group";
21  case E::WRONG_LOGGER:
22  return "Unknown logger";
23  }
24  return "Unknown log::Error";
25 }
26 
27 namespace kagome::log {
28 
29  namespace {
30  // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
31  std::shared_ptr<soralog::LoggingSystem> logging_system_;
32 
33  void ensure_logger_system_is_initialized() noexcept {
34  BOOST_ASSERT_MSG(
35  logging_system_,
36  "Logging system is not ready. "
37  "kagome::log::setLoggingSystem() must be executed once before");
38  }
39  } // namespace
40 
41  outcome::result<Level> str2lvl(std::string_view str) {
42  if (str == "trace") {
43  return Level::TRACE;
44  } else if (str == "debug") {
45  return Level::DEBUG;
46  } else if (str == "verbose") {
47  return Level::VERBOSE;
48  } else if (str == "info" or str == "inf") {
49  return Level::INFO;
50  } else if (str == "warning" or str == "warn") {
51  return Level::WARN;
52  } else if (str == "error" or str == "err") {
53  return Level::ERROR;
54  } else if (str == "critical" or str == "crit") {
55  return Level::CRITICAL;
56  } else if (str == "off" or str == "no") {
57  return Level::OFF;
58  } else {
59  return Error::WRONG_LEVEL;
60  }
61  }
62 
64  std::shared_ptr<soralog::LoggingSystem> logging_system) {
65  BOOST_ASSERT(logging_system != nullptr);
66  libp2p::log::setLoggingSystem(logging_system);
67  logging_system_ = std::move(logging_system);
68  profiling_logger = createLogger("Profiler", "profile");
69  }
70 
71  void tuneLoggingSystem(const std::vector<std::string> &cfg) {
72  ensure_logger_system_is_initialized();
73 
74  if (cfg.empty()) {
75  return;
76  }
77 
78  for (auto &chunk : cfg) {
79  if (auto res = str2lvl(chunk); res.has_value()) {
80  auto level = res.value();
82  continue;
83  }
84 
85  std::istringstream iss2(chunk);
86 
87  std::string group_name;
88  if (not std::getline(iss2, group_name, '=')) {
89  std::cerr << "Can't read group";
90  }
91  if (not logging_system_->getGroup(group_name)) {
92  std::cerr << "Unknown group: " << group_name << std::endl;
93  continue;
94  }
95 
96  std::string level_string;
97  if (not std::getline(iss2, level_string)) {
98  std::cerr << "Can't read level for group '" << group_name << "'";
99  continue;
100  }
101  auto res = str2lvl(level_string);
102  if (not res.has_value()) {
103  std::cerr << "Invalid level: " << level_string << std::endl;
104  continue;
105  }
106  auto level = res.value();
107 
108  logging_system_->setLevelOfGroup(group_name, level);
109  }
110  }
111 
112  Logger createLogger(const std::string &tag) {
113  ensure_logger_system_is_initialized();
114  return std::static_pointer_cast<soralog::LoggerFactory>(logging_system_)
115  ->getLogger(tag, defaultGroupName);
116  }
117 
118  Logger createLogger(const std::string &tag, const std::string &group) {
119  ensure_logger_system_is_initialized();
120  return std::static_pointer_cast<soralog::LoggerFactory>(logging_system_)
121  ->getLogger(tag, group);
122  }
123 
124  Logger createLogger(const std::string &tag,
125  const std::string &group,
126  Level level) {
127  ensure_logger_system_is_initialized();
128  return std::static_pointer_cast<soralog::LoggerFactory>(logging_system_)
129  ->getLogger(tag, group, level);
130  }
131 
132  bool setLevelOfGroup(const std::string &group_name, Level level) {
133  ensure_logger_system_is_initialized();
134  return logging_system_->setLevelOfGroup(group_name, level);
135  }
136  bool resetLevelOfGroup(const std::string &group_name) {
137  ensure_logger_system_is_initialized();
138  return logging_system_->resetLevelOfGroup(group_name);
139  }
140 
141  bool setLevelOfLogger(const std::string &logger_name, Level level) {
142  ensure_logger_system_is_initialized();
143  return logging_system_->setLevelOfLogger(logger_name, level);
144  }
145  bool resetLevelOfLogger(const std::string &logger_name) {
146  ensure_logger_system_is_initialized();
147  return logging_system_->resetLevelOfLogger(logger_name);
148  }
149 
150 } // namespace kagome::log
bool resetLevelOfGroup(const std::string &group_name)
Definition: logger.cpp:136
Logger profiling_logger
void tuneLoggingSystem(const std::vector< std::string > &cfg)
Definition: logger.cpp:71
bool resetLevelOfLogger(const std::string &logger_name)
Definition: logger.cpp:145
soralog::Level Level
Definition: logger.hpp:22
bool setLevelOfLogger(const std::string &logger_name, Level level)
Definition: logger.cpp:141
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
void setLoggingSystem(std::shared_ptr< soralog::LoggingSystem > logging_system)
Definition: logger.cpp:63
OUTCOME_CPP_DEFINE_CATEGORY(kagome::log, Error, e)
Definition: logger.cpp:14
static const std::string defaultGroupName("kagome")
outcome::result< Level > str2lvl(std::string_view str)
Definition: logger.cpp:41
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
bool setLevelOfGroup(const std::string &group_name, Level level)
Definition: logger.cpp:132