File indexing completed on 2025-01-30 10:24:42
0001
0002
0003
0004 #pragma once
0005
0006 #ifndef SPDLOG_HEADER_ONLY
0007 # include <spdlog/cfg/helpers.h>
0008 #endif
0009
0010 #include <spdlog/spdlog.h>
0011 #include <spdlog/details/os.h>
0012 #include <spdlog/details/registry.h>
0013
0014 #include <algorithm>
0015 #include <string>
0016 #include <utility>
0017 #include <sstream>
0018
0019 namespace spdlog {
0020 namespace cfg {
0021 namespace helpers {
0022
0023
0024 inline std::string &to_lower_(std::string &str)
0025 {
0026 std::transform(
0027 str.begin(), str.end(), str.begin(), [](char ch) { return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); });
0028 return str;
0029 }
0030
0031
0032 inline std::string &trim_(std::string &str)
0033 {
0034 const char *spaces = " \n\r\t";
0035 str.erase(str.find_last_not_of(spaces) + 1);
0036 str.erase(0, str.find_first_not_of(spaces));
0037 return str;
0038 }
0039
0040
0041
0042
0043
0044
0045
0046
0047 inline std::pair<std::string, std::string> extract_kv_(char sep, const std::string &str)
0048 {
0049 auto n = str.find(sep);
0050 std::string k, v;
0051 if (n == std::string::npos)
0052 {
0053 v = str;
0054 }
0055 else
0056 {
0057 k = str.substr(0, n);
0058 v = str.substr(n + 1);
0059 }
0060 return std::make_pair(trim_(k), trim_(v));
0061 }
0062
0063
0064
0065 inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str)
0066 {
0067 std::string token;
0068 std::istringstream token_stream(str);
0069 std::unordered_map<std::string, std::string> rv{};
0070 while (std::getline(token_stream, token, ','))
0071 {
0072 if (token.empty())
0073 {
0074 continue;
0075 }
0076 auto kv = extract_kv_('=', token);
0077 rv[kv.first] = kv.second;
0078 }
0079 return rv;
0080 }
0081
0082 SPDLOG_INLINE void load_levels(const std::string &input)
0083 {
0084 if (input.empty() || input.size() > 512)
0085 {
0086 return;
0087 }
0088
0089 auto key_vals = extract_key_vals_(input);
0090 std::unordered_map<std::string, level::level_enum> levels;
0091 level::level_enum global_level = level::info;
0092 bool global_level_found = false;
0093
0094 for (auto &name_level : key_vals)
0095 {
0096 auto &logger_name = name_level.first;
0097 auto level_name = to_lower_(name_level.second);
0098 auto level = level::from_str(level_name);
0099
0100 if (level == level::off && level_name != "off")
0101 {
0102 continue;
0103 }
0104 if (logger_name.empty())
0105 {
0106 global_level_found = true;
0107 global_level = level;
0108 }
0109 else
0110 {
0111 levels[logger_name] = level;
0112 }
0113 }
0114
0115 details::registry::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
0116 }
0117
0118 }
0119 }
0120 }