diff options
author | gabime <gmelman1@gmail.com> | 2019-12-21 18:47:02 +0300 |
---|---|---|
committer | gabime <gmelman1@gmail.com> | 2019-12-21 18:47:02 +0300 |
commit | 773b8c5a54317f18a60f2cb1c39257db548056c8 (patch) | |
tree | 7f99c15e00a2d4ec9915c876ab758aa606305109 /include/spdlog/cfg | |
parent | fc3d18ed64715b43a1cab919e59acc9f950fb06d (diff) |
refectored file names
Diffstat (limited to 'include/spdlog/cfg')
-rw-r--r-- | include/spdlog/cfg/argv.h (renamed from include/spdlog/cfg/argv_loader.h) | 12 | ||||
-rw-r--r-- | include/spdlog/cfg/env.h | 14 | ||||
-rw-r--r-- | include/spdlog/cfg/helpers-inl.h (renamed from include/spdlog/cfg/text_loader-inl.h) | 15 | ||||
-rw-r--r-- | include/spdlog/cfg/helpers.h | 26 | ||||
-rw-r--r-- | include/spdlog/cfg/log_levels.h (renamed from include/spdlog/cfg/cfg.h) | 2 | ||||
-rw-r--r-- | include/spdlog/cfg/text_loader.h | 38 |
6 files changed, 48 insertions, 59 deletions
diff --git a/include/spdlog/cfg/argv_loader.h b/include/spdlog/cfg/argv.h index 45872673..b690032c 100644 --- a/include/spdlog/cfg/argv_loader.h +++ b/include/spdlog/cfg/argv.h @@ -2,7 +2,7 @@ // Distributed under the MIT License (http://opensource.org/licenses/MIT) #pragma once -#include <spdlog/cfg/text_loader.h> +#include <spdlog/cfg/helpers.h> #include <spdlog/details/os.h> // @@ -11,8 +11,9 @@ namespace spdlog { namespace cfg { +namespace argv { // search for SPDLOG_LEVEL= in the args and use it to init the levels -void init_from_argv(int args, char *argv[]) +void load_levels(int args, char *argv[]) { const std::string spdlog_level_prefix = "SPDLOG_LEVEL="; for (int i = 1; i < args; i++) @@ -20,11 +21,12 @@ void init_from_argv(int args, char *argv[]) std::string arg = argv[i]; if (arg.find(spdlog_level_prefix) == 0) { - auto cfg_string = arg.substr(spdlog_level_prefix.size()); - auto levels = text_loader::load_levels(cfg_string); - spdlog::details::registry::instance().set_levels(levels); + auto levels_string = arg.substr(spdlog_level_prefix.size()); + auto levels = helpers::extract_levels(levels_string); + details::registry::instance().update_levels(std::move(levels)); } } } +} // namespace argv } // namespace cfg } // namespace spdlog diff --git a/include/spdlog/cfg/env.h b/include/spdlog/cfg/env.h index 5ef86f99..3700eeb7 100644 --- a/include/spdlog/cfg/env.h +++ b/include/spdlog/cfg/env.h @@ -2,7 +2,8 @@ // Distributed under the MIT License (http://opensource.org/licenses/MIT) #pragma once -#include <spdlog/cfg/text_loader.h> +#include <spdlog/cfg/helpers.h> +#include <spdlog/details/registry.h> #include <spdlog/details/os.h> // @@ -23,11 +24,14 @@ namespace spdlog { namespace cfg { -void init_from_env() +namespace env { +void load_levels() { - auto cfg = details::os::getenv("SPDLOG_LEVEL"); - auto levels = text_loader::load_levels(cfg); - spdlog::details::registry::instance().set_levels(levels); + auto env_val = details::os::getenv("SPDLOG_LEVEL"); + auto levels = helpers::extract_levels(env_val); + details::registry::instance().update_levels(std::move(levels)); } +} // namespace env + } // namespace cfg } // namespace spdlog diff --git a/include/spdlog/cfg/text_loader-inl.h b/include/spdlog/cfg/helpers-inl.h index d1a52bfa..87c270e3 100644 --- a/include/spdlog/cfg/text_loader-inl.h +++ b/include/spdlog/cfg/helpers-inl.h @@ -4,7 +4,7 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include <spdlog/cfg/text_loader.h> +#include <spdlog/cfg/helpers.h> #endif #include <spdlog/spdlog.h> @@ -17,11 +17,11 @@ namespace spdlog { namespace cfg { -namespace text_loader { +namespace helpers { using name_val_pair = std::pair<std::string, std::string>; -// inplace convert to lowercase +// inplace convert to lowercase inline std::string &to_lower_(std::string &str) { std::transform( @@ -62,7 +62,7 @@ inline name_val_pair extract_kv_(char sep, const std::string &str) // return vector of key/value pairs from sequence of "K1=V1,K2=V2,.." // "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...} -SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str) +inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str) { std::string token; std::istringstream token_stream(str); @@ -79,7 +79,7 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(con return rv; } -inline log_levels extract_levels_(const std::string &input) +SPDLOG_INLINE log_levels extract_levels(const std::string &input) { auto key_vals = extract_key_vals_(input); log_levels rv; @@ -99,11 +99,6 @@ inline log_levels extract_levels_(const std::string &input) return rv; } -SPDLOG_INLINE log_levels load_levels(const std::string &txt) -{ - return extract_levels_(txt); -} - } // namespace text_loader } // namespace cfg } // namespace spdlog diff --git a/include/spdlog/cfg/helpers.h b/include/spdlog/cfg/helpers.h new file mode 100644 index 00000000..c9738770 --- /dev/null +++ b/include/spdlog/cfg/helpers.h @@ -0,0 +1,26 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/cfg/log_levels.h> +#include <spdlog/common.h> +#include <string> +#include <unordered_map> + +// +// Init levels from given string +// + +namespace spdlog { +namespace cfg { +namespace helpers { +log_levels extract_levels(const std::string &txt); +} + +} // namespace cfg +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY +#include "helpers-inl.h" +#endif // SPDLOG_HEADER_ONLY diff --git a/include/spdlog/cfg/cfg.h b/include/spdlog/cfg/log_levels.h index d7a90445..db7a958d 100644 --- a/include/spdlog/cfg/cfg.h +++ b/include/spdlog/cfg/log_levels.h @@ -39,7 +39,7 @@ public: } private: - std::unordered_map<std::string, spdlog::level::level_enum> levels_; + levels_map levels_; spdlog::level::level_enum default_level_ = level::info; }; } // namespace cfg diff --git a/include/spdlog/cfg/text_loader.h b/include/spdlog/cfg/text_loader.h deleted file mode 100644 index a0c8cd3f..00000000 --- a/include/spdlog/cfg/text_loader.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include <spdlog/cfg/cfg.h> -#include <spdlog/common.h> -#include <string> -#include <unordered_map> - -// -// 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" - -namespace spdlog { -namespace cfg { -namespace text_loader { -log_levels load_levels(const std::string &txt); -} - -} // namespace cfg -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "text_loader-inl.h" -#endif // SPDLOG_HEADER_ONLY |