mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
Simplify and removed SPDLOG_PATTERN support
This commit is contained in:
parent
cd8d7e6de9
commit
3d203aa7c4
@ -6,13 +6,18 @@
|
|||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
#include <spdlog/cfg/env.h>
|
#include <spdlog/cfg/env.h>
|
||||||
|
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||||
|
|
||||||
int main(int, char *[])
|
int main(int, char *[])
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
spdlog::env::init();
|
spdlog::env::load_levels();
|
||||||
spdlog::info("Hello");
|
auto l1 = spdlog::stderr_color_st("l1");
|
||||||
|
auto l2 = spdlog::stderr_color_st("l2");
|
||||||
|
spdlog::info("Hello default logger");
|
||||||
|
l1->debug("Hello l1");
|
||||||
|
l2->trace("Hello l2");
|
||||||
}
|
}
|
||||||
catch (spdlog::spdlog_ex &ex)
|
catch (spdlog::spdlog_ex &ex)
|
||||||
{
|
{
|
||||||
|
@ -4,12 +4,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/cfg/env.h"
|
#include <spdlog/cfg/env.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/spdlog.h"
|
#include <spdlog/spdlog.h>
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/details/os.h>
|
||||||
#include "spdlog/details/registry.h"
|
#include <spdlog/details/registry.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@ -74,73 +74,43 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(con
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto kv = extract_kv_('=', token);
|
auto kv = extract_kv_('=', token);
|
||||||
|
rv[kv.first] = kv.second;
|
||||||
// '*' marks all loggers
|
|
||||||
if (kv.first.empty())
|
|
||||||
{
|
|
||||||
kv.first = "*";
|
|
||||||
}
|
|
||||||
rv.insert(std::move(kv));
|
|
||||||
}
|
}
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline details::registry::logger_cfgs from_env_()
|
inline details::registry::logger_levels from_env_()
|
||||||
{
|
{
|
||||||
using details::os::getenv;
|
using details::os::getenv;
|
||||||
details::registry::logger_cfgs configs;
|
auto key_vals = extract_key_vals_(getenv("SPDLOG_LEVEL"));
|
||||||
|
details::registry::logger_levels rv;
|
||||||
|
|
||||||
auto levels = extract_key_vals_(getenv("SPDLOG_LEVEL"));
|
for (auto &name_level : key_vals)
|
||||||
auto patterns = extract_key_vals_(getenv("SPDLOG_PATTERN"));
|
|
||||||
|
|
||||||
// merge to single dict. and take into account "*"
|
|
||||||
for (auto &name_level : levels)
|
|
||||||
{
|
{
|
||||||
auto &logger_name = name_level.first;
|
auto &logger_name = name_level.first;
|
||||||
assert(!logger_name.empty());
|
|
||||||
auto level_name = to_lower_(name_level.second);
|
auto level_name = to_lower_(name_level.second);
|
||||||
details::registry::logger_cfg cfg;
|
auto level = level::from_str(level_name);
|
||||||
cfg.level_name = level_name;
|
// fallback to "info" if unrecognized level name
|
||||||
if (logger_name == "*")
|
if (level == level::off && level_name != "off")
|
||||||
{
|
{
|
||||||
configs.default_cfg.level_name = cfg.level_name;
|
level = level::info;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (logger_name.empty() || logger_name == "*")
|
||||||
|
{
|
||||||
|
rv.default_level = level;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
configs.loggers.emplace(logger_name, cfg);
|
rv.levels[logger_name] = level;
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &name_pattern : patterns)
|
|
||||||
{
|
|
||||||
auto &logger_name = name_pattern.first;
|
|
||||||
auto &pattern = name_pattern.second;
|
|
||||||
auto it = configs.loggers.find(logger_name);
|
|
||||||
|
|
||||||
if (it != configs.loggers.end())
|
|
||||||
{
|
|
||||||
it->second.pattern = pattern;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (logger_name == "*")
|
|
||||||
{
|
|
||||||
configs.default_cfg.pattern = pattern;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
details::registry::logger_cfg cfg;
|
|
||||||
cfg.pattern = pattern;
|
|
||||||
configs.loggers.emplace(logger_name, cfg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return rv;
|
||||||
return configs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void init()
|
SPDLOG_INLINE void load_levels()
|
||||||
{
|
{
|
||||||
spdlog::details::registry::instance().set_configs(from_env_());
|
spdlog::details::registry::instance().set_levels(from_env_());
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace env
|
} // namespace env
|
||||||
|
@ -8,8 +8,9 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
//
|
//
|
||||||
// Init levels and patterns from env variables SPDLOG_LEVEL and SPDLOG_PATTERN.
|
// Init levels and patterns from env variables SPDLOG_LEVEL
|
||||||
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
|
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
|
||||||
|
// Note - fallback to "info" level on unrecognized levels
|
||||||
//
|
//
|
||||||
// Examples:
|
// Examples:
|
||||||
//
|
//
|
||||||
@ -21,19 +22,10 @@
|
|||||||
//
|
//
|
||||||
// turn off all logging except for logger1 and logger2:
|
// turn off all logging except for logger1 and logger2:
|
||||||
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
|
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
|
||||||
//
|
|
||||||
// set global pattern:
|
|
||||||
// export SPDLOG_PATTERN="[%x] [%l] [%n] %v"
|
|
||||||
//
|
|
||||||
// set pattern for logger1:
|
|
||||||
// export SPDLOG_PATTERN="logger1=%v,*=[%x] [%l] [%n] %v"
|
|
||||||
//
|
|
||||||
// set global pattern and different pattern for logger1:
|
|
||||||
// export SPDLOG_PATTERN="[%x] [%l] [%n] %v,logger1=[%u] %v"
|
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace env {
|
namespace env {
|
||||||
void init();
|
void load_levels();
|
||||||
}
|
}
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
|
@ -30,6 +30,12 @@
|
|||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
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()
|
SPDLOG_INLINE registry::registry()
|
||||||
: formatter_(new pattern_formatter())
|
: formatter_(new pattern_formatter())
|
||||||
{
|
{
|
||||||
@ -64,7 +70,7 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logge
|
|||||||
new_logger->set_error_handler(err_handler_);
|
new_logger->set_error_handler(err_handler_);
|
||||||
}
|
}
|
||||||
|
|
||||||
new_logger->set_level(level_);
|
new_logger->set_level(levels_.get_or_default(new_logger->name()));
|
||||||
new_logger->flush_on(flush_level_);
|
new_logger->flush_on(flush_level_);
|
||||||
|
|
||||||
if (backtrace_n_messages_ > 0)
|
if (backtrace_n_messages_ > 0)
|
||||||
@ -168,7 +174,7 @@ SPDLOG_INLINE void registry::set_level(level::level_enum log_level)
|
|||||||
{
|
{
|
||||||
l.second->set_level(log_level);
|
l.second->set_level(log_level);
|
||||||
}
|
}
|
||||||
level_ = log_level;
|
levels_.default_level = log_level;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
||||||
@ -260,23 +266,15 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat
|
|||||||
automatic_registration_ = automatic_registration;
|
automatic_registration_ = automatic_registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE void registry::set_levels(logger_levels levels)
|
||||||
SPDLOG_INLINE void registry::set_configs(logger_cfgs configs)
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
|
levels_ = std::move(levels);
|
||||||
for (auto &l : loggers_)
|
for (auto &l : loggers_)
|
||||||
{
|
{
|
||||||
auto &logger = l.second;
|
auto &logger = l.second;
|
||||||
auto cfg_it = configs.loggers.find(logger->name());
|
logger->set_level(levels_.get_or_default(logger->name()));
|
||||||
|
|
||||||
// use default config if not found for this logger name
|
|
||||||
logger_cfg *cfg = cfg_it != configs.loggers.end() ? &cfg_it->second : &configs.default_cfg;
|
|
||||||
auto &level_name = cfg->level_name.empty() ? cfg->level_name: configs.default_cfg.level_name;
|
|
||||||
auto &pattern = cfg->pattern.empty() ? cfg->pattern: configs.default_cfg.pattern;
|
|
||||||
logger->set_level(level::from_str(level_name));
|
|
||||||
logger->set_formatter(details::make_unique<pattern_formatter>(pattern));
|
|
||||||
}
|
}
|
||||||
logger_cfgs_ = std::move(configs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE registry ®istry::instance()
|
SPDLOG_INLINE registry ®istry::instance()
|
||||||
@ -299,5 +297,6 @@ SPDLOG_INLINE void registry::register_logger_(std::shared_ptr<logger> new_logger
|
|||||||
throw_if_exists_(logger_name);
|
throw_if_exists_(logger_name);
|
||||||
loggers_[logger_name] = std::move(new_logger);
|
loggers_[logger_name] = std::move(new_logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
@ -27,16 +27,11 @@ class periodic_worker;
|
|||||||
class registry
|
class registry
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
struct logger_cfg
|
struct logger_levels
|
||||||
{
|
{
|
||||||
std::string level_name;
|
std::unordered_map<std::string, spdlog::level::level_enum> levels;
|
||||||
std::string pattern;
|
spdlog::level::level_enum default_level = level::info;
|
||||||
};
|
spdlog::level::level_enum get_or_default(const std::string &name);
|
||||||
|
|
||||||
struct logger_cfgs
|
|
||||||
{
|
|
||||||
std::unordered_map<std::string, logger_cfg> loggers;
|
|
||||||
logger_cfg default_cfg = {"info", "%+"};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
registry(const registry &) = delete;
|
registry(const registry &) = delete;
|
||||||
@ -91,7 +86,7 @@ public:
|
|||||||
|
|
||||||
void set_automatic_registration(bool automatic_registration);
|
void set_automatic_registration(bool automatic_registration);
|
||||||
|
|
||||||
void set_configs(logger_cfgs configs);
|
void set_levels(logger_levels levels);
|
||||||
|
|
||||||
static registry &instance();
|
static registry &instance();
|
||||||
|
|
||||||
@ -104,15 +99,14 @@ private:
|
|||||||
std::mutex logger_map_mutex_, flusher_mutex_;
|
std::mutex logger_map_mutex_, flusher_mutex_;
|
||||||
std::recursive_mutex tp_mutex_;
|
std::recursive_mutex tp_mutex_;
|
||||||
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
||||||
|
logger_levels levels_;
|
||||||
std::unique_ptr<formatter> formatter_;
|
std::unique_ptr<formatter> formatter_;
|
||||||
level::level_enum level_ = level::info;
|
|
||||||
level::level_enum flush_level_ = level::off;
|
level::level_enum flush_level_ = level::off;
|
||||||
void (*err_handler_)(const std::string &msg);
|
void (*err_handler_)(const std::string &msg);
|
||||||
std::shared_ptr<thread_pool> tp_;
|
std::shared_ptr<thread_pool> tp_;
|
||||||
std::unique_ptr<periodic_worker> periodic_flusher_;
|
std::unique_ptr<periodic_worker> periodic_flusher_;
|
||||||
std::shared_ptr<logger> default_logger_;
|
std::shared_ptr<logger> default_logger_;
|
||||||
bool automatic_registration_ = true;
|
bool automatic_registration_ = true;
|
||||||
logger_cfgs logger_cfgs_;
|
|
||||||
size_t backtrace_n_messages_ = 0;
|
size_t backtrace_n_messages_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user