mirror of https://github.com/gabime/spdlog.git
wip2
parent
365eb899ed
commit
a8594177fa
@ -1,37 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
#include "./helpers.h"
|
||||
|
||||
//
|
||||
// Init log levels using each argv entry that starts with "SPDLOG_LEVEL="
|
||||
//
|
||||
// set all loggers to debug level:
|
||||
// example.exe "SPDLOG_LEVEL=debug"
|
||||
|
||||
// set logger1 to trace level
|
||||
// example.exe "SPDLOG_LEVEL=logger1=trace"
|
||||
|
||||
// turn off all logging except for logger1 and logger2:
|
||||
// example.exe "SPDLOG_LEVEL=off,logger1=debug,logger2=info"
|
||||
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
|
||||
// search for SPDLOG_LEVEL= in the args and use it to init the levels
|
||||
inline void load_argv_levels(int argc, const char **argv) {
|
||||
const std::string spdlog_level_prefix = "SPDLOG_LEVEL=";
|
||||
for (int i = 1; i < argc; i++) {
|
||||
std::string arg = argv[i];
|
||||
if (arg.find(spdlog_level_prefix) == 0) {
|
||||
auto levels_string = arg.substr(spdlog_level_prefix.size());
|
||||
helpers::load_levels(levels_string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void load_argv_levels(int argc, char **argv) { load_argv_levels(argc, const_cast<const char **>(argv)); }
|
||||
|
||||
} // namespace cfg
|
||||
} // namespace spdlog
|
@ -1,36 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
#include "../details/context.h"
|
||||
#include "../details/os.h"
|
||||
#include "./helpers.h"
|
||||
|
||||
//
|
||||
// Init levels and patterns from env variables SPDLOG_LEVEL
|
||||
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
|
||||
// Note - fallback to "info" level on unrecognized levels
|
||||
//
|
||||
// Examples:
|
||||
//
|
||||
// set global level to debug:
|
||||
// export SPDLOG_LEVEL=debug
|
||||
//
|
||||
// turn off all logging except for logger1:
|
||||
// export SPDLOG_LEVEL="*=off,logger1=debug"
|
||||
//
|
||||
|
||||
// turn off all logging except for logger1 and logger2:
|
||||
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
|
||||
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
inline void load_env_levels() {
|
||||
auto env_val = details::os::getenv("SPDLOG_LEVEL");
|
||||
if (!env_val.empty()) {
|
||||
helpers::load_levels(env_val);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace cfg
|
||||
} // namespace spdlog
|
@ -1,25 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "../common.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
namespace helpers {
|
||||
//
|
||||
// Init levels from given string
|
||||
//
|
||||
// Examples:
|
||||
//
|
||||
// set global level to debug: "debug"
|
||||
// turn off all logging except for logger1: "off,logger1=debug"
|
||||
// turn off all logging except for logger1 and logger2: "off,logger1=debug,logger2=info"
|
||||
//
|
||||
SPDLOG_API void load_levels(const std::string &input);
|
||||
} // namespace helpers
|
||||
} // namespace cfg
|
||||
} // namespace spdlog
|
@ -1,99 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#include "spdlog/cfg/helpers.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "spdlog/details/context.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
namespace helpers {
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// return (name,value) trimmed pair from given "name=value" string.
|
||||
// return empty string on missing parts
|
||||
// "key=val" => ("key", "val")
|
||||
// " key = val " => ("key", "val")
|
||||
// "key=" => ("key", "")
|
||||
// "val" => ("", "val")
|
||||
|
||||
inline std::pair<std::string, std::string> extract_kv_(char sep, const std::string &str) {
|
||||
auto n = str.find(sep);
|
||||
std::string k, 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 key/value pairs from sequence of "K1=V1,K2=V2,.."
|
||||
// "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...}
|
||||
inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str) {
|
||||
std::string token;
|
||||
std::istringstream token_stream(str);
|
||||
std::unordered_map<std::string, std::string> rv{};
|
||||
while (std::getline(token_stream, token, ',')) {
|
||||
if (token.empty()) {
|
||||
continue;
|
||||
}
|
||||
auto kv = extract_kv_('=', token);
|
||||
rv[kv.first] = kv.second;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
void load_levels(const std::string &input) {
|
||||
if (input.empty() || input.size() > 512) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto key_vals = extract_key_vals_(input);
|
||||
std::unordered_map<std::string, level> levels;
|
||||
level global_level = level::info;
|
||||
bool global_level_found = false;
|
||||
|
||||
for (auto &name_level : key_vals) {
|
||||
const auto &logger_name = name_level.first;
|
||||
auto level_name = to_lower_(name_level.second);
|
||||
auto level = level_from_str(level_name);
|
||||
// ignore unrecognized level names
|
||||
if (level == level::off && level_name != "off") {
|
||||
continue;
|
||||
}
|
||||
if (logger_name.empty()) // no logger name indicate global level
|
||||
{
|
||||
global_level_found = true;
|
||||
global_level = level;
|
||||
} else {
|
||||
levels[logger_name] = level;
|
||||
}
|
||||
}
|
||||
|
||||
details::context::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
|
||||
}
|
||||
|
||||
} // namespace helpers
|
||||
} // namespace cfg
|
||||
} // namespace spdlog
|
@ -1,168 +0,0 @@
|
||||
|
||||
#include "includes.h"
|
||||
#include "spdlog/cfg/argv.h"
|
||||
#include "spdlog/cfg/env.h"
|
||||
#include "test_sink.h"
|
||||
|
||||
using spdlog::cfg::load_argv_levels;
|
||||
using spdlog::cfg::load_env_levels;
|
||||
using spdlog::sinks::test_sink_st;
|
||||
|
||||
TEST_CASE("env", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
||||
#ifdef CATCH_PLATFORM_WINDOWS
|
||||
_putenv_s("SPDLOG_LEVEL", "l1=warn");
|
||||
#else
|
||||
setenv("SPDLOG_LEVEL", "l1=warn", 1);
|
||||
#endif
|
||||
load_env_levels();
|
||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
||||
spdlog::set_default_logger(spdlog::create<test_sink_st>("cfg-default"));
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv1", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn"};
|
||||
load_argv_levels(2, argv);
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv2", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn,trace"};
|
||||
load_argv_levels(2, argv);
|
||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::trace);
|
||||
}
|
||||
|
||||
TEST_CASE("argv3", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::trace);
|
||||
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=junk_name=warn"};
|
||||
load_argv_levels(2, argv);
|
||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::trace);
|
||||
}
|
||||
|
||||
TEST_CASE("argv4", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=junk"};
|
||||
load_argv_levels(2, argv);
|
||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
||||
REQUIRE(l1->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv5", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "ignore", "SPDLOG_LEVEL=l1=warn,trace"};
|
||||
load_argv_levels(3, argv);
|
||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::trace);
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv6", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::err);
|
||||
const char *argv[] = {""};
|
||||
load_argv_levels(1, argv);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::err);
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv7", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::err);
|
||||
const char *argv[] = {""};
|
||||
load_argv_levels(0, argv);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::err);
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test1", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", ""};
|
||||
load_argv_levels(2, argv);
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
l1->set_level(spdlog::level::trace);
|
||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test2", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
|
||||
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
l1->set_level(spdlog::level::warn);
|
||||
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
|
||||
l2->set_level(spdlog::level::warn);
|
||||
|
||||
load_argv_levels(2, argv);
|
||||
|
||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
||||
REQUIRE(l2->log_level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test3", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
|
||||
|
||||
load_argv_levels(2, argv);
|
||||
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
|
||||
|
||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
||||
REQUIRE(l2->log_level() == spdlog::level::info);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test4", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace,warn"};
|
||||
|
||||
load_argv_levels(2, argv);
|
||||
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
|
||||
|
||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
||||
REQUIRE(l2->log_level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::warn);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test5", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=junk,warn"};
|
||||
|
||||
load_argv_levels(2, argv);
|
||||
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
|
||||
|
||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
||||
REQUIRE(l2->log_level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::warn);
|
||||
}
|
||||
|
||||
TEST_CASE("restore-to-default", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=info"};
|
||||
load_argv_levels(2, argv);
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
Loading…
Reference in New Issue