wip config from env

This commit is contained in:
gabime 2019-12-11 00:13:27 +02:00
parent d0ed873ab6
commit da2c15ecb4
3 changed files with 63 additions and 51 deletions

View File

@ -9,12 +9,13 @@
int main(int, char *[]) int main(int, char *[])
{ {
spdlog::set_pattern("%v");
try try
{ {
auto cfg = spdlog::cfg::from_env(); auto cfg = spdlog::cfg::from_env();
for (auto &item : cfg) for (auto &item : cfg)
{ {
spdlog::info("['{}'] level: {} pattern: {}", item.first, spdlog::level::to_string_view(item.second.level), item.second.pattern); spdlog::info("logger: '{}' level: '{}' pattern: '{}'", item.first, item.second.level_name, item.second.pattern);
} }
} }
catch (spdlog::spdlog_ex &ex) catch (spdlog::spdlog_ex &ex)

View File

@ -37,12 +37,16 @@ inline std::string &trim_(std::string &str)
using name_val_pair = std::pair<std::string, std::string>; using name_val_pair = std::pair<std::string, std::string>;
// return tuple with name, value from "name=value" string. replace with empty string on missing parts // return (name,value) pair from given "name=value" string.
// return empty string on missing parts
// "key=val" => ("key", "val")
// " key = val " => ("key", "val")
// "key=" => ("key", "")
// "val" => ("", "val")
inline name_val_pair extract_kv_(char sep, const std::string &str) inline name_val_pair extract_kv_(char sep, const std::string &str)
{ {
auto n = str.find(sep); auto n = str.find(sep);
std::string k; std::string k, v;
std::string v;
if (n == std::string::npos) if (n == std::string::npos)
{ {
v = str; v = str;
@ -55,74 +59,86 @@ inline name_val_pair extract_kv_(char sep, const std::string &str)
return std::make_pair(trim_(k), trim_(v)); return std::make_pair(trim_(k), trim_(v));
} }
// return vector of name/value pairs from str. // return vector of key/value pairs from str.
// str format: "a=A,b=B,c=C,d=D,.." // str format: "a=A,b=B,c=C,d=D,.."
SPDLOG_INLINE std::vector<name_val_pair> extract_name_vals_(const std::string &str) SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str)
{ {
std::vector<name_val_pair> rv;
std::string token; std::string token;
std::istringstream tokenStream(str); std::istringstream token_stream(str);
while (std::getline(tokenStream, token, ',')) std::unordered_map<std::string, std::string> rv;
while (std::getline(token_stream, token, ','))
{ {
rv.push_back(extract_kv_('=', token)); auto kv = extract_kv_('=', token);
// empty logger name or '*' marks all loggers
if(kv.first.empty())
{
kv.first = "*";
}
rv.insert(std::move(kv));
} }
return rv; return rv;
} }
inline void load_levels_(cfg_map &configs) SPDLOG_INLINE cfg_map from_env()
{ {
using details::os::getenv; using details::os::getenv;
std::string levels = getenv("SPDLOG_LEVEL"); cfg_map configs;
auto name_vals = extract_name_vals_(levels);
for (auto &nv : name_vals) auto levels = extract_key_vals_(getenv("SPDLOG_LEVEL"));
auto patterns = extract_key_vals_(getenv("SPDLOG_PATTERN"));
// merge to single dict. and take into account "*"
std::string default_level_name = "info";
std::string default_pattern = "%+";
for(auto &name_level: levels)
{ {
auto logger_name = nv.first.empty() ? "*" : nv.first; auto &logger_name = name_level.first;
auto level_lowercase = to_lower_(nv.second); auto level_name = to_lower_(name_level.second);
auto log_level = level::from_str(level_lowercase); logger_cfg cfg;
// set as info if unknown log level given cfg.level_name = level_name;
if (log_level == level::off && level_lowercase != "off") configs[logger_name] = cfg;
if(logger_name == "*")
{ {
log_level = spdlog::level::info; default_level_name = cfg.level_name;
}
auto it = configs.find(logger_name);
if (it != configs.end())
{
it->second.level = log_level;
}
else
{
configs.insert({logger_name, logger_cfg{log_level, "%+"}});
}
} }
} }
SPDLOG_INLINE void load_patterns_(cfg_map &configs) for(auto &name_pattern: patterns)
{ {
using details::os::getenv; auto &logger_name = name_pattern.first;
std::string patterns = getenv("SPDLOG_PATTERN"); auto &pattern = name_pattern.second;
auto name_vals = extract_name_vals_(patterns);
for (auto &nv : name_vals)
{
auto logger_name = nv.first.empty() ? "*" : nv.first;
auto pattern = to_lower_(nv.second);
auto it = configs.find(logger_name); auto it = configs.find(logger_name);
if (it != configs.end()) if (it != configs.end())
{ {
it->second.pattern = pattern; it->second.pattern = pattern;
} }
else else
{ {
configs.insert({logger_name, logger_cfg{level::info, pattern}}); logger_cfg cfg;
cfg.pattern = pattern;
configs.insert({logger_name, cfg});
} }
if(logger_name == "*")
{
default_pattern = pattern;
} }
} }
SPDLOG_INLINE cfg_map from_env() //fill missing fields with the default values
for(auto &cfg:configs)
{ {
cfg_map configs; auto &val = cfg.second;
load_levels_(configs); if(val.pattern.empty())
load_patterns_(configs); {
val.pattern = default_pattern;
}
if(val.level_name.empty())
{
val.level_name = default_level_name;
}
}
return configs; return configs;
} }

View File

@ -12,12 +12,7 @@ namespace spdlog {
namespace cfg { namespace cfg {
struct logger_cfg struct logger_cfg
{ {
logger_cfg(level::level_enum level, std::string pattern) std::string level_name;
: level{level}
, pattern(std::move(pattern))
{}
level::level_enum level;
std::string pattern; std::string pattern;
}; };
using cfg_map = std::unordered_map<std::string, logger_cfg>; using cfg_map = std::unordered_map<std::string, logger_cfg>;