mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 08:25:43 +08:00
replace static registry instance with allocated pointer to be deallocated registry only in shutdown()
This commit is contained in:
parent
3b69fc58b5
commit
7956361956
@ -84,16 +84,15 @@ int main(int, char *[]) {
|
||||
// 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();
|
||||
}
|
||||
|
||||
// Exceptions will only be thrown upon failed logger or sink construction (not during logging).
|
||||
catch (const spdlog::spdlog_ex &ex) {
|
||||
std::printf("Log initialization failed: %s\n", ex.what());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Release all spdlog resources, and drop all loggers in the registry.
|
||||
spdlog::shutdown();
|
||||
}
|
||||
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
|
@ -35,22 +35,22 @@ template <async_overflow_policy OverflowPolicy = async_overflow_policy::block>
|
||||
struct async_factory_impl {
|
||||
template <typename Sink, typename... SinkArgs>
|
||||
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args) {
|
||||
auto ®istry_inst = details::registry::instance();
|
||||
auto *registry_inst = details::registry::instance();
|
||||
|
||||
// create global thread pool if not already exists..
|
||||
|
||||
auto &mutex = registry_inst.tp_mutex();
|
||||
auto &mutex = registry_inst->tp_mutex();
|
||||
std::lock_guard<std::recursive_mutex> tp_lock(mutex);
|
||||
auto tp = registry_inst.get_tp();
|
||||
auto tp = registry_inst->get_tp();
|
||||
if (tp == nullptr) {
|
||||
tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1U);
|
||||
registry_inst.set_tp(tp);
|
||||
registry_inst->set_tp(tp);
|
||||
}
|
||||
|
||||
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
||||
auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink),
|
||||
std::move(tp), OverflowPolicy);
|
||||
registry_inst.initialize_logger(new_logger);
|
||||
registry_inst->initialize_logger(new_logger);
|
||||
return new_logger;
|
||||
}
|
||||
};
|
||||
@ -79,7 +79,7 @@ inline void init_thread_pool(size_t q_size,
|
||||
std::function<void()> on_thread_stop) {
|
||||
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start,
|
||||
on_thread_stop);
|
||||
details::registry::instance().set_tp(std::move(tp));
|
||||
details::registry::instance()->set_tp(std::move(tp));
|
||||
}
|
||||
|
||||
inline void init_thread_pool(size_t q_size,
|
||||
@ -95,6 +95,6 @@ inline void init_thread_pool(size_t q_size, size_t thread_count) {
|
||||
|
||||
// get the global thread pool.
|
||||
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() {
|
||||
return details::registry::instance().get_tp();
|
||||
return details::registry::instance()->get_tp();
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@ -28,7 +28,9 @@ class SPDLOG_API registry {
|
||||
public:
|
||||
using log_levels = std::unordered_map<std::string, level>;
|
||||
|
||||
static registry &instance();
|
||||
static registry *instance();
|
||||
registry();
|
||||
~registry();
|
||||
registry(const registry &) = delete;
|
||||
registry &operator=(const registry &) = delete;
|
||||
|
||||
@ -89,9 +91,6 @@ public:
|
||||
void apply_logger_env_levels(std::shared_ptr<logger> new_logger);
|
||||
|
||||
private:
|
||||
registry();
|
||||
~registry();
|
||||
|
||||
void throw_if_exists_(const std::string &logger_name);
|
||||
void register_logger_(std::shared_ptr<logger> new_logger);
|
||||
std::mutex logger_map_mutex_, flusher_mutex_;
|
||||
|
@ -15,7 +15,7 @@ struct synchronous_factory {
|
||||
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...args) {
|
||||
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
||||
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));
|
||||
details::registry::instance().initialize_logger(new_logger);
|
||||
details::registry::instance()->initialize_logger(new_logger);
|
||||
return new_logger;
|
||||
}
|
||||
};
|
||||
|
@ -76,7 +76,7 @@ SPDLOG_API void flush_on(level level);
|
||||
// Warning: Use only if all your loggers are thread safe!
|
||||
template <typename Rep, typename Period>
|
||||
inline void flush_every(std::chrono::duration<Rep, Period> interval) {
|
||||
details::registry::instance().flush_every(interval);
|
||||
details::registry::instance()->flush_every(interval);
|
||||
}
|
||||
|
||||
// Set global error handler
|
||||
|
@ -93,7 +93,7 @@ void load_levels(const std::string &input) {
|
||||
}
|
||||
}
|
||||
|
||||
details::registry::instance().set_levels(std::move(levels),
|
||||
details::registry::instance()->set_levels(std::move(levels),
|
||||
global_level_found ? &global_level : nullptr);
|
||||
}
|
||||
|
||||
|
@ -211,8 +211,8 @@ void registry::set_levels(log_levels levels, level *global_level) {
|
||||
}
|
||||
}
|
||||
|
||||
registry ®istry::instance() {
|
||||
static registry s_instance;
|
||||
registry *registry::instance() {
|
||||
static registry *s_instance = new registry();
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
|
@ -13,15 +13,15 @@
|
||||
namespace spdlog {
|
||||
|
||||
void initialize_logger(std::shared_ptr<logger> logger) {
|
||||
details::registry::instance().initialize_logger(std::move(logger));
|
||||
details::registry::instance()->initialize_logger(std::move(logger));
|
||||
}
|
||||
|
||||
std::shared_ptr<logger> get(const std::string &name) {
|
||||
return details::registry::instance().get(name);
|
||||
return details::registry::instance()->get(name);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
void set_pattern(std::string pattern, pattern_time_type time_type) {
|
||||
@ -32,43 +32,46 @@ level get_level() { return default_logger_raw()->log_level(); }
|
||||
|
||||
bool should_log(level level) { return default_logger_raw()->should_log(level); }
|
||||
|
||||
void set_level(level level) { details::registry::instance().set_level(level); }
|
||||
void set_level(level level) { details::registry::instance()->set_level(level); }
|
||||
|
||||
void flush_on(level level) { details::registry::instance().flush_on(level); }
|
||||
void flush_on(level level) { details::registry::instance()->flush_on(level); }
|
||||
|
||||
void set_error_handler(void (*handler)(const std::string &msg)) {
|
||||
details::registry::instance().set_error_handler(handler);
|
||||
details::registry::instance()->set_error_handler(handler);
|
||||
}
|
||||
|
||||
void register_logger(std::shared_ptr<logger> logger) {
|
||||
details::registry::instance().register_logger(std::move(logger));
|
||||
details::registry::instance()->register_logger(std::move(logger));
|
||||
}
|
||||
|
||||
void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun) {
|
||||
details::registry::instance().apply_all(fun);
|
||||
details::registry::instance()->apply_all(fun);
|
||||
}
|
||||
|
||||
void drop(const std::string &name) { details::registry::instance().drop(name); }
|
||||
void drop(const std::string &name) { details::registry::instance()->drop(name); }
|
||||
|
||||
void drop_all() { details::registry::instance().drop_all(); }
|
||||
void drop_all() { details::registry::instance()->drop_all(); }
|
||||
|
||||
void shutdown() { details::registry::instance().shutdown(); }
|
||||
void shutdown() {
|
||||
details::registry::instance()->shutdown();
|
||||
delete details::registry::instance();
|
||||
}
|
||||
|
||||
void set_automatic_registration(bool automatic_registration) {
|
||||
details::registry::instance().set_automatic_registration(automatic_registration);
|
||||
details::registry::instance()->set_automatic_registration(automatic_registration);
|
||||
}
|
||||
|
||||
std::shared_ptr<spdlog::logger> default_logger() {
|
||||
return details::registry::instance().default_logger();
|
||||
return details::registry::instance()->default_logger();
|
||||
}
|
||||
|
||||
spdlog::logger *default_logger_raw() { return details::registry::instance().get_default_raw(); }
|
||||
spdlog::logger *default_logger_raw() { return details::registry::instance()->get_default_raw(); }
|
||||
|
||||
void set_default_logger(std::shared_ptr<spdlog::logger> default_logger) {
|
||||
details::registry::instance().set_default_logger(std::move(default_logger));
|
||||
details::registry::instance()->set_default_logger(std::move(default_logger));
|
||||
}
|
||||
|
||||
void apply_logger_env_levels(std::shared_ptr<logger> logger) {
|
||||
details::registry::instance().apply_logger_env_levels(std::move(logger));
|
||||
details::registry::instance()->apply_logger_env_levels(std::move(logger));
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
Loading…
Reference in New Issue
Block a user