mirror of
https://github.com/gabime/spdlog.git
synced 2025-04-28 19:43:52 +00:00
Compare commits
15 Commits
5d4e6f17ee
...
0dfb1d264e
Author | SHA1 | Date | |
---|---|---|---|
|
0dfb1d264e | ||
|
a056b9115b | ||
|
62ecc04212 | ||
|
4a0f4fc186 | ||
|
3a61dcd360 | ||
|
04d0240f8d | ||
|
13ebfc0779 | ||
|
d70d5aa9d8 | ||
|
70d3c2cd3e | ||
|
6fbe0dec2c | ||
|
9d3591dcd5 | ||
|
8992f36fbf | ||
|
3d203aa7c4 | ||
|
cd8d7e6de9 | ||
|
d52e825bbc |
@ -4,18 +4,245 @@
|
|||||||
|
|
||||||
// spdlog usage example
|
// spdlog usage example
|
||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <cstdio>
|
||||||
#include <spdlog/cfg/env.h>
|
|
||||||
|
void stdout_logger_example();
|
||||||
|
void basic_example();
|
||||||
|
void rotating_example();
|
||||||
|
void daily_example();
|
||||||
|
void async_example();
|
||||||
|
void binary_example();
|
||||||
|
void trace_example();
|
||||||
|
void multi_sink_example();
|
||||||
|
void user_defined_example();
|
||||||
|
void err_handler_example();
|
||||||
|
void syslog_example();
|
||||||
|
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
int main(int, char *[])
|
int main(int, char *[])
|
||||||
{
|
{
|
||||||
|
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
|
||||||
|
spdlog::warn("Easy padding in numbers like {:08d}", 12);
|
||||||
|
spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
|
||||||
|
spdlog::info("Support for floats {:03.2f}", 1.23456);
|
||||||
|
spdlog::info("Positional args are {1} {0}..", "too", "supported");
|
||||||
|
spdlog::info("{:>8} aligned, {:<8} aligned", "right", "left");
|
||||||
|
|
||||||
|
// Runtime log levels
|
||||||
|
spdlog::set_level(spdlog::level::info); // Set global log level to info
|
||||||
|
spdlog::debug("This message should not be displayed!");
|
||||||
|
spdlog::set_level(spdlog::level::trace); // Set specific logger's log level
|
||||||
|
spdlog::debug("This message should be displayed..");
|
||||||
|
|
||||||
|
// Customize msg format for all loggers
|
||||||
|
spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v");
|
||||||
|
spdlog::info("This an info message with custom format");
|
||||||
|
spdlog::set_pattern("%+"); // back to default format
|
||||||
|
spdlog::set_level(spdlog::level::info);
|
||||||
|
|
||||||
|
// Backtrace support
|
||||||
|
// Loggers can store in a ring buffer all messages (including debug/trace) for later inspection.
|
||||||
|
// When needed, call dump_backtrace() to see what happened:
|
||||||
|
spdlog::enable_backtrace(10); // create ring buffer with capacity of 10 messages
|
||||||
|
for (int i = 0; i < 100; i++)
|
||||||
|
{
|
||||||
|
spdlog::debug("Backtrace message {}", i); // not logged..
|
||||||
|
}
|
||||||
|
// e.g. if some error happened:
|
||||||
|
spdlog::dump_backtrace(); // log them now!
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
spdlog::env::init();
|
stdout_logger_example();
|
||||||
spdlog::info("Hello");
|
basic_example();
|
||||||
|
rotating_example();
|
||||||
|
daily_example();
|
||||||
|
async_example();
|
||||||
|
binary_example();
|
||||||
|
multi_sink_example();
|
||||||
|
user_defined_example();
|
||||||
|
err_handler_example();
|
||||||
|
trace_example();
|
||||||
|
|
||||||
|
// Flush all *registered* loggers using a worker thread every 3 seconds.
|
||||||
|
// note: registered loggers *must* be thread safe for this to work correctly!
|
||||||
|
spdlog::flush_every(std::chrono::seconds(3));
|
||||||
|
|
||||||
|
// Apply some function on all registered loggers
|
||||||
|
spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); });
|
||||||
|
|
||||||
|
// Release all spdlog resources, and drop all loggers in the registry.
|
||||||
|
// This is optional (only mandatory if using windows + async log).
|
||||||
|
spdlog::shutdown();
|
||||||
}
|
}
|
||||||
catch (spdlog::spdlog_ex &ex)
|
|
||||||
|
// Exceptions will only be thrown upon failed logger or sink construction (not during logging).
|
||||||
|
catch (const spdlog::spdlog_ex &ex)
|
||||||
{
|
{
|
||||||
spdlog::info("spdlog_ex: {}", ex.what());
|
std::printf("Log initialization failed: %s\n", ex.what());
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||||
|
// or #include "spdlog/sinks/stdout_sinks.h" if no colors needed.
|
||||||
|
void stdout_logger_example()
|
||||||
|
{
|
||||||
|
// Create color multi threaded logger.
|
||||||
|
auto console = spdlog::stdout_color_mt("console");
|
||||||
|
// or for stderr:
|
||||||
|
// auto console = spdlog::stderr_color_mt("error-logger");
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "spdlog/sinks/basic_file_sink.h"
|
||||||
|
void basic_example()
|
||||||
|
{
|
||||||
|
// Create basic file logger (not rotated).
|
||||||
|
auto my_logger = spdlog::basic_logger_mt("file_logger", "logs/basic-log.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "spdlog/sinks/rotating_file_sink.h"
|
||||||
|
void rotating_example()
|
||||||
|
{
|
||||||
|
// Create a file rotating logger with 5mb size max and 3 rotated files.
|
||||||
|
auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "spdlog/sinks/daily_file_sink.h"
|
||||||
|
void daily_example()
|
||||||
|
{
|
||||||
|
// Create a daily logger - a new file is created every day on 2:30am.
|
||||||
|
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load log levels from SPDLOG_LEVEL environment variable if exists.
|
||||||
|
//
|
||||||
|
// set global level to debug:
|
||||||
|
// export SPDLOG_LEVEL=debug
|
||||||
|
//
|
||||||
|
// turn off all logging except for logger1:
|
||||||
|
// export SPDLOG_LEVEL="off,logger1=debug"
|
||||||
|
#include "spdlog/cfg/env.h"
|
||||||
|
void env_example()
|
||||||
|
{
|
||||||
|
spdlog::env::load_levels();
|
||||||
|
spdlog::warn("env_sample. enabled with export SPDLOG_LEVEL=warn");
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "spdlog/async.h"
|
||||||
|
void async_example()
|
||||||
|
{
|
||||||
|
// Default thread pool settings can be modified *before* creating the async logger:
|
||||||
|
// spdlog::init_thread_pool(32768, 1); // queue with max 32k items 1 backing thread.
|
||||||
|
auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
|
||||||
|
// alternatively:
|
||||||
|
// auto async_file = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("async_file_logger", "logs/async_log.txt");
|
||||||
|
|
||||||
|
for (int i = 1; i < 101; ++i)
|
||||||
|
{
|
||||||
|
async_file->info("Async message #{}", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log binary data as hex.
|
||||||
|
// Many types of std::container<char> types can be used.
|
||||||
|
// Iterator ranges are supported too.
|
||||||
|
// Format flags:
|
||||||
|
// {:X} - print in uppercase.
|
||||||
|
// {:s} - don't separate each byte with space.
|
||||||
|
// {:p} - don't print the position on each line start.
|
||||||
|
// {:n} - don't split the output to lines.
|
||||||
|
|
||||||
|
#include "spdlog/fmt/bin_to_hex.h"
|
||||||
|
void binary_example()
|
||||||
|
{
|
||||||
|
std::vector<char> buf(80);
|
||||||
|
for (int i = 0; i < 80; i++)
|
||||||
|
{
|
||||||
|
buf.push_back(static_cast<char>(i & 0xff));
|
||||||
|
}
|
||||||
|
spdlog::info("Binary example: {}", spdlog::to_hex(buf));
|
||||||
|
spdlog::info("Another binary example:{:n}", spdlog::to_hex(std::begin(buf), std::begin(buf) + 10));
|
||||||
|
// more examples:
|
||||||
|
// logger->info("uppercase: {:X}", spdlog::to_hex(buf));
|
||||||
|
// logger->info("uppercase, no delimiters: {:Xs}", spdlog::to_hex(buf));
|
||||||
|
// logger->info("uppercase, no delimiters, no position info: {:Xsp}", spdlog::to_hex(buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile time log levels.
|
||||||
|
// define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE)
|
||||||
|
void trace_example()
|
||||||
|
{
|
||||||
|
// trace from default logger
|
||||||
|
SPDLOG_TRACE("Some trace message.. {} ,{}", 1, 3.23);
|
||||||
|
// debug from default logger
|
||||||
|
SPDLOG_DEBUG("Some debug message.. {} ,{}", 1, 3.23);
|
||||||
|
|
||||||
|
// trace from logger object
|
||||||
|
auto logger = spdlog::get("file_logger");
|
||||||
|
SPDLOG_LOGGER_TRACE(logger, "another trace message");
|
||||||
|
}
|
||||||
|
|
||||||
|
// A logger with multiple sinks (stdout and file) - each with a different format and log level.
|
||||||
|
void multi_sink_example()
|
||||||
|
{
|
||||||
|
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||||
|
console_sink->set_level(spdlog::level::warn);
|
||||||
|
console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v");
|
||||||
|
|
||||||
|
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", true);
|
||||||
|
file_sink->set_level(spdlog::level::trace);
|
||||||
|
|
||||||
|
spdlog::logger logger("multi_sink", {console_sink, file_sink});
|
||||||
|
logger.set_level(spdlog::level::debug);
|
||||||
|
logger.warn("this should appear in both console and file");
|
||||||
|
logger.info("this message should not appear in the console, only in the file");
|
||||||
|
}
|
||||||
|
|
||||||
|
// User defined types logging by implementing operator<<
|
||||||
|
#include "spdlog/fmt/ostr.h" // must be included
|
||||||
|
struct my_type
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
template<typename OStream>
|
||||||
|
friend OStream &operator<<(OStream &os, const my_type &c)
|
||||||
|
{
|
||||||
|
return os << "[my_type i=" << c.i << "]";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void user_defined_example()
|
||||||
|
{
|
||||||
|
spdlog::info("user defined type: {}", my_type{14});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Custom error handler. Will be triggered on log failure.
|
||||||
|
void err_handler_example()
|
||||||
|
{
|
||||||
|
// can be set globally or per logger(logger->set_error_handler(..))
|
||||||
|
spdlog::set_error_handler([](const std::string &msg) { printf("*** Custom log error handler: %s ***\n", msg.c_str()); });
|
||||||
|
}
|
||||||
|
|
||||||
|
// syslog example (linux/osx/freebsd)
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include "spdlog/sinks/syslog_sink.h"
|
||||||
|
void syslog_example()
|
||||||
|
{
|
||||||
|
std::string ident = "spdlog-example";
|
||||||
|
auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID);
|
||||||
|
syslog_logger->warn("This is warning that will end up in syslog.");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Android example.
|
||||||
|
#if defined(__ANDROID__)
|
||||||
|
#include "spdlog/sinks/android_sink.h"
|
||||||
|
void android_example()
|
||||||
|
{
|
||||||
|
std::string tag = "spdlog-android";
|
||||||
|
auto android_logger = spdlog::android_logger_mt("android", tag);
|
||||||
|
android_logger->critical("Use \"adb shell logcat\" to view this message.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
@ -4,12 +4,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/cfg/env.h"
|
#include <spdlog/cfg/env.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/spdlog.h"
|
#include <spdlog/spdlog.h>
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/details/os.h>
|
||||||
#include "spdlog/details/registry.h"
|
#include <spdlog/details/registry.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@ -37,7 +37,7 @@ inline std::string &trim_(std::string &str)
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// return (name,value) pair from given "name=value" string.
|
// return (name,value) trimmed pair from given "name=value" string.
|
||||||
// return empty string on missing parts
|
// return empty string on missing parts
|
||||||
// "key=val" => ("key", "val")
|
// "key=val" => ("key", "val")
|
||||||
// " key = val " => ("key", "val")
|
// " key = val " => ("key", "val")
|
||||||
@ -65,7 +65,7 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(con
|
|||||||
{
|
{
|
||||||
std::string token;
|
std::string token;
|
||||||
std::istringstream token_stream(str);
|
std::istringstream token_stream(str);
|
||||||
std::unordered_map<std::string, std::string> rv;
|
std::unordered_map<std::string, std::string> rv{};
|
||||||
while (std::getline(token_stream, token, ','))
|
while (std::getline(token_stream, token, ','))
|
||||||
{
|
{
|
||||||
if (token.empty())
|
if (token.empty())
|
||||||
@ -73,72 +73,43 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(con
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto kv = extract_kv_('=', token);
|
auto kv = extract_kv_('=', token);
|
||||||
|
rv[kv.first] = kv.second;
|
||||||
// '*' marks all loggers
|
|
||||||
if (kv.first.empty())
|
|
||||||
{
|
|
||||||
kv.first = "*";
|
|
||||||
}
|
|
||||||
rv.insert(std::move(kv));
|
|
||||||
}
|
}
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline details::registry::logger_cfgs from_env_()
|
inline details::registry::logger_levels extract_levels_(const std::string &input)
|
||||||
{
|
{
|
||||||
using details::os::getenv;
|
auto key_vals = extract_key_vals_(input);
|
||||||
details::registry::logger_cfgs configs;
|
details::registry::logger_levels rv;
|
||||||
|
|
||||||
auto levels = extract_key_vals_(getenv("SPDLOG_LEVEL"));
|
for (auto &name_level : key_vals)
|
||||||
auto patterns = extract_key_vals_(getenv("SPDLOG_PATTERN"));
|
|
||||||
|
|
||||||
// merge to single dict. and take into account "*"
|
|
||||||
for (auto &name_level : levels)
|
|
||||||
{
|
{
|
||||||
auto &logger_name = name_level.first;
|
auto &logger_name = name_level.first;
|
||||||
auto level_name = to_lower_(name_level.second);
|
auto level_name = to_lower_(name_level.second);
|
||||||
details::registry::logger_cfg cfg;
|
auto level = level::from_str(level_name);
|
||||||
cfg.level_name = level_name;
|
// fallback to "info" if unrecognized level name
|
||||||
if (logger_name == "*")
|
if (level == level::off && level_name != "off")
|
||||||
{
|
{
|
||||||
configs.default_cfg.level_name = cfg.level_name;
|
level = level::info;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (logger_name.empty() || logger_name == "*")
|
||||||
|
{
|
||||||
|
rv.default_level = level;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
configs.loggers.emplace(logger_name, cfg);
|
rv.levels[logger_name] = level;
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &name_pattern : patterns)
|
|
||||||
{
|
|
||||||
auto &logger_name = name_pattern.first;
|
|
||||||
auto &pattern = name_pattern.second;
|
|
||||||
auto it = configs.loggers.find(logger_name);
|
|
||||||
|
|
||||||
if (it != configs.loggers.end())
|
|
||||||
{
|
|
||||||
it->second.pattern = pattern;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (logger_name == "*")
|
|
||||||
{
|
|
||||||
configs.default_cfg.pattern = pattern;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
details::registry::logger_cfg cfg;
|
|
||||||
cfg.pattern = pattern;
|
|
||||||
configs.loggers.emplace(logger_name, cfg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return rv;
|
||||||
return configs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void init()
|
SPDLOG_INLINE void load_levels()
|
||||||
{
|
{
|
||||||
spdlog::details::registry::instance().set_configs(from_env_());
|
auto levels = extract_levels_(details::os::getenv("SPDLOG_LEVEL"));
|
||||||
|
spdlog::details::registry::instance().set_levels(levels);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace env
|
} // namespace env
|
||||||
|
@ -8,8 +8,9 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
//
|
//
|
||||||
// Init levels and patterns from env variables SPDLOG_LEVEL and SPDLOG_PATTERN.
|
// Init levels and patterns from env variables SPDLOG_LEVEL
|
||||||
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
|
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
|
||||||
|
// Note - fallback to "info" level on unrecognized levels
|
||||||
//
|
//
|
||||||
// Examples:
|
// Examples:
|
||||||
//
|
//
|
||||||
@ -21,20 +22,11 @@
|
|||||||
//
|
//
|
||||||
// turn off all logging except for logger1 and logger2:
|
// turn off all logging except for logger1 and logger2:
|
||||||
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
|
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
|
||||||
//
|
|
||||||
// set global pattern:
|
|
||||||
// export SPDLOG_PATTERN="[%x] [%l] [%n] %v"
|
|
||||||
//
|
|
||||||
// set pattern for logger1:
|
|
||||||
// export SPDLOG_PATTERN="logger1=%v,*=[%x] [%l] [%n] %v"
|
|
||||||
//
|
|
||||||
// set global pattern and different pattern for logger1:
|
|
||||||
// export SPDLOG_PATTERN="[%x] [%l] [%n] %v,logger1=[%u] %v"
|
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace env {
|
namespace env {
|
||||||
void init();
|
void load_levels();
|
||||||
}
|
} // namespace env
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
|
@ -34,7 +34,8 @@ SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG
|
|||||||
}
|
}
|
||||||
level++;
|
level++;
|
||||||
}
|
}
|
||||||
return level::off;
|
// allow warn = warning before giving up
|
||||||
|
return name == "warn" ? level::warn : level::off;
|
||||||
}
|
}
|
||||||
} // namespace level
|
} // namespace level
|
||||||
|
|
||||||
|
@ -538,11 +538,16 @@ SPDLOG_INLINE filename_t dir_name(filename_t path)
|
|||||||
|
|
||||||
std::string SPDLOG_INLINE getenv(const char *field)
|
std::string SPDLOG_INLINE getenv(const char *field)
|
||||||
{
|
{
|
||||||
#if defined(_MSC_VER) && !defined(__cplusplus_winrt)
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#if defined(__cplusplus_winrt)
|
||||||
|
return std::string{}; // not supported under uwp
|
||||||
|
#else
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
char buf[128];
|
char buf[128];
|
||||||
bool ok = ::getenv_s(&len, buf, sizeof(buf), field) == 0;
|
bool ok = ::getenv_s(&len, buf, sizeof(buf), field) == 0;
|
||||||
return ok ? buf : std::string{};
|
return ok ? buf : std::string{};
|
||||||
|
#endif
|
||||||
#else // revert to getenv
|
#else // revert to getenv
|
||||||
char *buf = ::getenv(field);
|
char *buf = ::getenv(field);
|
||||||
return buf ? buf : std::string{};
|
return buf ? buf : std::string{};
|
||||||
|
@ -30,6 +30,12 @@
|
|||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
|
SPDLOG_INLINE level::level_enum registry::logger_levels::get_or_default(const std::string &name)
|
||||||
|
{
|
||||||
|
auto it = levels.find(name);
|
||||||
|
return it != levels.end() ? it->second : default_level;
|
||||||
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE registry::registry()
|
SPDLOG_INLINE registry::registry()
|
||||||
: formatter_(new pattern_formatter())
|
: formatter_(new pattern_formatter())
|
||||||
{
|
{
|
||||||
@ -64,7 +70,7 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logge
|
|||||||
new_logger->set_error_handler(err_handler_);
|
new_logger->set_error_handler(err_handler_);
|
||||||
}
|
}
|
||||||
|
|
||||||
new_logger->set_level(level_);
|
new_logger->set_level(levels_.get_or_default(new_logger->name()));
|
||||||
new_logger->flush_on(flush_level_);
|
new_logger->flush_on(flush_level_);
|
||||||
|
|
||||||
if (backtrace_n_messages_ > 0)
|
if (backtrace_n_messages_ > 0)
|
||||||
@ -168,7 +174,7 @@ SPDLOG_INLINE void registry::set_level(level::level_enum log_level)
|
|||||||
{
|
{
|
||||||
l.second->set_level(log_level);
|
l.second->set_level(log_level);
|
||||||
}
|
}
|
||||||
level_ = log_level;
|
levels_.default_level = log_level;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
||||||
@ -260,22 +266,15 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat
|
|||||||
automatic_registration_ = automatic_registration;
|
automatic_registration_ = automatic_registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void registry::set_configs(logger_cfgs configs)
|
SPDLOG_INLINE void registry::set_levels(logger_levels levels)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
|
levels_ = std::move(levels);
|
||||||
for (auto &l : loggers_)
|
for (auto &l : loggers_)
|
||||||
{
|
{
|
||||||
auto &logger = l.second;
|
auto &logger = l.second;
|
||||||
auto cfg_it = configs.loggers.find(logger->name());
|
logger->set_level(levels_.get_or_default(logger->name()));
|
||||||
|
|
||||||
// use default config if not found for this logger name
|
|
||||||
logger_cfg *cfg = cfg_it != configs.loggers.end() ? &cfg_it->second : &configs.default_cfg;
|
|
||||||
auto &level_name = cfg->level_name.empty() ? cfg->level_name: configs.default_cfg.level_name;
|
|
||||||
auto &pattern = cfg->pattern.empty() ? cfg->pattern: configs.default_cfg.pattern;
|
|
||||||
logger->set_level(level::from_str(level_name));
|
|
||||||
logger->set_formatter(details::make_unique<pattern_formatter>(pattern));
|
|
||||||
}
|
}
|
||||||
logger_cfgs_ = std::move(configs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE registry ®istry::instance()
|
SPDLOG_INLINE registry ®istry::instance()
|
||||||
@ -298,5 +297,6 @@ SPDLOG_INLINE void registry::register_logger_(std::shared_ptr<logger> new_logger
|
|||||||
throw_if_exists_(logger_name);
|
throw_if_exists_(logger_name);
|
||||||
loggers_[logger_name] = std::move(new_logger);
|
loggers_[logger_name] = std::move(new_logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
@ -27,16 +27,11 @@ class periodic_worker;
|
|||||||
class registry
|
class registry
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
struct logger_cfg
|
struct logger_levels
|
||||||
{
|
{
|
||||||
std::string level_name;
|
std::unordered_map<std::string, spdlog::level::level_enum> levels;
|
||||||
std::string pattern;
|
spdlog::level::level_enum default_level = level::info;
|
||||||
};
|
spdlog::level::level_enum get_or_default(const std::string &name);
|
||||||
|
|
||||||
struct logger_cfgs
|
|
||||||
{
|
|
||||||
std::unordered_map<std::string, logger_cfg> loggers;
|
|
||||||
logger_cfg default_cfg = {"info", "%+"};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
registry(const registry &) = delete;
|
registry(const registry &) = delete;
|
||||||
@ -91,7 +86,7 @@ public:
|
|||||||
|
|
||||||
void set_automatic_registration(bool automatic_registration);
|
void set_automatic_registration(bool automatic_registration);
|
||||||
|
|
||||||
void set_configs(logger_cfgs configs);
|
void set_levels(logger_levels levels);
|
||||||
|
|
||||||
static registry &instance();
|
static registry &instance();
|
||||||
|
|
||||||
@ -104,15 +99,14 @@ private:
|
|||||||
std::mutex logger_map_mutex_, flusher_mutex_;
|
std::mutex logger_map_mutex_, flusher_mutex_;
|
||||||
std::recursive_mutex tp_mutex_;
|
std::recursive_mutex tp_mutex_;
|
||||||
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
||||||
|
logger_levels levels_;
|
||||||
std::unique_ptr<formatter> formatter_;
|
std::unique_ptr<formatter> formatter_;
|
||||||
level::level_enum level_ = level::info;
|
|
||||||
level::level_enum flush_level_ = level::off;
|
level::level_enum flush_level_ = level::off;
|
||||||
void (*err_handler_)(const std::string &msg);
|
void (*err_handler_)(const std::string &msg);
|
||||||
std::shared_ptr<thread_pool> tp_;
|
std::shared_ptr<thread_pool> tp_;
|
||||||
std::unique_ptr<periodic_worker> periodic_flusher_;
|
std::unique_ptr<periodic_worker> periodic_flusher_;
|
||||||
std::shared_ptr<logger> default_logger_;
|
std::shared_ptr<logger> default_logger_;
|
||||||
bool automatic_registration_ = true;
|
bool automatic_registration_ = true;
|
||||||
logger_cfgs logger_cfgs_;
|
|
||||||
size_t backtrace_n_messages_ = 0;
|
size_t backtrace_n_messages_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -72,6 +72,7 @@ TEST_CASE("to_level_enum", "[convert_to_level_enum]")
|
|||||||
REQUIRE(spdlog::level::from_str("debug") == spdlog::level::debug);
|
REQUIRE(spdlog::level::from_str("debug") == spdlog::level::debug);
|
||||||
REQUIRE(spdlog::level::from_str("info") == spdlog::level::info);
|
REQUIRE(spdlog::level::from_str("info") == spdlog::level::info);
|
||||||
REQUIRE(spdlog::level::from_str("warning") == spdlog::level::warn);
|
REQUIRE(spdlog::level::from_str("warning") == spdlog::level::warn);
|
||||||
|
REQUIRE(spdlog::level::from_str("warn") == spdlog::level::warn);
|
||||||
REQUIRE(spdlog::level::from_str("error") == spdlog::level::err);
|
REQUIRE(spdlog::level::from_str("error") == spdlog::level::err);
|
||||||
REQUIRE(spdlog::level::from_str("critical") == spdlog::level::critical);
|
REQUIRE(spdlog::level::from_str("critical") == spdlog::level::critical);
|
||||||
REQUIRE(spdlog::level::from_str("off") == spdlog::level::off);
|
REQUIRE(spdlog::level::from_str("off") == spdlog::level::off);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user