Added registry::set_instance() and shared_instance()

This commit is contained in:
gabime 2024-11-23 13:15:37 +02:00
parent fceb33fa86
commit 8af91bcda0
2 changed files with 22 additions and 7 deletions

View File

@ -26,10 +26,27 @@
#include <memory>
#include <string>
#include <unordered_map>
#include <mutex>
namespace spdlog {
namespace details {
SPDLOG_INLINE static std::shared_ptr<registry> s_instance = std::make_shared<registry>();
SPDLOG_INLINE registry &registry::instance() {
return *s_instance;
}
SPDLOG_INLINE std::shared_ptr<registry> registry::shared_instance() {
return s_instance;
}
SPDLOG_INLINE void registry::set_instance(std::shared_ptr<registry> instance) {
s_instance = instance;
}
SPDLOG_INLINE registry::registry()
: formatter_(new pattern_formatter()) {
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
@ -233,10 +250,6 @@ SPDLOG_INLINE void registry::set_levels(log_levels levels, level::level_enum *gl
}
}
SPDLOG_INLINE registry &registry::instance() {
static registry s_instance;
return s_instance;
}
SPDLOG_INLINE void registry::apply_logger_env_levels(std::shared_ptr<logger> new_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);

View File

@ -26,6 +26,9 @@ class thread_pool;
class SPDLOG_API registry {
public:
registry();
~registry();
using log_levels = std::unordered_map<std::string, level::level_enum>;
registry(const registry &) = delete;
registry &operator=(const registry &) = delete;
@ -95,16 +98,15 @@ public:
// set levels for all existing/future loggers. global_level can be null if should not set.
void set_levels(log_levels levels, level::level_enum *global_level);
static void set_instance(std::shared_ptr<registry> instance);
static registry &instance();
static std::shared_ptr<registry> shared_instance();
void apply_logger_env_levels(std::shared_ptr<logger> new_logger);
std::mutex& console_mutex();
private:
registry();
~registry();
void throw_if_exists_(const std::string &logger_name);
void register_logger_(std::shared_ptr<logger> new_logger);
bool set_level_from_cfg_(logger *logger);