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