diff --git a/example/example.cpp b/example/example.cpp index a9fca546..20ccb8df 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -21,12 +21,14 @@ void syslog_example(); #include #include #include -#include +#include int main(int args, char *argv[]) { - // spdlog::cfg::init_from_env(); - spdlog::cfg::init_from_argv(args, argv); + + + //spdlog::cfg::env::load_levels(); + spdlog::cfg::argv::load_levels(args, argv); spdlog::info("HELLO INFO"); auto l1 = spdlog::stderr_color_st("l1"); diff --git a/include/spdlog/cfg/argv_loader.h b/include/spdlog/cfg/argv.h similarity index 66% rename from include/spdlog/cfg/argv_loader.h rename to 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 +#include #include // @@ -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 +#include +#include #include // @@ -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 similarity index 87% rename from include/spdlog/cfg/text_loader-inl.h rename to 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 +#include #endif #include @@ -17,11 +17,11 @@ namespace spdlog { namespace cfg { -namespace text_loader { +namespace helpers { using name_val_pair = std::pair; -// 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 extract_key_vals_(const std::string &str) +inline std::unordered_map extract_key_vals_(const std::string &str) { std::string token; std::istringstream token_stream(str); @@ -79,7 +79,7 @@ SPDLOG_INLINE std::unordered_map 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 +#include +#include +#include + +// +// 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 similarity index 93% rename from include/spdlog/cfg/cfg.h rename to 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 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 -#include -#include -#include - -// -// 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 diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index 21827279..2202b4cc 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -260,7 +260,7 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat automatic_registration_ = automatic_registration; } -SPDLOG_INLINE void registry::set_levels(cfg::log_levels levels) +SPDLOG_INLINE void registry::update_levels(cfg::log_levels levels) { std::lock_guard lock(logger_map_mutex_); levels_ = std::move(levels); diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 612a2412..e7adb541 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -9,7 +9,7 @@ // This class is thread safe #include -#include +#include #include #include @@ -80,7 +80,7 @@ public: void set_automatic_registration(bool automatic_registration); - void set_levels(cfg::log_levels levels); + void update_levels(cfg::log_levels levels); static registry &instance(); diff --git a/src/spdlog.cpp b/src/spdlog.cpp index 9882768c..bd47ea8c 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -17,7 +17,7 @@ #include "spdlog/sinks/sink-inl.h" #include "spdlog/sinks/base_sink-inl.h" #include "spdlog/details/null_mutex.h" -#include "spdlog/cfg/text_loader-inl.h" +#include "spdlog/cfg/helpers-inl.h" #include