1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-03-30 01:42:40 +08:00
spdlog/src/cfg/helpers.cpp

102 lines
3.0 KiB
C++
Raw Normal View History

2019-12-08 23:07:52 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2023-09-25 01:26:32 +08:00
#include <spdlog/cfg/helpers.h>
#include <spdlog/details/registry.h>
2023-09-25 07:35:55 +08:00
#include <spdlog/spdlog.h>
2019-12-08 23:07:52 +08:00
2020-11-19 21:09:33 +08:00
#include <algorithm>
2023-09-25 07:35:55 +08:00
#include <sstream>
2019-12-08 23:07:52 +08:00
#include <string>
2019-12-08 23:09:17 +08:00
#include <utility>
2019-12-08 23:07:52 +08:00
namespace spdlog {
2019-12-23 02:40:19 +08:00
namespace cfg {
2019-12-21 23:47:02 +08:00
namespace helpers {
2019-12-11 07:24:51 +08:00
2019-12-21 23:47:02 +08:00
// inplace convert to lowercase
2023-09-25 07:35:55 +08:00
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);
});
2019-12-08 23:08:20 +08:00
return str;
}
2019-12-08 23:07:52 +08:00
2019-12-08 23:08:20 +08:00
// inplace trim spaces
2023-09-25 07:35:55 +08:00
inline std::string &trim_(std::string &str) {
2019-12-08 23:08:20 +08:00
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;
}
2019-12-13 21:14:20 +08:00
// return (name,value) trimmed pair from given "name=value" string.
2019-12-11 06:13:27 +08:00
// return empty string on missing parts
// "key=val" => ("key", "val")
// " key = val " => ("key", "val")
// "key=" => ("key", "")
// "val" => ("", "val")
2019-12-21 23:55:30 +08:00
2023-09-25 07:35:55 +08:00
inline std::pair<std::string, std::string> extract_kv_(char sep, const std::string &str) {
2019-12-08 23:08:20 +08:00
auto n = str.find(sep);
2019-12-11 06:13:27 +08:00
std::string k, v;
2023-09-25 07:35:55 +08:00
if (n == std::string::npos) {
2019-12-08 23:08:20 +08:00
v = str;
2023-09-25 07:35:55 +08:00
} else {
2019-12-08 23:08:20 +08:00
k = str.substr(0, n);
v = str.substr(n + 1);
}
return std::make_pair(trim_(k), trim_(v));
}
2019-12-11 06:42:00 +08:00
// 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"),...}
2023-09-25 07:35:55 +08:00
inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str) {
2019-12-08 23:08:20 +08:00
std::string token;
2019-12-11 06:13:27 +08:00
std::istringstream token_stream(str);
2019-12-13 23:55:39 +08:00
std::unordered_map<std::string, std::string> rv{};
2023-09-25 07:35:55 +08:00
while (std::getline(token_stream, token, ',')) {
if (token.empty()) {
2019-12-11 06:42:00 +08:00
continue;
}
2019-12-11 06:13:27 +08:00
auto kv = extract_kv_('=', token);
rv[kv.first] = kv.second;
2019-12-08 23:08:20 +08:00
}
return rv;
}
2019-12-08 23:07:52 +08:00
2023-09-25 07:35:55 +08:00
void load_levels(const std::string &input) {
if (input.empty() || input.size() > 512) {
2020-09-27 07:08:24 +08:00
return;
}
2019-12-13 21:14:20 +08:00
auto key_vals = extract_key_vals_(input);
std::unordered_map<std::string, level> levels;
level global_level = level::info;
2020-09-27 07:08:24 +08:00
bool global_level_found = false;
2019-12-08 23:07:52 +08:00
2023-09-25 07:35:55 +08:00
for (auto &name_level : key_vals) {
2023-09-23 05:27:32 +08:00
const auto &logger_name = name_level.first;
2019-12-11 06:13:27 +08:00
auto level_name = to_lower_(name_level.second);
2023-09-23 05:27:32 +08:00
auto level = level_from_str(level_name);
2020-09-26 19:41:33 +08:00
// ignore unrecognized level names
2023-09-25 07:35:55 +08:00
if (level == level::off && level_name != "off") {
2020-09-26 19:41:33 +08:00
continue;
2019-12-08 23:07:52 +08:00
}
2020-09-27 07:08:24 +08:00
if (logger_name.empty()) // no logger name indicate global level
{
global_level_found = true;
global_level = level;
2023-09-25 07:35:55 +08:00
} else {
2020-09-27 07:08:24 +08:00
levels[logger_name] = level;
}
2019-12-08 23:08:20 +08:00
}
2020-09-27 07:08:24 +08:00
details::registry::instance().set_levels(std::move(levels),
global_level_found ? &global_level : nullptr);
2019-12-08 23:08:20 +08:00
}
2019-12-08 23:07:52 +08:00
2019-12-22 01:52:37 +08:00
} // namespace helpers
2019-12-23 02:40:19 +08:00
} // namespace cfg
2019-12-08 23:07:52 +08:00
} // namespace spdlog