Ported pull #3023 with some changes and tests

This commit is contained in:
gabime 2024-03-16 17:12:46 +02:00
parent 167bf989d8
commit 8e10782a58
5 changed files with 223 additions and 175 deletions

View File

@ -13,6 +13,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <string_view>
#include <unordered_map> #include <unordered_map>
#include "../common.h" #include "../common.h"
@ -35,6 +36,8 @@ public:
void register_logger(std::shared_ptr<logger> new_logger); void register_logger(std::shared_ptr<logger> new_logger);
void initialize_logger(std::shared_ptr<logger> new_logger); void initialize_logger(std::shared_ptr<logger> new_logger);
std::shared_ptr<logger> get(const std::string &logger_name); std::shared_ptr<logger> get(const std::string &logger_name);
std::shared_ptr<logger> get(std::string_view logger_name);
std::shared_ptr<logger> get(const char *logger_name);
std::shared_ptr<logger> default_logger(); std::shared_ptr<logger> default_logger();
// Return raw ptr to the default logger. // Return raw ptr to the default logger.

View File

@ -14,6 +14,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <string_view>
#include "./common.h" #include "./common.h"
#include "./details/registry.h" #include "./details/registry.h"
@ -50,6 +51,8 @@ SPDLOG_API void initialize_logger(std::shared_ptr<logger> logger);
// exist. // exist.
// example: spdlog::get("my_logger")->info("hello {}", "world"); // example: spdlog::get("my_logger")->info("hello {}", "world");
SPDLOG_API std::shared_ptr<logger> get(const std::string &name); SPDLOG_API std::shared_ptr<logger> get(const std::string &name);
SPDLOG_API std::shared_ptr<logger> get(std::string_view name);
SPDLOG_API std::shared_ptr<logger> get(const char *name);
// Set global formatter. Each sink in each logger will get a clone of this object // Set global formatter. Each sink in each logger will get a clone of this object
SPDLOG_API void set_formatter(std::unique_ptr<spdlog::formatter> formatter); SPDLOG_API void set_formatter(std::unique_ptr<spdlog::formatter> formatter);

View File

