clang-format

This commit is contained in:
gabime 2024-05-03 18:30:46 +03:00
parent 15829cfb84
commit 06be4409e6
14 changed files with 273 additions and 290 deletions

View File

@ -17,24 +17,24 @@
void bench_c_string(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
const char *msg =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
"lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, "
"eu consequat sem "
"libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec "
"fringilla dui sed "
"augue pretium, nec scelerisque est maximus. Nullam convallis, sem nec blandit maximus, "
"nisi turpis ornare "
"nisl, sit amet volutpat neque massa eu odio. Maecenas malesuada quam ex, posuere congue "
"nibh turpis duis.";
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
"lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, "
"eu consequat sem "
"libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec "
"fringilla dui sed "
"augue pretium, nec scelerisque est maximus. Nullam convallis, sem nec blandit maximus, "
"nisi turpis ornare "
"nisl, sit amet volutpat neque massa eu odio. Maecenas malesuada quam ex, posuere congue "
"nibh turpis duis.";
for (auto _: state) {
for (auto _ : state) {
logger->info(msg);
}
}
void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
int i = 0;
for (auto _: state) {
for (auto _ : state) {
logger->info("Hello logger: msg number {}...............", ++i);
}
}
@ -42,7 +42,7 @@ void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logge
void bench_global_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
spdlog::set_default_logger(std::move(logger));
int i = 0;
for (auto _: state) {
for (auto _ : state) {
spdlog::info("Hello logger: msg number {}...............", ++i);
}
}
@ -51,7 +51,7 @@ void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logge
int i = 0;
benchmark::DoNotOptimize(i); // prevent unused warnings
benchmark::DoNotOptimize(logger); // prevent unused warnings
for (auto _: state) {
for (auto _ : state) {
SPDLOG_LOGGER_DEBUG(logger, "Hello logger: msg number {}...............", i++);
}
}
@ -61,7 +61,7 @@ void bench_disabled_macro_global_logger(benchmark::State &state, std::shared_ptr
int i = 0;
benchmark::DoNotOptimize(i); // prevent unused warnings
benchmark::DoNotOptimize(logger); // prevent unused warnings
for (auto _: state) {
for (auto _ : state) {
SPDLOG_DEBUG("Hello logger: msg number {}...............", i++);
}
}
@ -80,7 +80,6 @@ void bench_dev_null() {
#endif // __linux__
// test spdlog::get() performance
// for this test we create multiple null loggers and then call spdlog::get() on one of them multiple times
// create multiple null loggers and return name of the one to test
@ -97,8 +96,8 @@ static std::string prepare_null_loggers() {
// benchmark spdlog::get() with const char*
void bench_get_logger_const_char(benchmark::State &state) {
std::string str_name = prepare_null_loggers();
const char* name = str_name.c_str();
for (auto _: state) {
const char *name = str_name.c_str();
for (auto _ : state) {
auto rv = spdlog::get(name);
if (rv == nullptr) {
state.SkipWithError("get() returned nullptr");
@ -110,7 +109,7 @@ void bench_get_logger_const_char(benchmark::State &state) {
void bench_get_logger_sv(benchmark::State &state) {
auto str_name = prepare_null_loggers();
auto sv_name = std::string_view{str_name};
for (auto _: state) {
for (auto _ : state) {
auto rv = spdlog::get(sv_name);
if (rv == nullptr) {
state.SkipWithError("get() returned nullptr");
@ -121,7 +120,7 @@ void bench_get_logger_sv(benchmark::State &state) {
// benchmark spdlog::get() with std::string
void bench_get_logger_string(benchmark::State &state) {
auto str_name = prepare_null_loggers();
for (auto _: state) {
for (auto _ : state) {
auto rv = spdlog::get(str_name);
if (rv == nullptr) {
state.SkipWithError("get() returned nullptr");
@ -201,7 +200,6 @@ int main(int argc, char *argv[]) {
spdlog::async_overflow_policy::overrun_oldest);
benchmark::RegisterBenchmark("async_logger", bench_logger, async_logger)->Threads(n_threads)->UseRealTime();
benchmark::RegisterBenchmark("spdlog::get(const char* name)", bench_get_logger_const_char);
benchmark::RegisterBenchmark("spdlog::get(std::string_view name)", bench_get_logger_sv);
benchmark::RegisterBenchmark("spdlog::get(const std::string &name)", bench_get_logger_string);

View File

@ -353,8 +353,7 @@ void replace_default_logger_example() {
}
#include "spdlog/mdc.h"
void mdc_example()
{
void mdc_example() {
spdlog::mdc::put("key1", "value1");
spdlog::mdc::put("key2", "value2");
// use the %& formatter flag to print all MDC values

View File

@ -80,8 +80,7 @@ inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<v
}
inline void init_thread_pool(size_t q_size, size_t thread_count) {
init_thread_pool(
q_size, thread_count, [] {}, [] {});
init_thread_pool(q_size, thread_count, [] {}, [] {});
}
// get the global thread pool.

View File

@ -69,7 +69,10 @@ public:
periodic_flusher_ = std::make_unique<periodic_worker>(clbk, interval);
}
std::unique_ptr<periodic_worker> &get_flusher() { std::lock_guard<std::mutex> lock(flusher_mutex_); return periodic_flusher_; }
std::unique_ptr<periodic_worker> &get_flusher() {
std::lock_guard<std::mutex> lock(flusher_mutex_);
return periodic_flusher_;
}
void set_error_handler(err_handler handler);

View File

@ -1,36 +1,34 @@
#pragma once
#include <spdlog/common.h>
#include <map>
#include <string>
#include <spdlog/common.h>
namespace spdlog {
class SPDLOG_API mdc {
public:
using mdc_map_t = std::map<std::string, std::string>;
class SPDLOG_API mdc {
public:
using mdc_map_t = std::map<std::string, std::string>;
static void put(const std::string &key, const std::string &value) {
get_context()[key] = value;
static void put(const std::string &key, const std::string &value) { get_context()[key] = value; }
static std::string get(const std::string &key) {
auto &context = get_context();
auto it = context.find(key);
if (it != context.end()) {
return it->second;
}
return "";
}
static std::string get(const std::string &key) {
auto &context = get_context();
auto it = context.find(key);
if (it != context.end()) {
return it->second;
}
return "";
}
static void remove(const std::string &key) { get_context().erase(key); }
static void remove(const std::string &key) { get_context().erase(key); }
static void clear() { get_context().clear(); }
static void clear() { get_context().clear(); }
static mdc_map_t &get_context() {
static thread_local mdc_map_t context;
return context;
}
};
static mdc_map_t &get_context() {
static thread_local mdc_map_t context;
return context;
}
};
} // namespace spdlog

View File

@ -27,7 +27,7 @@ public:
protected:
void sink_it_(const details::log_msg &msg) override { callback_(msg); }
void flush_() override{};
void flush_() override {};
private:
custom_log_callback callback_;

View File

@ -170,8 +170,7 @@ protected:
color_range_start, // color range start
color_range_end}; // color range end
QMetaObject::invokeMethod(
qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection);
QMetaObject::invokeMethod(qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection);
}
void flush_() override {}

View File

@ -37,7 +37,9 @@ public:
: start_tp_{clock::now()} {}
[[nodiscard]]
std::chrono::duration<double> elapsed() const { return std::chrono::duration<double>(clock::now() - start_tp_); }
std::chrono::duration<double> elapsed() const {
return std::chrono::duration<double>(clock::now() - start_tp_);
}
[[nodiscard]]
std::chrono::milliseconds elapsed_ms() const {

View File

@ -8,14 +8,14 @@
#include "spdlog/pattern_formatter.h"
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
// support for the default stdout color logger
#ifdef _WIN32
#include "spdlog/sinks/wincolor_sink.h"
#else
// support for the default stdout color logger
#ifdef _WIN32
#include "spdlog/sinks/wincolor_sink.h"
#else
#include "spdlog/sinks/ansicolor_sink.h"
#include "spdlog/sinks/ansicolor_sink.h"
#endif
#endif
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
#include <memory>
@ -25,245 +25,242 @@
static constexpr size_t small_map_threshold = 10;
namespace spdlog {
namespace details {
namespace details {
registry::registry()
: formatter_(new pattern_formatter()) {
registry::registry()
: formatter_(new pattern_formatter()) {
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
// create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows).
#ifdef _WIN32
auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>();
#else
auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();
#endif
const char *default_logger_name = "";
default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink));
loggers_[default_logger_name] = default_logger_;
// create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows).
#ifdef _WIN32
auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>();
#else
auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();
#endif
const char *default_logger_name = "";
default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink));
loggers_[default_logger_name] = default_logger_;
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
}
}
registry::~registry() = default;
registry::~registry() = default;
void registry::register_logger(std::shared_ptr<logger> new_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
register_logger_(std::move(new_logger));
}
void registry::register_logger(std::shared_ptr<logger> new_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
register_logger_(std::move(new_logger));
}
void registry::initialize_logger(std::shared_ptr<logger> new_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
new_logger->set_formatter(formatter_->clone());
void registry::initialize_logger(std::shared_ptr<logger> new_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
new_logger->set_formatter(formatter_->clone());
if (err_handler_) {
new_logger->set_error_handler(err_handler_);
}
if (err_handler_) {
new_logger->set_error_handler(err_handler_);
}
// set new level according to previously configured level or default level
auto it = log_levels_.find(new_logger->name());
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
new_logger->set_level(new_level);
// set new level according to previously configured level or default level
auto it = log_levels_.find(new_logger->name());
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
new_logger->set_level(new_level);
new_logger->flush_on(flush_level_);
new_logger->flush_on(flush_level_);
if (automatic_registration_) {
register_logger_(std::move(new_logger));
if (automatic_registration_) {
register_logger_(std::move(new_logger));
}
}
// if the map is small do a sequential search, otherwise use the standard find()
std::shared_ptr<logger> registry::get(const std::string &logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
if (loggers_.size() <= small_map_threshold) {
for (const auto &[key, val] : loggers_) {
if (logger_name == key) {
return val;
}
}
return nullptr;
} else {
auto found = loggers_.find(logger_name);
return found == loggers_.end() ? nullptr : found->second;
}
}
// if the map is small do a sequential search, otherwise use the standard find()
std::shared_ptr<logger> registry::get(const std::string &logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
if (loggers_.size() <= small_map_threshold) {
for (const auto &[key, val]: loggers_) {
if (logger_name == key) {
return val;
}
}
return nullptr;
}
else {
auto found = loggers_.find(logger_name);
return found == loggers_.end() ? nullptr : found->second;
// if the map is small do a sequential search and avoid creating string for find(logger_name)
// otherwise use the standard find()
std::shared_ptr<logger> registry::get(std::string_view logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
if (loggers_.size() <= small_map_threshold) {
for (const auto &[key, val] : loggers_) {
if (logger_name == key) {
return val;
}
}
return nullptr;
}
// otherwise use the normal map lookup
else {
auto found = loggers_.find(std::string(logger_name));
return found == loggers_.end() ? nullptr : found->second;
}
}
// if the map is small do a sequential search and avoid creating string for find(logger_name)
// otherwise use the standard find()
std::shared_ptr<logger> registry::get(std::string_view logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
if (loggers_.size() <= small_map_threshold) {
for (const auto &[key, val]: loggers_) {
if (logger_name == key) {
return val;
}
}
return nullptr;
}
// otherwise use the normal map lookup
else {
auto found = loggers_.find(std::string(logger_name));
return found == loggers_.end() ? nullptr : found->second;
}
}
std::shared_ptr<logger> registry::get(const char *logger_name) { return get(std::string_view(logger_name)); }
std::shared_ptr<logger> registry::get(const char *logger_name) {
return get(std::string_view(logger_name));
}
std::shared_ptr<logger> registry::default_logger() {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
return default_logger_;
}
std::shared_ptr<logger> registry::default_logger() {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
return default_logger_;
}
// Return raw ptr to the default logger.
// To be used directly by the spdlog default api (e.g. spdlog::info)
// This make the default API faster, but cannot be used concurrently with set_default_logger().
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
logger *registry::get_default_raw() { return default_logger_.get(); }
logger *registry::get_default_raw() { return default_logger_.get(); }
// set default logger.
// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
void registry::set_default_logger(std::shared_ptr<logger> new_default_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
if (new_default_logger != nullptr) {
loggers_[new_default_logger->name()] = new_default_logger;
}
default_logger_ = std::move(new_default_logger);
}
void registry::set_default_logger(std::shared_ptr<logger> new_default_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
if (new_default_logger != nullptr) {
loggers_[new_default_logger->name()] = new_default_logger;
}
default_logger_ = std::move(new_default_logger);
}
void registry::set_tp(std::shared_ptr<thread_pool> tp) {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
tp_ = std::move(tp);
}
void registry::set_tp(std::shared_ptr<thread_pool> tp) {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
tp_ = std::move(tp);
}
std::shared_ptr<thread_pool> registry::get_tp() {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
return tp_;
}
std::shared_ptr<thread_pool> registry::get_tp() {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
return tp_;
}
// Set global formatter. Each sink in each logger will get a clone of this object
void registry::set_formatter(std::unique_ptr<formatter> formatter) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
formatter_ = std::move(formatter);
for (auto &l: loggers_) {
l.second->set_formatter(formatter_->clone());
}
}
void registry::set_formatter(std::unique_ptr<formatter> formatter) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
formatter_ = std::move(formatter);
for (auto &l : loggers_) {
l.second->set_formatter(formatter_->clone());
}
}
void registry::set_level(level level) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l: loggers_) {
l.second->set_level(level);
}
global_log_level_ = level;
}
void registry::set_level(level level) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) {
l.second->set_level(level);
}
global_log_level_ = level;
}
void registry::flush_on(level level) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l: loggers_) {
l.second->flush_on(level);
}
flush_level_ = level;
}
void registry::flush_on(level level) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) {
l.second->flush_on(level);
}
flush_level_ = level;
}
void registry::set_error_handler(err_handler handler) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l: loggers_) {
l.second->set_error_handler(handler);
}
err_handler_ = std::move(handler);
}
void registry::set_error_handler(err_handler handler) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) {
l.second->set_error_handler(handler);
}
err_handler_ = std::move(handler);
}
void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l: loggers_) {
fun(l.second);
}
}
void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) {
fun(l.second);
}
}
void registry::flush_all() {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l: loggers_) {
l.second->flush();
}
}
void registry::flush_all() {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) {
l.second->flush();
}
}
void registry::drop(const std::string &logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
auto is_default_logger = default_logger_ && default_logger_->name() == logger_name;
loggers_.erase(logger_name);
if (is_default_logger) {
default_logger_.reset();
}
}
void registry::drop(const std::string &logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
auto is_default_logger = default_logger_ && default_logger_->name() == logger_name;
loggers_.erase(logger_name);
if (is_default_logger) {
default_logger_.reset();
}
}
void registry::drop_all() {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
loggers_.clear();
default_logger_.reset();
}
void registry::drop_all() {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
loggers_.clear();
default_logger_.reset();
}
// clean all resources and threads started by the registry
void registry::shutdown() {
{
std::lock_guard<std::mutex> lock(flusher_mutex_);
periodic_flusher_.reset();
}
void registry::shutdown() {
{
std::lock_guard<std::mutex> lock(flusher_mutex_);
periodic_flusher_.reset();
}
drop_all();
drop_all();
{
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
tp_.reset();
}
{
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
tp_.reset();
}
}
std::recursive_mutex &registry::tp_mutex() { return tp_mutex_; }
void registry::set_automatic_registration(bool automatic_registration) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
automatic_registration_ = automatic_registration;
}
void registry::set_levels(log_levels levels, level *global_level) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
log_levels_ = std::move(levels);
auto global_level_requested = global_level != nullptr;
global_log_level_ = global_level_requested ? *global_level : global_log_level_;
for (auto &logger : loggers_) {
auto logger_entry = log_levels_.find(logger.first);
if (logger_entry != log_levels_.end()) {
logger.second->set_level(logger_entry->second);
} else if (global_level_requested) {
logger.second->set_level(*global_level);
}
}
}
std::recursive_mutex &registry::tp_mutex() { return tp_mutex_; }
registry &registry::instance() {
static registry s_instance;
return s_instance;
}
void registry::set_automatic_registration(bool automatic_registration) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
automatic_registration_ = automatic_registration;
}
void registry::apply_logger_env_levels(std::shared_ptr<logger> new_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
auto it = log_levels_.find(new_logger->name());
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
new_logger->set_level(new_level);
}
void registry::set_levels(log_levels levels, level *global_level) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
log_levels_ = std::move(levels);
auto global_level_requested = global_level != nullptr;
global_log_level_ = global_level_requested ? *global_level : global_log_level_;
void registry::throw_if_exists_(const std::string &logger_name) {
if (loggers_.find(logger_name) != loggers_.end()) {
throw_spdlog_ex("logger with name '" + logger_name + "' already exists");
}
}
for (auto &logger: loggers_) {
auto logger_entry = log_levels_.find(logger.first);
if (logger_entry != log_levels_.end()) {
logger.second->set_level(logger_entry->second);
} else if (global_level_requested) {
logger.second->set_level(*global_level);
}
}
}
void registry::register_logger_(std::shared_ptr<logger> new_logger) {
auto logger_name = new_logger->name();
throw_if_exists_(logger_name);
loggers_[logger_name] = std::move(new_logger);
}
registry &registry::instance() {
static registry s_instance;
return s_instance;
}
void registry::apply_logger_env_levels(std::shared_ptr<logger> new_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
auto it = log_levels_.find(new_logger->name());
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
new_logger->set_level(new_level);
}
void registry::throw_if_exists_(const std::string &logger_name) {
if (loggers_.find(logger_name) != loggers_.end()) {
throw_spdlog_ex("logger with name '" + logger_name + "' already exists");
}
}
void registry::register_logger_(std::shared_ptr<logger> new_logger) {
auto logger_name = new_logger->name();
throw_if_exists_(logger_name);
loggers_[logger_name] = std::move(new_logger);
}
} // namespace details
} // namespace details
} // namespace spdlog

