From 0f2439988736076242875c7cdfb2598c3e13661b Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 8 Dec 2019 17:08:20 +0200 Subject: [PATCH] wip --- example/example.cpp | 20 ++-- include/spdlog/cfg/env-inl.h | 194 ++++++++++++++++---------------- include/spdlog/cfg/env.h | 43 +++---- include/spdlog/details/os-inl.h | 5 +- include/spdlog/details/os.h | 2 +- include/spdlog/fmt/fmt.h | 1 - src/fmt.cpp | 40 ++----- 7 files changed, 144 insertions(+), 161 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 2135be04..e39b1d25 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -4,21 +4,21 @@ // spdlog usage example - - #include #include -int main(int, char *[]) { - - - try { - auto cfg = spdlog::cfg::from_env(); - for(auto &item:cfg) +int main(int, char *[]) +{ + try { - spdlog::info("['{}'] level: {} pattern: {}", item.first, spdlog::level::to_string_view(item.second.level), item.second.pattern); + auto cfg = spdlog::cfg::from_env(); + for (auto &item : cfg) + { + spdlog::info("['{}'] level: {} pattern: {}", item.first, spdlog::level::to_string_view(item.second.level), item.second.pattern); + } } - }catch(spdlog::spdlog_ex& ex){ + catch (spdlog::spdlog_ex &ex) + { spdlog::info("spdlog_ex: {}", ex.what()); } } diff --git a/include/spdlog/cfg/env-inl.h b/include/spdlog/cfg/env-inl.h index 7e5f297e..292332f2 100644 --- a/include/spdlog/cfg/env-inl.h +++ b/include/spdlog/cfg/env-inl.h @@ -18,114 +18,114 @@ #include namespace spdlog { - namespace cfg { - // inplace convert to lowercase - inline std::string& to_lower_(std::string &str) +namespace cfg { +// inplace convert to lowercase +inline std::string &to_lower_(std::string &str) +{ + std::transform( + str.begin(), str.end(), str.begin(), [](char ch) { return static_cast((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); }); + return str; +} + +// inplace trim spaces +inline std::string &trim_(std::string &str) +{ + const char *spaces = " \n\r\t"; + str.erase(str.find_last_not_of(spaces) + 1); + str.erase(0, str.find_first_not_of(spaces)); + return str; +} + +using name_val_pair = std::pair; + +// return tuple with name, value from "name=value" string. replace with empty string on missing parts +inline name_val_pair extract_kv_(char sep, const std::string &str) +{ + auto n = str.find(sep); + std::string k; + std::string v; + if (n == std::string::npos) + { + v = str; + } + else + { + k = str.substr(0, n); + v = str.substr(n + 1); + } + return std::make_pair(trim_(k), trim_(v)); +} + +// return vector of name/value pairs from str. +// str format: "a=A,b=B,c=C,d=D,.." +SPDLOG_INLINE std::vector extract_name_vals_(const std::string &str) +{ + std::vector rv; + std::string token; + std::istringstream tokenStream(str); + while (std::getline(tokenStream, token, ',')) + { + rv.push_back(extract_kv_('=', token)); + } + return rv; +} + +inline void load_levels_(cfg_map &configs) +{ + using details::os::getenv; + std::string levels = getenv("SPDLOG_LEVEL"); + auto name_vals = extract_name_vals_(levels); + + for (auto &nv : name_vals) + { + auto logger_name = nv.first.empty() ? "*" : nv.first; + auto level_lowercase = to_lower_(nv.second); + auto log_level = level::from_str(level_lowercase); + // set as info if unknown log level given + if (log_level == level::off && level_lowercase != "off") { - std::transform(str.begin(), str.end(), str.begin(), [](char ch) { - return static_cast((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); - }); - return str; + log_level = spdlog::level::info; } - - // inplace trim spaces - inline std::string& trim_(std::string &str) + auto it = configs.find(logger_name); + if (it != configs.end()) { - const char* spaces = " \n\r\t"; - str.erase(str.find_last_not_of(spaces) + 1); - str.erase(0, str.find_first_not_of(spaces)); - return str; + it->second.level = log_level; } - - using name_val_pair = std::pair; - - // return tuple with name, value from "name=value" string. replace with empty string on missing parts - inline name_val_pair extract_kv_(char sep, const std::string &str) + else { - auto n = str.find(sep); - std::string k; - std::string v; - if (n == std::string::npos) - { - v = str; - } else{ - k = str.substr(0, n); - v = str.substr(n + 1); - } - return std::make_pair(trim_(k), trim_(v)); + configs.insert({logger_name, logger_cfg{log_level, "%+"}}); } + } +} - // return vector of name/value pairs from str. - // str format: "a=A,b=B,c=C,d=D,.." - SPDLOG_INLINE std::vector extract_name_vals_(const std::string &str) +SPDLOG_INLINE void load_patterns_(cfg_map &configs) +{ + using details::os::getenv; + std::string patterns = getenv("SPDLOG_PATTERN"); + 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); + if (it != configs.end()) { - std::vector rv; - std::string token; - std::istringstream tokenStream(str); - while (std::getline(tokenStream, token, ',')) - { - rv.push_back(extract_kv_('=', token)); - } - return rv; + it->second.pattern = pattern; } - - - inline void load_levels_(cfg_map &configs) + else { - using details::os::getenv; - std::string levels = getenv("SPDLOG_LEVEL"); - auto name_vals = extract_name_vals_(levels); - - for (auto &nv : name_vals) - { - auto logger_name = nv.first.empty() ? "*" : nv.first; - auto level_lowercase = to_lower_(nv.second); - auto log_level = level::from_str(level_lowercase); - // set as info if unknown log level given - if (log_level == level::off && level_lowercase != "off") - { - log_level = spdlog::level::info; - } - auto it = configs.find(logger_name); - if (it != configs.end()) - { - it->second.level = log_level; - } - else - { - configs.insert({logger_name, logger_cfg{log_level, "%+"}}); - } - } + configs.insert({logger_name, logger_cfg{level::info, pattern}}); } + } +} - SPDLOG_INLINE void load_patterns_(cfg_map &configs) - { - using details::os::getenv; - std::string patterns = getenv("SPDLOG_PATTERN"); - 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); - if (it != configs.end()) - { - it->second.pattern = pattern; - } - else - { - configs.insert({logger_name, logger_cfg{level::info, pattern}}); - } - } - } +SPDLOG_INLINE cfg_map from_env() +{ + cfg_map configs; + load_levels_(configs); + load_patterns_(configs); + return configs; +} - SPDLOG_INLINE cfg_map from_env() - { - cfg_map configs; - load_levels_(configs); - load_patterns_(configs); - return configs; - } - - } // namespace cfg +} // namespace cfg } // namespace spdlog diff --git a/include/spdlog/cfg/env.h b/include/spdlog/cfg/env.h index 0cfb961b..12c46568 100644 --- a/include/spdlog/cfg/env.h +++ b/include/spdlog/cfg/env.h @@ -8,30 +8,31 @@ #include // config spdlog from environment variables -namespace spdlog +namespace spdlog { +namespace cfg { +struct logger_cfg { - namespace cfg - { - struct logger_cfg { - logger_cfg(level::level_enum level, std::string pattern): - level{level}, pattern(std::move(pattern)) {} + logger_cfg(level::level_enum level, std::string pattern) + : level{level} + , pattern(std::move(pattern)) + {} - level::level_enum level; - std::string pattern; - }; - using cfg_map = std::unordered_map; + level::level_enum level; + std::string pattern; +}; +using cfg_map = std::unordered_map; - // 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(); - } -} +// 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 #ifdef SPDLOG_HEADER_ONLY #include "env-inl.h" diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 2521a45c..dcce9a92 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -536,18 +536,17 @@ SPDLOG_INLINE filename_t dir_name(filename_t path) return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } -std::string SPDLOG_INLINE getenv(const char* field) +std::string SPDLOG_INLINE getenv(const char *field) { #if defined(_MSC_VER) && !defined(__cplusplus_winrt) size_t len = 0; char buf[128]; - bool ok = ::getenv_s(&len , buf, sizeof(buf), field) == 0; + bool ok = ::getenv_s(&len, buf, sizeof(buf), field) == 0; return ok ? buf : std::string{}; #else // revert to getenv char *buf = ::getenv(field); return buf ? buf : std::string{}; #endif - } } // namespace os diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 8ea70f9a..16b1294c 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -104,7 +104,7 @@ bool create_dir(filename_t path); // non thread safe, cross platform getenv/getenv_s // return empty string if field not found -std::string getenv(const char* field); +std::string getenv(const char *field); } // namespace os } // namespace details diff --git a/include/spdlog/fmt/fmt.h b/include/spdlog/fmt/fmt.h index a3762ba9..66a82d87 100644 --- a/include/spdlog/fmt/fmt.h +++ b/include/spdlog/fmt/fmt.h @@ -16,7 +16,6 @@ #pragma GCC diagnostic ignored "-Wsign-conversion" #endif // __GNUC__ || __clang__ - #if !defined(SPDLOG_FMT_EXTERNAL) #ifdef SPDLOG_HEADER_ONLY #ifndef FMT_HEADER_ONLY diff --git a/src/fmt.cpp b/src/fmt.cpp index 431b2a57..1b76b915 100644 --- a/src/fmt.cpp +++ b/src/fmt.cpp @@ -15,23 +15,19 @@ #if !defined(SPDLOG_FMT_EXTERNAL) #include "spdlog/fmt/bundled/format-inl.h" - // pop warnings supressions #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic pop #endif - FMT_BEGIN_NAMESPACE template struct FMT_API internal::basic_data; // Workaround a bug in MSVC2013 that prevents instantiation of format_float. -int (*instantiate_format_float)(double, int, internal::float_specs, - internal::buffer&) = -internal::format_float; +int (*instantiate_format_float)(double, int, internal::float_specs, internal::buffer &) = internal::format_float; #ifndef FMT_STATIC_THOUSANDS_SEPARATOR -template FMT_API internal::locale_ref::locale_ref(const std::locale& loc); +template FMT_API internal::locale_ref::locale_ref(const std::locale &loc); template FMT_API std::locale internal::locale_ref::get() const; #endif @@ -41,28 +37,18 @@ template FMT_API std::string internal::grouping_impl(locale_ref); template FMT_API char internal::thousands_sep_impl(locale_ref); template FMT_API char internal::decimal_point_impl(locale_ref); -template FMT_API void internal::buffer::append(const char*, const char*); +template FMT_API void internal::buffer::append(const char *, const char *); -template FMT_API void internal::arg_map::init( - const basic_format_args& args); +template FMT_API void internal::arg_map::init(const basic_format_args &args); -template FMT_API std::string internal::vformat( - string_view, basic_format_args); +template FMT_API std::string internal::vformat(string_view, basic_format_args); -template FMT_API format_context::iterator internal::vformat_to( - internal::buffer&, string_view, basic_format_args); +template FMT_API format_context::iterator internal::vformat_to(internal::buffer &, string_view, basic_format_args); -template FMT_API int internal::snprintf_float(double, int, - internal::float_specs, - internal::buffer&); -template FMT_API int internal::snprintf_float(long double, int, - internal::float_specs, - internal::buffer&); -template FMT_API int internal::format_float(double, int, internal::float_specs, - internal::buffer&); -template FMT_API int internal::format_float(long double, int, - internal::float_specs, - internal::buffer&); +template FMT_API int internal::snprintf_float(double, int, internal::float_specs, internal::buffer &); +template FMT_API int internal::snprintf_float(long double, int, internal::float_specs, internal::buffer &); +template FMT_API int internal::format_float(double, int, internal::float_specs, internal::buffer &); +template FMT_API int internal::format_float(long double, int, internal::float_specs, internal::buffer &); // Explicit instantiations for wchar_t. @@ -70,11 +56,9 @@ template FMT_API std::string internal::grouping_impl(locale_ref); template FMT_API wchar_t internal::thousands_sep_impl(locale_ref); template FMT_API wchar_t internal::decimal_point_impl(locale_ref); -template FMT_API void internal::buffer::append(const wchar_t*, - const wchar_t*); +template FMT_API void internal::buffer::append(const wchar_t *, const wchar_t *); -template FMT_API std::wstring internal::vformat( - wstring_view, basic_format_args); +template FMT_API std::wstring internal::vformat(wstring_view, basic_format_args); FMT_END_NAMESPACE #endif