@ -8,12 +8,14 @@
#include "spdlog/pattern_formatter.h" #include "spdlog/pattern_formatter.h"
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
// support for the default stdout color logger // support for the default stdout color logger
#ifdef _WIN32 #ifdef _WIN32
#include "spdlog/sinks/wincolor_sink.h" #include "spdlog/sinks/wincolor_sink.h"
#else #else
#include "spdlog/sinks/ansicolor_sink.h"
#endif #include "spdlog/sinks/ansicolor_sink.h"
#endif
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER #endif // SPDLOG_DISABLE_DEFAULT_LOGGER
#include <functional> #include <functional>
@ -22,216 +24,230 @@
#include <unordered_map> #include <unordered_map>
namespace spdlog { namespace spdlog {
namespace details { namespace details {
registry::registry() registry::registry()
: formatter_(new pattern_formatter()) { : formatter_(new pattern_formatter()) {
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
// create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows). // create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows).
#ifdef _WIN32 #ifdef _WIN32
auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>(); auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>();
#else #else
auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>(); auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();
#endif #endif
const char *default_logger_name = ""; const char *default_logger_name = "";
default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink)); default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink));
loggers_[default_logger_name] = default_logger_; loggers_[default_logger_name] = default_logger_;
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER #endif // SPDLOG_DISABLE_DEFAULT_LOGGER
} }
registry::~registry() = default; registry::~registry() = default;
void registry::register_logger(std::shared_ptr<logger> new_logger) { void registry::register_logger(std::shared_ptr<logger> new_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
register_logger_(std::move(new_logger)); register_logger_(std::move(new_logger));
} }
void registry::initialize_logger(std::shared_ptr<logger> new_logger) { void registry::initialize_logger(std::shared_ptr<logger> new_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
new_logger->set_formatter(formatter_->clone()); new_logger->set_formatter(formatter_->clone());
if (err_handler_) { if (err_handler_) {
new_logger->set_error_handler(err_handler_); new_logger->set_error_handler(err_handler_);
} }
// set new level according to previously configured level or default level // set new level according to previously configured level or default level
auto it = log_levels_.find(new_logger->name()); auto it = log_levels_.find(new_logger->name());
auto new_level = it != log_levels_.end() ? it->second : global_log_level_; auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
new_logger->set_level(new_level); new_logger->set_level(new_level);
new_logger->flush_on(flush_level_); new_logger->flush_on(flush_level_);
if (automatic_registration_) { if (automatic_registration_) {
register_logger_(std::move(new_logger)); register_logger_(std::move(new_logger));
} }
} }
std::shared_ptr<logger> registry::get(const std::string &logger_name) { std::shared_ptr<logger> registry::get(const std::string &logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
auto found = loggers_.find(logger_name); auto found = loggers_.find(logger_name);
return found == loggers_.end() ? nullptr : found->second; return found == loggers_.end() ? nullptr : found->second;
} }
std::shared_ptr<logger> registry::default_logger() { std::shared_ptr<logger> registry::get(std::string_view logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
return default_logger_; for (const auto &[key, val]: loggers_) {
} if (key == logger_name) {
return val;
}
}
return nullptr;
}
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_;
}
// Return raw ptr to the default logger. // Return raw ptr to the default logger.
// To be used directly by the spdlog default api (e.g. spdlog::info) // 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(). // 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. // 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. // set default logger.
// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map. // 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) { void registry::set_default_logger(std::shared_ptr<logger> new_default_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
// remove previous default logger from the map // remove previous default logger from the map
if (default_logger_ != nullptr) { if (default_logger_ != nullptr) {
loggers_.erase(default_logger_->name()); loggers_.erase(default_logger_->name());
} }
if (new_default_logger != nullptr) { if (new_default_logger != nullptr) {
loggers_[new_default_logger->name()] = new_default_logger; loggers_[new_default_logger->name()] = new_default_logger;
} }
default_logger_ = std::move(new_default_logger); default_logger_ = std::move(new_default_logger);
} }
void registry::set_tp(std::shared_ptr<thread_pool> tp) { void registry::set_tp(std::shared_ptr<thread_pool> tp) {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_); std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
tp_ = std::move(tp); tp_ = std::move(tp);
} }
std::shared_ptr<thread_pool> registry::get_tp() { std::shared_ptr<thread_pool> registry::get_tp() {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_); std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
return tp_; return tp_;
} }
// Set global formatter. Each sink in each logger will get a clone of this object // Set global formatter. Each sink in each logger will get a clone of this object
void registry::set_formatter(std::unique_ptr<formatter> formatter) { void registry::set_formatter(std::unique_ptr<formatter> formatter) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
formatter_ = std::move(formatter); formatter_ = std::move(formatter);
for (auto &l : loggers_) { for (auto &l: loggers_) {
l.second->set_formatter(formatter_->clone()); l.second->set_formatter(formatter_->clone());
} }
} }
void registry::set_level(level level) { void registry::set_level(level level) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) { for (auto &l: loggers_) {
l.second->set_level(level); l.second->set_level(level);
} }
global_log_level_ = level; global_log_level_ = level;
} }
void registry::flush_on(level level) { void registry::flush_on(level level) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) { for (auto &l: loggers_) {
l.second->flush_on(level); l.second->flush_on(level);
} }
flush_level_ = level; flush_level_ = level;
} }
void registry::set_error_handler(err_handler handler) { void registry::set_error_handler(err_handler handler) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) { for (auto &l: loggers_) {
l.second->set_error_handler(handler); l.second->set_error_handler(handler);
} }
err_handler_ = std::move(handler); err_handler_ = std::move(handler);
} }
void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) { void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) { for (auto &l: loggers_) {
fun(l.second); fun(l.second);
} }
} }
void registry::flush_all() { void registry::flush_all() {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) { for (auto &l: loggers_) {
l.second->flush(); l.second->flush();
} }
} }
void registry::drop(const std::string &logger_name) { void registry::drop(const std::string &logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
auto is_default_logger = default_logger_ && default_logger_->name() == logger_name; auto is_default_logger = default_logger_ && default_logger_->name() == logger_name;
loggers_.erase(logger_name); loggers_.erase(logger_name);
if (is_default_logger) { if (is_default_logger) {
default_logger_.reset(); default_logger_.reset();
} }
} }
void registry::drop_all() { void registry::drop_all() {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
loggers_.clear(); loggers_.clear();
default_logger_.reset(); default_logger_.reset();
} }
// clean all resources and threads started by the registry // clean all resources and threads started by the registry
void registry::shutdown() { void registry::shutdown() {
{ {
std::lock_guard<std::mutex> lock(flusher_mutex_); std::lock_guard<std::mutex> lock(flusher_mutex_);
periodic_flusher_.reset(); periodic_flusher_.reset();
} }
drop_all(); drop_all();
{ {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_); std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
tp_.reset(); 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);
} }
}
}
registry &registry::instance() { std::recursive_mutex &registry::tp_mutex() { return tp_mutex_; }
static registry s_instance;
return s_instance;
}
void registry::apply_logger_env_levels(std::shared_ptr<logger> new_logger) { void registry::set_automatic_registration(bool automatic_registration) {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
auto it = log_levels_.find(new_logger->name()); automatic_registration_ = automatic_registration;
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) { void registry::set_levels(log_levels levels, level *global_level) {
if (loggers_.find(logger_name) != loggers_.end()) { std::lock_guard<std::mutex> lock(logger_map_mutex_);
throw_spdlog_ex("logger with name '" + logger_name + "' already exists"); 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::register_logger_(std::shared_ptr<logger> new_logger) { for (auto &logger: loggers_) {
auto logger_name = new_logger->name(); auto logger_entry = log_levels_.find(logger.first);
throw_if_exists_(logger_name); if (logger_entry != log_levels_.end()) {
loggers_[logger_name] = std::move(new_logger); logger.second->set_level(logger_entry->second);
} } else if (global_level_requested) {
logger.second->set_level(*global_level);
}
}
}
} // namespace details 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 spdlog } // namespace spdlog

View File

@ -16,6 +16,10 @@ 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(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(const char *name) { return details::registry::instance().get(name); }
void set_formatter(std::unique_ptr<spdlog::formatter> formatter) { void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
details::registry::instance().set_formatter(std::move(formatter)); details::registry::instance().set_formatter(std::move(formatter));
} }

View File

@ -1,4 +1,5 @@
#include "includes.h" #include "includes.h"
#include <string_view>
#include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h" #include "spdlog/sinks/rotating_file_sink.h"
@ -106,3 +107,24 @@ TEST_CASE("disable automatic registration", "[registry]") {
spdlog::set_level(spdlog::level::info); spdlog::set_level(spdlog::level::info);
spdlog::set_automatic_registration(true); spdlog::set_automatic_registration(true);
} }
TEST_CASE("get(const char* name)", "[registry]") {
spdlog::drop_all();
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
spdlog::drop_all();
}
TEST_CASE("get(std::string_view name)", "[registry]") {
spdlog::drop_all();
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
REQUIRE(spdlog::get(std::string_view(tested_logger_name)) != nullptr);
spdlog::drop_all();
}
TEST_CASE("get(std::string name)", "[registry]") {
spdlog::drop_all();
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
REQUIRE(spdlog::get(std::string(tested_logger_name)) != nullptr);
spdlog::drop_all();
}