1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-04-01 02:42:41 +08:00
spdlog/include/spdlog/loaders/helpers-inl.h

104 lines
2.6 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)
#pragma once
#ifndef SPDLOG_HEADER_ONLY
2019-12-22 01:45:14 +08:00
#include <spdlog/loaders/helpers.h>
2019-12-08 23:07:52 +08:00
#endif
#include <spdlog/spdlog.h>
#include <spdlog/details/os.h>
#include <spdlog/details/registry.h>
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
#include <sstream>
2019-12-08 23:09:17 +08:00
2019-12-08 23:07:52 +08:00
namespace spdlog {
2019-12-22 01:45:14 +08:00
namespace loaders {
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
2019-12-08 23:08:20 +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); });
return str;
}
2019-12-08 23:07:52 +08:00
2019-12-08 23:08:20 +08:00
// 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;
}
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
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;
2019-12-08 23:08:20 +08:00
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));
}
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"),...}
2019-12-21 23:47:02 +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{};
2019-12-11 06:13:27 +08:00
while (std::getline(token_stream, token, ','))
2019-12-08 23:08:20 +08:00
{
2019-12-11 07:24:51 +08:00
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
2019-12-21 23:47:02 +08:00
SPDLOG_INLINE log_levels extract_levels(const std::string &input)
2019-12-08 23:08:20 +08:00
{
2019-12-13 21:14:20 +08:00
auto key_vals = extract_key_vals_(input);
2019-12-21 21:54:09 +08:00
log_levels rv;
2019-12-08 23:07:52 +08:00
for (auto &name_level : key_vals)
2019-12-08 23:08:20 +08:00
{
2019-12-11 06:13:27 +08:00
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")
2019-12-08 23:07:52 +08:00
{
level = level::info;
2019-12-08 23:07:52 +08:00
}
2019-12-21 21:54:09 +08:00
rv.set(logger_name, level);
2019-12-08 23:08:20 +08:00
}
return rv;
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-22 01:45:14 +08:00
} // namespace loaders
2019-12-08 23:07:52 +08:00
} // namespace spdlog