mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-15 17:27:57 +00:00
Compare commits
10 Commits
cd5ddca00d
...
0b55e2c332
Author | SHA1 | Date | |
---|---|---|---|
|
0b55e2c332 | ||
|
b105046202 | ||
|
de20255c71 | ||
|
1a1c37db7c | ||
|
a87700a28c | ||
|
1f8e9ad0fc | ||
|
e13e978af4 | ||
|
28e334c728 | ||
|
15a9427112 | ||
|
010b0e1d75 |
@ -103,7 +103,7 @@ set(SPDLOG_SRCS
|
||||
src/color_sinks.cpp
|
||||
src/file_sinks.cpp
|
||||
src/async.cpp
|
||||
src/cfg.cpp)
|
||||
src/loaders.cpp)
|
||||
|
||||
|
||||
if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)
|
||||
|
@ -20,15 +20,13 @@ void syslog_example();
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/cfg/env.h>
|
||||
#include <spdlog/cfg/argv.h>
|
||||
#include <spdlog/loaders/env.h>
|
||||
#include <spdlog/loaders/argv.h>
|
||||
|
||||
int main(int args, char *argv[])
|
||||
{
|
||||
|
||||
|
||||
//spdlog::cfg::env::load_levels();
|
||||
spdlog::cfg::argv::load_levels(args, argv);
|
||||
spdlog::loaders::load_env();
|
||||
spdlog::loaders::load_argv(args, argv);
|
||||
spdlog::info("HELLO INFO");
|
||||
|
||||
auto l1 = spdlog::stderr_color_st("l1");
|
||||
|
@ -168,7 +168,7 @@ SPDLOG_INLINE void registry::set_level(level::level_enum log_level)
|
||||
{
|
||||
l.second->set_level(log_level);
|
||||
}
|
||||
levels_.set("*", log_level);
|
||||
levels_.set_default(log_level);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
||||
@ -260,7 +260,7 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat
|
||||
automatic_registration_ = automatic_registration;
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void registry::update_levels(cfg::log_levels levels)
|
||||
SPDLOG_INLINE void registry::update_levels(loaders::log_levels levels)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||
levels_ = std::move(levels);
|
||||
|
@ -9,7 +9,7 @@
|
||||
// This class is thread safe
|
||||
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/cfg/log_levels.h>
|
||||
#include <spdlog/loaders/log_levels.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
@ -80,7 +80,7 @@ public:
|
||||
|
||||
void set_automatic_registration(bool automatic_registration);
|
||||
|
||||
void update_levels(cfg::log_levels levels);
|
||||
void update_levels(loaders::log_levels levels);
|
||||
|
||||
static registry &instance();
|
||||
|
||||
@ -93,7 +93,7 @@ private:
|
||||
std::mutex logger_map_mutex_, flusher_mutex_;
|
||||
std::recursive_mutex tp_mutex_;
|
||||
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
||||
cfg::log_levels levels_;
|
||||
loaders::log_levels levels_;
|
||||
std::unique_ptr<formatter> formatter_;
|
||||
level::level_enum flush_level_ = level::off;
|
||||
void (*err_handler_)(const std::string &msg);
|
||||
|
@ -2,18 +2,26 @@
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
#include <spdlog/cfg/helpers.h>
|
||||
#include <spdlog/loaders/helpers.h>
|
||||
#include <spdlog/details/os.h>
|
||||
|
||||
//
|
||||
// Init levels from argv SPDLOG_LEVEL
|
||||
// Example: my_program.exe "SPDLOG_LEVEL=trace"
|
||||
// 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 {
|
||||
namespace argv {
|
||||
namespace loaders {
|
||||
|
||||
// search for SPDLOG_LEVEL= in the args and use it to init the levels
|
||||
void load_levels(int args, char *argv[])
|
||||
void load_argv(int args, char **argv)
|
||||
{
|
||||
const std::string spdlog_level_prefix = "SPDLOG_LEVEL=";
|
||||
for (int i = 1; i < args; i++)
|
||||
@ -27,6 +35,6 @@ void load_levels(int args, char *argv[])
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace argv
|
||||
} // namespace cfg
|
||||
|
||||
} // namespace loaders
|
||||
} // namespace spdlog
|
@ -2,7 +2,7 @@
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
#include <spdlog/cfg/helpers.h>
|
||||
#include <spdlog/loaders/helpers.h>
|
||||
#include <spdlog/details/registry.h>
|
||||
#include <spdlog/details/os.h>
|
||||
|
||||
@ -19,19 +19,18 @@
|
||||
// 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 {
|
||||
namespace env {
|
||||
void load_levels()
|
||||
namespace loaders {
|
||||
void load_env()
|
||||
{
|
||||
auto env_val = details::os::getenv("SPDLOG_LEVEL");
|
||||
auto levels = helpers::extract_levels(env_val);
|
||||
details::registry::instance().update_levels(std::move(levels));
|
||||
}
|
||||
} // namespace env
|
||||
|
||||
} // namespace cfg
|
||||
} // namespace loaders
|
||||
} // namespace spdlog
|
@ -4,7 +4,7 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef SPDLOG_HEADER_ONLY
|
||||
#include <spdlog/cfg/helpers.h>
|
||||
#include <spdlog/loaders/helpers.h>
|
||||
#endif
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
@ -16,10 +16,9 @@
|
||||
#include <sstream>
|
||||
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
namespace loaders {
|
||||
namespace helpers {
|
||||
|
||||
|
||||
// inplace convert to lowercase
|
||||
inline std::string &to_lower_(std::string &str)
|
||||
{
|
||||
@ -99,6 +98,6 @@ SPDLOG_INLINE log_levels extract_levels(const std::string &input)
|
||||
return rv;
|
||||
}
|
||||
|
||||
} // namespace text_loader
|
||||
} // namespace cfg
|
||||
} // namespace helpers
|
||||
} // namespace loaders
|
||||
} // namespace spdlog
|
@ -3,20 +3,24 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/cfg/log_levels.h>
|
||||
#include <string>
|
||||
#include <spdlog/loaders/log_levels.h>
|
||||
|
||||
namespace spdlog {
|
||||
namespace loaders {
|
||||
namespace helpers {
|
||||
//
|
||||
// Init levels from given string
|
||||
//
|
||||
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
namespace helpers {
|
||||
// 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"
|
||||
//
|
||||
log_levels extract_levels(const std::string &txt);
|
||||
}
|
||||
} // namespace helpers
|
||||
|
||||
} // namespace cfg
|
||||
} // namespace loaders
|
||||
} // namespace spdlog
|
||||
|
||||
#ifdef SPDLOG_HEADER_ONLY
|
@ -8,11 +8,10 @@
|
||||
#include <unordered_map>
|
||||
|
||||
namespace spdlog {
|
||||
namespace cfg {
|
||||
namespace loaders {
|
||||
class log_levels
|
||||
{
|
||||
using levels_map = std::unordered_map<std::string, spdlog::level::level_enum>;
|
||||
levels_map levels_;
|
||||
std::unordered_map<std::string, spdlog::level::level_enum> levels_;
|
||||
spdlog::level::level_enum default_level_ = level::info;
|
||||
|
||||
public:
|
||||
@ -28,18 +27,21 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void set_default(level::level_enum lvl)
|
||||
{
|
||||
default_level_ = lvl;
|
||||
}
|
||||
|
||||
level::level_enum get(const std::string &logger_name)
|
||||
{
|
||||
auto it = levels_.find(logger_name);
|
||||
return it != levels_.end() ? it->second : default_level_;
|
||||
}
|
||||
|
||||
level::level_enum get()
|
||||
level::level_enum get_default()
|
||||
{
|
||||
return default_level_;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
} // namespace cfg
|
||||
} // namespace loaders
|
||||
} // namespace spdlog
|
@ -83,7 +83,7 @@ spdlog_srcs = files([
|
||||
'src/file_sinks.cpp',
|
||||
'src/spdlog.cpp',
|
||||
'src/stdout_sinks.cpp',
|
||||
'src/cfg.cpp'
|
||||
'src/loaders.cpp'
|
||||
])
|
||||
|
||||
if not get_option('external_fmt')
|
||||
|
@ -5,4 +5,4 @@
|
||||
#error Please define SPDLOG_COMPILED_LIB to compile this file.
|
||||
#endif
|
||||
|
||||
#include "spdlog/cfg/helpers-inl.h"
|
||||
#include "spdlog/loaders/helpers-inl.h"
|
@ -23,7 +23,8 @@ set(SPDLOG_UTESTS_SOURCES
|
||||
test_fmt_helper.cpp
|
||||
test_stdout_api.cpp
|
||||
test_backtrace.cpp
|
||||
test_create_dir.cpp)
|
||||
test_create_dir.cpp
|
||||
test_loaders.cpp)
|
||||
|
||||
if(NOT SPDLOG_NO_EXCEPTIONS)
|
||||
list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp)
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
|
||||
|
||||
|
@ -14,7 +14,8 @@ test_sources = files([
|
||||
'test_fmt_helper.cpp',
|
||||
'test_stdout_api.cpp',
|
||||
'test_backtrace.cpp',
|
||||
'test_create_dir.cpp'
|
||||
'test_create_dir.cpp',
|
||||
'test_loaders.cpp',
|
||||
])
|
||||
|
||||
if not get_option('no_exceptions')
|
||||
|
88
tests/test_loaders.cpp
Normal file
88
tests/test_loaders.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
#include "includes.h"
|
||||
#include "test_sink.h"
|
||||
|
||||
#include <spdlog/loaders/env.h>
|
||||
#include <spdlog/loaders/argv.h>
|
||||
|
||||
using spdlog::loaders::load_argv;
|
||||
using spdlog::loaders::load_env;
|
||||
|
||||
TEST_CASE("env", "[loaders]")
|
||||
{
|
||||
spdlog::drop("l1");
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
#ifdef _MSVC
|
||||
_putenv_s("SPDLOG_LEVEL", "l1=warn");
|
||||
#else
|
||||
setenv("SPDLOG_LEVEL", "l1=warn", 1);
|
||||
#endif
|
||||
load_env();
|
||||
REQUIRE(l1->level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv1", "[loaders]")
|
||||
{
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn"};
|
||||
load_argv(2, const_cast<char**>(argv));
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
REQUIRE(l1->level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv2", "[loaders]")
|
||||
{
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn,trace"};
|
||||
load_argv(2, const_cast<char**>(argv));
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
REQUIRE(l1->level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::trace);
|
||||
}
|
||||
|
||||
TEST_CASE("argv3", "[loaders]")
|
||||
{
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL="};
|
||||
load_argv(2, const_cast<char**>(argv));
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
REQUIRE(l1->level() == spdlog::level::info);
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv4", "[loaders]")
|
||||
{
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=junk"};
|
||||
load_argv(2, const_cast<char**>(argv));
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
REQUIRE(l1->level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv5", "[loaders]")
|
||||
{
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "ignore", "SPDLOG_LEVEL=l1=warn,trace"};
|
||||
load_argv(3, const_cast<char**>(argv));
|
||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
||||
REQUIRE(l1->level() == spdlog::level::warn);
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::trace);
|
||||
}
|
||||
|
||||
TEST_CASE("argv6", "[loaders]")
|
||||
{
|
||||
spdlog::set_level(spdlog::level::err);
|
||||
const char *argv[] = {""};
|
||||
load_argv(1, const_cast<char**>(argv));
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::err);
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("argv7", "[loaders]")
|
||||
{
|
||||
spdlog::set_level(spdlog::level::err);
|
||||
const char *argv[] = {""};
|
||||
load_argv(0, const_cast<char**>(argv));
|
||||
REQUIRE(spdlog::default_logger()->level() == spdlog::level::err);
|
||||
}
|
Loading…
Reference in New Issue
Block a user