You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/include/spdlog/cfg/env-inl.h

132 lines
3.3 KiB
C

6 years ago
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#ifndef SPDLOG_HEADER_ONLY
#include "spdlog/cfg/env.h"
#endif
#include "spdlog/spdlog.h"
#include "spdlog/details/os.h"
#include "spdlog/details/registry.h"
#include <string>
#include <tuple>
#include <sstream>
#include <cctype>
#include <algorithm>
namespace spdlog {
6 years ago
namespace cfg {
// inplace convert to lowercase
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;
}
6 years ago
6 years ago
// 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;
}
using name_val_pair = std::pair<std::string, std::string>;
// return tuple with name, value from "name=value" string. replace with empty string on missing parts
inline name_val_pair extract_kv_(char sep, const std::string &str)
{
auto n = str.find(sep);
std::string k;
std::string v;
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));
}
// return vector of name/value pairs from str.
// str format: "a=A,b=B,c=C,d=D,.."
SPDLOG_INLINE std::vector<name_val_pair> extract_name_vals_(const std::string &str)
{
std::vector<name_val_pair> rv;
std::string token;
std::istringstream tokenStream(str);
while (std::getline(tokenStream, token, ','))
{
rv.push_back(extract_kv_('=', token));
}
return rv;
}
6 years ago
6 years ago
inline void load_levels_(cfg_map &configs)
{
using details::os::getenv;
std::string levels = getenv("SPDLOG_LEVEL");
auto name_vals = extract_name_vals_(levels);
6 years ago
6 years ago
for (auto &nv : name_vals)
{
auto logger_name = nv.first.empty() ? "*" : nv.first;
auto level_lowercase = to_lower_(nv.second);
auto log_level = level::from_str(level_lowercase);
// set as info if unknown log level given
if (log_level == level::off && level_lowercase != "off")
6 years ago
{
6 years ago
log_level = spdlog::level::info;
6 years ago
}
6 years ago
auto it = configs.find(logger_name);
if (it != configs.end())
6 years ago
{
6 years ago
it->second.level = log_level;
6 years ago
}
6 years ago
else
6 years ago
{
6 years ago
configs.insert({logger_name, logger_cfg{log_level, "%+"}});
6 years ago
}
6 years ago
}
}
6 years ago
6 years ago
SPDLOG_INLINE void load_patterns_(cfg_map &configs)
{
using details::os::getenv;
std::string patterns = getenv("SPDLOG_PATTERN");
auto name_vals = extract_name_vals_(patterns);
for (auto &nv : name_vals)
{
auto logger_name = nv.first.empty() ? "*" : nv.first;
auto pattern = to_lower_(nv.second);
auto it = configs.find(logger_name);
if (it != configs.end())
6 years ago
{
6 years ago
it->second.pattern = pattern;
6 years ago
}
6 years ago
else
6 years ago
{
6 years ago
configs.insert({logger_name, logger_cfg{level::info, pattern}});
6 years ago
}
6 years ago
}
}
SPDLOG_INLINE cfg_map from_env()
{
cfg_map configs;
load_levels_(configs);
load_patterns_(configs);
return configs;
}
6 years ago
6 years ago
} // namespace cfg
6 years ago
} // namespace spdlog