Refactored to cfg::log_levels class

This commit is contained in:
gabime
2019-12-21 15:54:09 +02:00
parent 65ada37399
commit 68ed281461
7 changed files with 68 additions and 52 deletions

View File

@@ -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
View 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

View File

@@ -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

View File

@@ -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