mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 08:25:43 +08:00
Refactored to cfg::log_levels class
This commit is contained in:
parent
65ada37399
commit
68ed281461
@ -25,9 +25,8 @@ void syslog_example();
|
||||
|
||||
int main(int args, char *argv[])
|
||||
{
|
||||
spdlog::cfg::init_from_env();
|
||||
//spdlog::cfg::init_from_env();
|
||||
spdlog::cfg::init_from_argv(args, argv);
|
||||
spdlog::debug("HELLO DEBUG");
|
||||
spdlog::info("HELLO INFO");
|
||||
|
||||
auto l1 = spdlog::stderr_color_st("l1");
|
||||
|
@ -6,32 +6,17 @@
|
||||
#include <spdlog/details/os.h>
|
||||
|
||||
//
|
||||
// Init levels and patterns from env variables SPDLOG_LEVEL
|
||||
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
|
||||
// Note - fallback to "info" level on unrecognized levels
|
||||
//
|
||||
// Examples:
|
||||
//
|
||||
// set global level to debug:
|
||||
// export SPDLOG_LEVEL=debug
|
||||
//
|
||||
// turn off all logging except for logger1:
|
||||
// export SPDLOG_LEVEL="off,logger1=debug"
|
||||
//
|
||||
// turn off all logging except for logger1 and logger2:
|
||||
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
|
||||
// Init levels from argv SPDLOG_LEVEL
|
||||
// Example: my_program.exe "SPDLOG_LEVEL=trace"
|
||||
|
||||
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
// search for SPDLOG_LEVEL= in the args and use it to init the levels
|
||||
void init_from_argv(int args, char *argv[])
|
||||
{
|
||||
if (args < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
const std::string spdlog_level_prefix = "SPDLOG_LEVEL=";
|
||||
for (int i = 0; i < args; i++)
|
||||
for (int i = 1; i < args; i++)
|
||||
{
|
||||
std::string arg = argv[i];
|
||||
if (arg.find(spdlog_level_prefix) == 0)
|
||||
@ -39,7 +24,6 @@ void init_from_argv(int args, char *argv[])
|
||||
auto cfg_string = arg.substr(spdlog_level_prefix.size());
|
||||
auto levels = text_loader::load_levels(cfg_string);
|
||||
spdlog::details::registry::instance().set_levels(levels);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
49
include/spdlog/cfg/cfg.h
Normal file
49
include/spdlog/cfg/cfg.h
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/common.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
class log_levels
|
||||
{
|
||||
|
||||
public:
|
||||
using levels_map = std::unordered_map<std::string, spdlog::level::level_enum>;
|
||||
|
||||
void set(const std::string& logger_name, level::level_enum lvl)
|
||||
{
|
||||
if (logger_name.empty())
|
||||
{
|
||||
default_level_ = lvl;
|
||||
}
|
||||
else
|
||||
{
|
||||
levels_[logger_name] = lvl;
|
||||
}
|
||||
}
|
||||
|
||||
level::level_enum get(const std::string &logger_name)
|
||||
{
|
||||
auto it = levels_.find(logger_name);
|
||||
return it != levels_.end() ? it->second : default_level_;
|
||||
}
|
||||
|
||||
level::level_enum get()
|
||||
{
|
||||
return default_level_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, spdlog::level::level_enum> levels_;
|
||||
spdlog::level::level_enum default_level_ = level::info;
|
||||
|
||||
};
|
||||
} // namespace cfg
|
||||
} // namespace spdlog
|
||||
|
||||
|
@ -79,10 +79,10 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(con
|
||||
return rv;
|
||||
}
|
||||
|
||||
inline details::registry::logger_levels extract_levels_(const std::string &input)
|
||||
inline log_levels extract_levels_(const std::string &input)
|
||||
{
|
||||
auto key_vals = extract_key_vals_(input);
|
||||
details::registry::logger_levels rv;
|
||||
log_levels rv;
|
||||
|
||||
for (auto &name_level : key_vals)
|
||||
{
|
||||
@ -94,22 +94,15 @@ inline details::registry::logger_levels extract_levels_(const std::string &input
|
||||
{
|
||||
level = level::info;
|
||||
}
|
||||
rv.set(logger_name, level);
|
||||
|
||||
if (logger_name.empty() || logger_name == "*")
|
||||
{
|
||||
rv.default_level = level;
|
||||
}
|
||||
else
|
||||
{
|
||||
rv.levels[logger_name] = level;
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
SPDLOG_INLINE details::registry::logger_levels load_levels(const std::string &cfg)
|
||||
SPDLOG_INLINE log_levels load_levels(const std::string &txt)
|
||||
{
|
||||
return extract_levels_(cfg);
|
||||
return extract_levels_(txt);
|
||||
}
|
||||
|
||||
} // namespace text_loader
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/cfg/cfg.h>
|
||||
#include <spdlog/common.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
@ -26,7 +27,7 @@
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
namespace text_loader {
|
||||
details::registry::logger_levels load_levels(const std::string &cfg);
|
||||
log_levels load_levels(const std::string &txt);
|
||||
}
|
||||
|
||||
} // namespace cfg
|
||||
|
@ -30,11 +30,6 @@
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
SPDLOG_INLINE level::level_enum registry::logger_levels::get_or_default(const std::string &name)
|
||||
{
|
||||
auto it = levels.find(name);
|
||||
return it != levels.end() ? it->second : default_level;
|
||||
}
|
||||
|
||||
SPDLOG_INLINE registry::registry()
|
||||
: formatter_(new pattern_formatter())
|
||||
@ -70,7 +65,7 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logge
|
||||
new_logger->set_error_handler(err_handler_);
|
||||
}
|
||||
|
||||
new_logger->set_level(levels_.get_or_default(new_logger->name()));
|
||||
new_logger->set_level(levels_.get(new_logger->name()));
|
||||
new_logger->flush_on(flush_level_);
|
||||
|
||||
if (backtrace_n_messages_ > 0)
|
||||
@ -174,7 +169,7 @@ SPDLOG_INLINE void registry::set_level(level::level_enum log_level)
|
||||
{
|
||||
l.second->set_level(log_level);
|
||||
}
|
||||
levels_.default_level = log_level;
|
||||
levels_.set("*", log_level);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
||||
@ -266,14 +261,14 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat
|
||||
automatic_registration_ = automatic_registration;
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void registry::set_levels(logger_levels levels)
|
||||
SPDLOG_INLINE void registry::set_levels(cfg::log_levels levels)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||
levels_ = std::move(levels);
|
||||
for (auto &l : loggers_)
|
||||
{
|
||||
auto &logger = l.second;
|
||||
logger->set_level(levels_.get_or_default(logger->name()));
|
||||
logger->set_level(levels_.get(logger->name()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
// This class is thread safe
|
||||
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/cfg/cfg.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
@ -27,12 +28,6 @@ class periodic_worker;
|
||||
class registry
|
||||
{
|
||||
public:
|
||||
struct logger_levels
|
||||
{
|
||||
std::unordered_map<std::string, spdlog::level::level_enum> levels;
|
||||
spdlog::level::level_enum default_level = level::info;
|
||||
spdlog::level::level_enum get_or_default(const std::string &name);
|
||||
};
|
||||
|
||||
registry(const registry &) = delete;
|
||||
registry &operator=(const registry &) = delete;
|
||||
@ -86,7 +81,7 @@ public:
|
||||
|
||||
void set_automatic_registration(bool automatic_registration);
|
||||
|
||||
void set_levels(logger_levels levels);
|
||||
void set_levels(cfg::log_levels levels);
|
||||
|
||||
static registry &instance();
|
||||
|
||||
@ -99,7 +94,7 @@ private:
|
||||
std::mutex logger_map_mutex_, flusher_mutex_;
|
||||
std::recursive_mutex tp_mutex_;
|
||||
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
||||
logger_levels levels_;
|
||||
cfg::log_levels levels_;
|
||||
std::unique_ptr<formatter> formatter_;
|
||||
level::level_enum flush_level_ = level::off;
|
||||
void (*err_handler_)(const std::string &msg);
|
||||
|
Loading…
Reference in New Issue
Block a user