From 2b3000dddce5989127dfbd5d0a5c5a2667c999ff Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 11 Dec 2019 01:24:51 +0200 Subject: [PATCH] wip --- example/example.cpp | 14 +++++++----- include/spdlog/cfg/env-inl.h | 40 +++++++++++++++++++++++++++++---- include/spdlog/cfg/env.h | 43 ++++++++++++++++++++++-------------- 3 files changed, 71 insertions(+), 26 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index c4c1567b..c2101f84 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -9,14 +9,16 @@ int main(int, char *[]) { - spdlog::set_pattern("%v"); try { - auto cfg = spdlog::cfg::from_env(); - for (auto &item : cfg) - { - spdlog::info("logger: '{}' level: '{}' pattern: '{}'", item.first, item.second.level_name, item.second.pattern); - } + spdlog::cfg::env::init(); + spdlog::info("Hello"); + + // auto cfg = spdlog::cfg::from_env(); + // for (auto &item : cfg) + // { + // spdlog::info("logger: '{}' level: '{}' pattern: '{}'", item.first, item.second.level_name, item.second.pattern); + // } } catch (spdlog::spdlog_ex &ex) { diff --git a/include/spdlog/cfg/env-inl.h b/include/spdlog/cfg/env-inl.h index 7c23954e..0f590070 100644 --- a/include/spdlog/cfg/env-inl.h +++ b/include/spdlog/cfg/env-inl.h @@ -17,6 +17,17 @@ namespace spdlog { namespace cfg { +namespace env { + +struct logger_cfg +{ + std::string level_name; + std::string pattern; +}; + +using cfg_map = std::unordered_map; +using name_val_pair = std::pair; + // inplace convert to lowercase inline std::string &to_lower_(std::string &str) { @@ -34,8 +45,6 @@ inline std::string &trim_(std::string &str) return str; } -using name_val_pair = std::pair; - // return (name,value) pair from given "name=value" string. // return empty string on missing parts // "key=val" => ("key", "val") @@ -67,7 +76,7 @@ SPDLOG_INLINE std::unordered_map extract_key_vals_(con std::unordered_map rv; while (std::getline(token_stream, token, ',')) { - if(token.empty()) + if (token.empty()) { continue; } @@ -83,7 +92,7 @@ SPDLOG_INLINE std::unordered_map extract_key_vals_(con return rv; } -SPDLOG_INLINE cfg_map from_env() +inline cfg_map from_env_() { using details::os::getenv; cfg_map configs; @@ -145,5 +154,28 @@ SPDLOG_INLINE cfg_map from_env() return configs; } +SPDLOG_INLINE void init() +{ + auto cfg_map = from_env_(); + for (const auto &logger_cfg : cfg_map) + { + auto &logger_name = logger_cfg.first; + auto level = level::from_str(logger_cfg.second.level_name); + auto &pattern = logger_cfg.second.pattern; + + if (logger_name == "*") + { + spdlog::set_level(level); + spdlog::set_pattern(pattern); + } + else if (auto logger = spdlog::get(logger_name)) + { + logger->set_level(level); + logger->set_pattern(pattern); + } + } +} + +} // namespace env } // namespace cfg } // namespace spdlog diff --git a/include/spdlog/cfg/env.h b/include/spdlog/cfg/env.h index b22717d4..c52149eb 100644 --- a/include/spdlog/cfg/env.h +++ b/include/spdlog/cfg/env.h @@ -7,25 +7,36 @@ #include #include -// config spdlog from environment variables +// +// Init levels and patterns from env variables SPDLOG_LEVEL and SPDLOG_PATTERN. +// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger). +// +// 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" +// +// 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 cfg { -struct logger_cfg -{ - std::string level_name; - std::string pattern; -}; -using cfg_map = std::unordered_map; +namespace env { +void init(); +} -// Init levels and patterns from env variabls SPDLOG_LEVEL & SPDLOG_PATTERN -// Examples: -// export SPDLOG_LEVEL=debug -// export SPDLOG_LEVEL=logger1=%v,*=[%x] [%l] [%n] %v -// export SPDLOG_LEVEL=logger1=debug,logger2=info,*=error -// export SPDLOG_PATTERN=[%x] [%l] [%n] %v -// -// Note: will set the level to info if finds unknown level in SPDLOG_LEVEL -cfg_map from_env(); } // namespace cfg } // namespace spdlog