View File

@ -33,8 +33,7 @@ thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<voi
: thread_pool(q_max_items, threads_n, on_thread_start, [] {}) {}
thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
: thread_pool(
q_max_items, threads_n, [] {}, [] {}) {}
: thread_pool(q_max_items, threads_n, [] {}, [] {}) {}
// message all threads to terminate gracefully join them
thread_pool::~thread_pool() {

View File

@ -3,6 +3,8 @@
#include "spdlog/pattern_formatter.h"
#include <spdlog/mdc.h>
#include <algorithm>
#include <array>
#include <cctype>
@ -16,7 +18,6 @@
#include "spdlog/details/fmt_helper.h"
#include "spdlog/details/log_msg.h"
#include "spdlog/details/os.h"
#include <spdlog/mdc.h>
#include "spdlog/fmt/fmt.h"
#include "spdlog/formatter.h"
@ -744,7 +745,7 @@ template <typename ScopedPadder>
class mdc_formatter final : public flag_formatter {
public:
explicit mdc_formatter(padding_info padinfo)
: flag_formatter(padinfo) {}
: flag_formatter(padinfo) {}
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override {
auto &mdc_map = mdc::get_context();
@ -756,7 +757,7 @@ public:
}
}
void format_mdc(const mdc::mdc_map_t &mdc_map, memory_buf_t &dest){
void format_mdc(const mdc::mdc_map_t &mdc_map, memory_buf_t &dest) {
auto last_element = --mdc_map.end();
for (auto it = mdc_map.begin(); it != mdc_map.end(); ++it) {
auto &pair = *it;
@ -779,7 +780,6 @@ public:
}
};
// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
class full_formatter final : public flag_formatter {

View File

@ -16,7 +16,7 @@ void initialize_logger(std::shared_ptr<logger> logger) { details::registry::inst
std::shared_ptr<logger> get(const std::string &name) { return details::registry::instance().get(name); }
std::shared_ptr<logger> get(std::string_view name) {return details::registry::instance().get(name); }
std::shared_ptr<logger> get(std::string_view name) { return details::registry::instance().get(name); }
std::shared_ptr<logger> get(const char *name) { return details::registry::instance().get(name); }

View File

@ -1,7 +1,7 @@
#include "includes.h"
#include "spdlog/mdc.h"
#include "spdlog/sinks/ostream_sink.h"
#include "test_sink.h"
#include "spdlog/mdc.h"
using spdlog::memory_buf_t;
using spdlog::details::to_string_view;
@ -467,13 +467,11 @@ TEST_CASE("mdc formatter test-1", "[pattern_formatter]") {
formatter->set_pattern("[%n] [%l] [%&] %v");
memory_buf_t formatted;
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
"some message");
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
formatter->format(msg, formatted);
auto expected = spdlog::fmt_lib::format(
"[logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message{}",
spdlog::details::os::default_eol);
auto expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message{}",
spdlog::details::os::default_eol);
REQUIRE(to_string_view(formatted) == expected);
SECTION("Tear down") { spdlog::mdc::clear(); }
@ -487,22 +485,19 @@ TEST_CASE("mdc formatter value update", "[pattern_formatter]") {
formatter->set_pattern("[%n] [%l] [%&] %v");
memory_buf_t formatted_1;
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
"some message");
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
formatter->format(msg, formatted_1);
auto expected = spdlog::fmt_lib::format(
"[logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message{}",
spdlog::details::os::default_eol);
auto expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message{}",
spdlog::details::os::default_eol);
REQUIRE(to_string_view(formatted_1) == expected);
spdlog::mdc::put("mdc_key_1", "new_mdc_value_1");
memory_buf_t formatted_2;
formatter->format(msg, formatted_2);
expected = spdlog::fmt_lib::format(
"[logger-name] [info] [mdc_key_1:new_mdc_value_1 mdc_key_2:mdc_value_2] some message{}",
spdlog::details::os::default_eol);
expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_1:new_mdc_value_1 mdc_key_2:mdc_value_2] some message{}",
spdlog::details::os::default_eol);
REQUIRE(to_string_view(formatted_2) == expected);
@ -512,8 +507,7 @@ TEST_CASE("mdc formatter value update", "[pattern_formatter]") {
TEST_CASE("mdc different threads", "[pattern_formatter]") {
auto formatter = std::make_shared<spdlog::pattern_formatter>();
formatter->set_pattern("[%n] [%l] [%&] %v");
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
"some message");
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
memory_buf_t formatted_2;
@ -522,9 +516,8 @@ TEST_CASE("mdc different threads", "[pattern_formatter]") {
memory_buf_t formatted;
formatter->format(msg, formatted);
auto expected =
spdlog::fmt_lib::format("[logger-name] [info] [mdc_key:thread_1_id] some message{}",
spdlog::details::os::default_eol);
auto expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key:thread_1_id] some message{}",
spdlog::details::os::default_eol);
REQUIRE(to_string_view(formatted) == expected);
};
@ -534,9 +527,8 @@ TEST_CASE("mdc different threads", "[pattern_formatter]") {
memory_buf_t formatted;
formatter->format(msg, formatted);
auto expected =
spdlog::fmt_lib::format("[logger-name] [info] [mdc_key:thread_2_id] some message{}",
spdlog::details::os::default_eol);
auto expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key:thread_2_id] some message{}",
spdlog::details::os::default_eol);
REQUIRE(to_string_view(formatted) == expected);
};
@ -559,13 +551,11 @@ TEST_CASE("mdc remove key", "[pattern_formatter]") {
formatter->set_pattern("[%n] [%l] [%&] %v");
memory_buf_t formatted;
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
"some message");
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
formatter->format(msg, formatted);
auto expected =
spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_2:mdc_value_2] some message{}",
spdlog::details::os::default_eol);
spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_2:mdc_value_2] some message{}", spdlog::details::os::default_eol);
REQUIRE(to_string_view(formatted) == expected);
SECTION("Tear down") { spdlog::mdc::clear(); }
@ -576,12 +566,10 @@ TEST_CASE("mdc empty", "[pattern_formatter]") {
formatter->set_pattern("[%n] [%l] [%&] %v");
memory_buf_t formatted;
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
"some message");
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
formatter->format(msg, formatted);
auto expected = spdlog::fmt_lib::format("[logger-name] [info] [] some message{}",
spdlog::details::os::default_eol);
auto expected = spdlog::fmt_lib::format("[logger-name] [info] [] some message{}", spdlog::details::os::default_eol);
REQUIRE(to_string_view(formatted) == expected);
SECTION("Tear down") { spdlog::mdc::clear(); }

View File

@ -1,5 +1,6 @@
#include "includes.h"
#include <string_view>
#include "includes.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"