Use std::function for the global error handler

This commit is contained in:
Sprite 2021-06-16 05:04:17 +08:00
parent 7a7611e977
commit 7fa751d36e
2 changed files with 4 additions and 4 deletions

View File

@ -195,14 +195,14 @@ SPDLOG_INLINE void registry::flush_every(std::chrono::seconds interval)
periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval); periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval);
} }
SPDLOG_INLINE void registry::set_error_handler(void (*handler)(const std::string &msg)) SPDLOG_INLINE 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_ = handler; err_handler_ = std::move(handler);
} }
SPDLOG_INLINE void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) SPDLOG_INLINE void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun)

View File

@ -63,7 +63,7 @@ public:
void flush_every(std::chrono::seconds interval); void flush_every(std::chrono::seconds interval);
void set_error_handler(void (*handler)(const std::string &msg)); void set_error_handler(err_handler handler);
void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun); void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun);
@ -99,7 +99,7 @@ private:
std::unique_ptr<formatter> formatter_; std::unique_ptr<formatter> formatter_;
spdlog::level::level_enum global_log_level_ = level::info; spdlog::level::level_enum global_log_level_ = level::info;
level::level_enum flush_level_ = level::off; level::level_enum flush_level_ = level::off;
void (*err_handler_)(const std::string &msg) = nullptr; err_handler err_handler_;
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_;