Photon 1.0.0
Loading...
Searching...
No Matches
helpers-inl.h
Go to the documentation of this file.
1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6#ifndef SPDLOG_HEADER_ONLY
8#endif
9
10#include <spdlog/spdlog.h>
11#include <spdlog/details/os.h>
13
14#include <algorithm>
15#include <string>
16#include <utility>
17#include <sstream>
18
19namespace spdlog
20{
21 namespace cfg
22 {
23 namespace helpers
24 {
25
26 // inplace convert to lowercase
27 inline std::string& to_lower_(std::string& str)
28 {
29 std::transform(
30 str.begin(), str.end(), str.begin(), [](char ch) { return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); });
31 return str;
32 }
33
34 // inplace trim spaces
35 inline std::string& trim_(std::string& str)
36 {
37 const char* spaces = " \n\r\t";
38 str.erase(str.find_last_not_of(spaces) + 1);
39 str.erase(0, str.find_first_not_of(spaces));
40 return str;
41 }
42
43 // return (name,value) trimmed pair from given "name=value" string.
44 // return empty string on missing parts
45 // "key=val" => ("key", "val")
46 // " key = val " => ("key", "val")
47 // "key=" => ("key", "")
48 // "val" => ("", "val")
49
50 inline std::pair<std::string, std::string> extract_kv_(char sep, const std::string& str)
51 {
52 auto n = str.find(sep);
53 std::string k, v;
54 if (n == std::string::npos)
55 {
56 v = str;
57 }
58 else
59 {
60 k = str.substr(0, n);
61 v = str.substr(n + 1);
62 }
63 return std::make_pair(trim_(k), trim_(v));
64 }
65
66 // return vector of key/value pairs from sequence of "K1=V1,K2=V2,.."
67 // "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...}
68 inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string& str)
69 {
70 std::string token;
71 std::istringstream token_stream(str);
72 std::unordered_map<std::string, std::string> rv{};
73 while (std::getline(token_stream, token, ','))
74 {
75 if (token.empty())
76 {
77 continue;
78 }
79 auto kv = extract_kv_('=', token);
80 rv[kv.first] = kv.second;
81 }
82 return rv;
83 }
84
85 SPDLOG_INLINE void load_levels(const std::string& input)
86 {
87 if (input.empty() || input.size() > 512)
88 {
89 return;
90 }
91
92 auto key_vals = extract_key_vals_(input);
93 std::unordered_map<std::string, level::level_enum> levels;
94 level::level_enum global_level = level::info;
95 bool global_level_found = false;
96
97 for (auto& name_level : key_vals)
98 {
99 auto& logger_name = name_level.first;
100 auto level_name = to_lower_(name_level.second);
101 auto level = level::from_str(level_name);
102 // ignore unrecognized level names
103 if (level == level::off && level_name != "off")
104 {
105 continue;
106 }
107 if (logger_name.empty()) // no logger name indicate global level
108 {
109 global_level_found = true;
110 global_level = level;
111 }
112 else
113 {
114 levels[logger_name] = level;
115 }
116 }
117
118 details::registry::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
119 }
120
121 } // namespace helpers
122 } // namespace cfg
123} // namespace spdlog
void set_levels(log_levels levels, level::level_enum *global_level)
Definition registry-inl.h:265
static registry & instance()
Definition registry-inl.h:286
#define SPDLOG_INLINE
Definition common.h:47
std::unordered_map< std::string, std::string > extract_key_vals_(const std::string &str)
Definition helpers-inl.h:68
std::pair< std::string, std::string > extract_kv_(char sep, const std::string &str)
Definition helpers-inl.h:50
std::string & to_lower_(std::string &str)
Definition helpers-inl.h:27
std::string & trim_(std::string &str)
Definition helpers-inl.h:35
SPDLOG_INLINE void load_levels(const std::string &input)
Definition helpers-inl.h:85
level_enum
Definition common.h:233
@ info
Definition common.h:236
@ off
Definition common.h:240
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
Definition common-inl.h:35
Definition async.h:26
annotation input
Definition tag_strings.h:114