17 #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER 24 #endif // SPDLOG_DISABLE_DEFAULT_LOGGER 30 #include <unordered_map> 44 std::lock_guard<std::mutex> lock(logger_map_mutex_);
45 register_logger_(std::move(new_logger));
50 std::lock_guard<std::mutex> lock(logger_map_mutex_);
51 new_logger->set_formatter(formatter_->clone());
55 new_logger->set_error_handler(err_handler_);
58 new_logger->set_level(level_);
59 new_logger->flush_on(flush_level_);
61 if (automatic_registration_)
63 register_logger_(std::move(new_logger));
67 std::shared_ptr<logger>
get(
const std::string &logger_name)
69 std::lock_guard<std::mutex> lock(logger_map_mutex_);
70 auto found = loggers_.find(logger_name);
71 return found == loggers_.end() ?
nullptr : found->second;
76 std::lock_guard<std::mutex> lock(logger_map_mutex_);
77 return default_logger_;
86 return default_logger_.get();
93 std::lock_guard<std::mutex> lock(logger_map_mutex_);
95 if (default_logger_ !=
nullptr)
97 loggers_.erase(default_logger_->name());
99 if (new_default_logger !=
nullptr)
101 loggers_[new_default_logger->name()] = new_default_logger;
103 default_logger_ = std::move(new_default_logger);
106 void set_tp(std::shared_ptr<thread_pool> tp)
108 std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
114 std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
121 std::lock_guard<std::mutex> lock(logger_map_mutex_);
122 formatter_ = std::move(formatter);
123 for (
auto &l : loggers_)
125 l.second->set_formatter(formatter_->clone());
131 std::lock_guard<std::mutex> lock(logger_map_mutex_);
132 for (
auto &l : loggers_)
134 l.second->set_level(log_level);
141 std::lock_guard<std::mutex> lock(logger_map_mutex_);
142 for (
auto &l : loggers_)
144 l.second->flush_on(log_level);
146 flush_level_ = log_level;
151 std::lock_guard<std::mutex> lock(flusher_mutex_);
153 periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval);
158 std::lock_guard<std::mutex> lock(logger_map_mutex_);
159 for (
auto &l : loggers_)
161 l.second->set_error_handler(handler);
163 err_handler_ = handler;
166 void apply_all(
const std::function<
void(
const std::shared_ptr<logger>)> &fun)
168 std::lock_guard<std::mutex> lock(logger_map_mutex_);
169 for (
auto &l : loggers_)
177 std::lock_guard<std::mutex> lock(logger_map_mutex_);
178 for (
auto &l : loggers_)
184 void drop(
const std::string &logger_name)
186 std::lock_guard<std::mutex> lock(logger_map_mutex_);
187 loggers_.erase(logger_name);
188 if (default_logger_ && default_logger_->name() == logger_name)
190 default_logger_.reset();
196 std::lock_guard<std::mutex> lock(logger_map_mutex_);
198 default_logger_.reset();
205 std::lock_guard<std::mutex> lock(flusher_mutex_);
206 periodic_flusher_.reset();
212 std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
224 std::lock_guard<std::mutex> lock(logger_map_mutex_);
225 automatic_registration_ = automatic_regsistration;
239 #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER 242 auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>();
244 auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();
247 const char *default_logger_name =
"";
248 default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink));
249 loggers_[default_logger_name] = default_logger_;
251 #endif // SPDLOG_DISABLE_DEFAULT_LOGGER 256 void throw_if_exists_(
const std::string &logger_name)
258 if (loggers_.find(logger_name) != loggers_.end())
260 throw spdlog_ex(
"logger with name '" + logger_name +
"' already exists");
264 void register_logger_(std::shared_ptr<logger> new_logger)
266 auto logger_name = new_logger->name();
267 throw_if_exists_(logger_name);
268 loggers_[logger_name] = std::move(new_logger);
271 std::mutex logger_map_mutex_, flusher_mutex_;
272 std::recursive_mutex tp_mutex_;
273 std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
274 std::unique_ptr<formatter> formatter_;
278 std::shared_ptr<thread_pool> tp_;
279 std::unique_ptr<periodic_worker> periodic_flusher_;
280 std::shared_ptr<logger> default_logger_;
281 bool automatic_registration_ =
true;
std::shared_ptr< thread_pool > get_tp()
void apply_all(const std::function< void(const std::shared_ptr< logger >)> &fun)
void set_formatter(std::unique_ptr< formatter > formatter)
void flush_on(level::level_enum log_level)
static registry & instance()
void set_error_handler(log_err_handler handler)
std::recursive_mutex & tp_mutex()
void set_tp(std::shared_ptr< thread_pool > tp)
static level::level_enum default_level()
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
void set_level(level::level_enum log_level)
registry & operator=(const registry &)=delete
void initialize_logger(std::shared_ptr< logger > new_logger)
void set_default_logger(std::shared_ptr< logger > new_default_logger)
void set_automatic_registration(bool automatic_regsistration)
void flush_every(std::chrono::seconds interval)
void drop(const std::string &logger_name)
std::function< void(const std::string &err_msg)> log_err_handler
void register_logger(std::shared_ptr< logger > new_logger)
logger * get_default_raw()
std::shared_ptr< logger > default_logger()