6 #include <boost/assert.hpp> 8 #include <libp2p/log/logger.hpp> 9 #include <soralog/impl/sink_to_console.hpp> 18 return "Unknown level";
20 return "Unknown group";
22 return "Unknown logger";
24 return "Unknown log::Error";
31 std::shared_ptr<soralog::LoggingSystem> logging_system_;
33 void ensure_logger_system_is_initialized() noexcept {
36 "Logging system is not ready. " 37 "kagome::log::setLoggingSystem() must be executed once before");
41 outcome::result<Level>
str2lvl(std::string_view str) {
44 }
else if (str ==
"debug") {
46 }
else if (str ==
"verbose") {
47 return Level::VERBOSE;
48 }
else if (str ==
"info" or str ==
"inf") {
50 }
else if (str ==
"warning" or str ==
"warn") {
52 }
else if (str ==
"error" or str ==
"err") {
54 }
else if (str ==
"critical" or str ==
"crit") {
55 return Level::CRITICAL;
56 }
else if (str ==
"off" or str ==
"no") {
64 std::shared_ptr<soralog::LoggingSystem> logging_system) {
65 BOOST_ASSERT(logging_system !=
nullptr);
67 logging_system_ = std::move(logging_system);
72 ensure_logger_system_is_initialized();
78 for (
auto &chunk : cfg) {
79 if (
auto res =
str2lvl(chunk); res.has_value()) {
80 auto level = res.value();
85 std::istringstream iss2(chunk);
87 std::string group_name;
88 if (not std::getline(iss2, group_name,
'=')) {
89 std::cerr <<
"Can't read group";
91 if (not logging_system_->getGroup(group_name)) {
92 std::cerr <<
"Unknown group: " << group_name << std::endl;
96 std::string level_string;
97 if (not std::getline(iss2, level_string)) {
98 std::cerr <<
"Can't read level for group '" << group_name <<
"'";
101 auto res =
str2lvl(level_string);
102 if (not res.has_value()) {
103 std::cerr <<
"Invalid level: " << level_string << std::endl;
106 auto level = res.value();
108 logging_system_->setLevelOfGroup(group_name, level);
113 ensure_logger_system_is_initialized();
114 return std::static_pointer_cast<soralog::LoggerFactory>(logging_system_)
119 ensure_logger_system_is_initialized();
120 return std::static_pointer_cast<soralog::LoggerFactory>(logging_system_)
121 ->getLogger(tag, group);
125 const std::string &group,
127 ensure_logger_system_is_initialized();
128 return std::static_pointer_cast<soralog::LoggerFactory>(logging_system_)
129 ->getLogger(tag, group, level);
133 ensure_logger_system_is_initialized();
134 return logging_system_->setLevelOfGroup(group_name, level);
137 ensure_logger_system_is_initialized();
138 return logging_system_->resetLevelOfGroup(group_name);
142 ensure_logger_system_is_initialized();
143 return logging_system_->setLevelOfLogger(logger_name, level);
146 ensure_logger_system_is_initialized();
147 return logging_system_->resetLevelOfLogger(logger_name);
bool resetLevelOfGroup(const std::string &group_name)
void tuneLoggingSystem(const std::vector< std::string > &cfg)
bool resetLevelOfLogger(const std::string &logger_name)
bool setLevelOfLogger(const std::string &logger_name, Level level)
std::shared_ptr< soralog::Logger > Logger
void setLoggingSystem(std::shared_ptr< soralog::LoggingSystem > logging_system)
OUTCOME_CPP_DEFINE_CATEGORY(kagome::log, Error, e)
static const std::string defaultGroupName("kagome")
outcome::result< Level > str2lvl(std::string_view str)
Logger createLogger(const std::string &tag)
bool setLevelOfGroup(const std::string &group_name, Level level)