Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gabime/spdlog.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgabime <gmelman1@gmail.com>2019-12-22 21:40:19 +0300
committergabime <gmelman1@gmail.com>2019-12-22 21:40:19 +0300
commit877eee408e5dae9b0c97dd2af31e90713ba4f64a (patch)
tree0e739f1a4fb1d433a878f9b412a2f54ebbe2c444 /include/spdlog/cfg/helpers-inl.h
parent8dd54de326af2063b4a1a9bffa86a4071f632d75 (diff)
renamed loaders with cfg
Diffstat (limited to 'include/spdlog/cfg/helpers-inl.h')
-rw-r--r--include/spdlog/cfg/helpers-inl.h103
1 files changed, 103 insertions, 0 deletions
diff --git a/include/spdlog/cfg/helpers-inl.h b/include/spdlog/cfg/helpers-inl.h
new file mode 100644
index 00000000..b0915073
--- /dev/null
+++ b/include/spdlog/cfg/helpers-inl.h
@@ -0,0 +1,103 @@
+// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
+// Distributed under the MIT License (http://opensource.org/licenses/MIT)
+
+#pragma once
+
+#ifndef SPDLOG_HEADER_ONLY
+#include <spdlog/cfg/helpers.h>
+#endif
+
+#include <spdlog/spdlog.h>
+#include <spdlog/details/os.h>
+#include <spdlog/details/registry.h>
+
+#include <string>
+#include <utility>
+#include <sstream>
+
+namespace spdlog {
+namespace cfg {
+namespace helpers {
+
+// 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<char>((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;
+}
+
+// return (name,value) trimmed 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 std::pair<std::string, std::string> extract_kv_(char sep, const std::string &str)
+{
+ auto n = str.find(sep);
+ std::string k, 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 key/value pairs from sequence of "K1=V1,K2=V2,.."
+// "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...}
+inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str)
+{
+ std::string token;
+ std::istringstream token_stream(str);
+ std::unordered_map<std::string, std::string> rv{};
+ while (std::getline(token_stream, token, ','))
+ {
+ if (token.empty())
+ {
+ continue;
+ }
+ auto kv = extract_kv_('=', token);
+ rv[kv.first] = kv.second;
+ }
+ return rv;
+}
+
+SPDLOG_INLINE log_levels extract_levels(const std::string &input)
+{
+ auto key_vals = extract_key_vals_(input);
+ log_levels rv;
+
+ for (auto &name_level : key_vals)
+ {
+ auto &logger_name = name_level.first;
+ auto level_name = to_lower_(name_level.second);
+ auto level = level::from_str(level_name);
+ // fallback to "info" if unrecognized level name
+ if (level == level::off && level_name != "off")
+ {
+ level = level::info;
+ }
+ rv.set(logger_name, level);
+ }
+ return rv;
+}
+
+} // namespace helpers
+} // namespace cfg
+} // namespace spdlog