| 1 | // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. |
|---|---|
| 2 | // Distributed under the MIT License (http://opensource.org/licenses/MIT) |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #ifndef SPDLOG_HEADER_ONLY |
| 7 | #include <spdlog/details/registry.h> |
| 8 | #endif |
| 9 | |
| 10 | #include <spdlog/common.h> |
| 11 | #include <spdlog/details/periodic_worker.h> |
| 12 | #include <spdlog/logger.h> |
| 13 | #include <spdlog/pattern_formatter.h> |
| 14 | |
| 15 | #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER |
| 16 | // support for the default stdout color logger |
| 17 | #ifdef _WIN32 |
| 18 | #include <spdlog/sinks/wincolor_sink.h> |
| 19 | #else |
| 20 | #include <spdlog/sinks/ansicolor_sink.h> |
| 21 | #endif |
| 22 | #endif // SPDLOG_DISABLE_DEFAULT_LOGGER |
| 23 | |
| 24 | #include <chrono> |
| 25 | #include <functional> |
| 26 | #include <memory> |
| 27 | #include <string> |
| 28 | #include <unordered_map> |
| 29 | |
| 30 | namespace spdlog { |
| 31 | namespace details { |
| 32 | |
| 33 | SPDLOG_INLINE registry::registry() |
| 34 | : formatter_(new pattern_formatter()) { |
| 35 | #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER |
| 36 | // create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows). |
| 37 | #ifdef _WIN32 |
| 38 | auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>(); |
| 39 | #else |
| 40 | auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>(); |
| 41 | #endif |
| 42 | |
| 43 | const char *default_logger_name = ""; |
| 44 | default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink)); |
| 45 | loggers_[default_logger_name] = default_logger_; |
| 46 | |
| 47 | #endif // SPDLOG_DISABLE_DEFAULT_LOGGER |
| 48 | } |
| 49 | |
| 50 | SPDLOG_INLINE registry::~registry() = default; |
| 51 | |
| 52 | SPDLOG_INLINE void registry::register_logger(std::shared_ptr<logger> new_logger) { |
| 53 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 54 | register_logger_(new_logger: std::move(new_logger)); |
| 55 | } |
| 56 | |
| 57 | SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logger) { |
| 58 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 59 | new_logger->set_formatter(formatter_->clone()); |
| 60 | |
| 61 | if (err_handler_) { |
| 62 | new_logger->set_error_handler(err_handler_); |
| 63 | } |
| 64 | |
| 65 | // set new level according to previously configured level or default level |
| 66 | auto it = log_levels_.find(x: new_logger->name()); |
| 67 | auto new_level = it != log_levels_.end() ? it->second : global_log_level_; |
| 68 | new_logger->set_level(new_level); |
| 69 | |
| 70 | new_logger->flush_on(log_level: flush_level_); |
| 71 | |
| 72 | if (backtrace_n_messages_ > 0) { |
| 73 | new_logger->enable_backtrace(n_messages: backtrace_n_messages_); |
| 74 | } |
| 75 | |
| 76 | if (automatic_registration_) { |
| 77 | register_logger_(new_logger: std::move(new_logger)); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | SPDLOG_INLINE std::shared_ptr<logger> registry::get(const std::string &logger_name) { |
| 82 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 83 | auto found = loggers_.find(x: logger_name); |
| 84 | return found == loggers_.end() ? nullptr : found->second; |
| 85 | } |
| 86 | |
| 87 | SPDLOG_INLINE std::shared_ptr<logger> registry::default_logger() { |
| 88 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 89 | return default_logger_; |
| 90 | } |
| 91 | |
| 92 | // Return raw ptr to the default logger. |
| 93 | // To be used directly by the spdlog default api (e.g. spdlog::info) |
| 94 | // This make the default API faster, but cannot be used concurrently with set_default_logger(). |
| 95 | // e.g do not call set_default_logger() from one thread while calling spdlog::info() from another. |
| 96 | SPDLOG_INLINE logger *registry::get_default_raw() { return default_logger_.get(); } |
| 97 | |
| 98 | // set default logger. |
| 99 | // default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map. |
| 100 | SPDLOG_INLINE void registry::set_default_logger(std::shared_ptr<logger> new_default_logger) { |
| 101 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 102 | if (new_default_logger != nullptr) { |
| 103 | loggers_[new_default_logger->name()] = new_default_logger; |
| 104 | } |
| 105 | default_logger_ = std::move(new_default_logger); |
| 106 | } |
| 107 | |
| 108 | SPDLOG_INLINE void registry::set_tp(std::shared_ptr<thread_pool> tp) { |
| 109 | std::lock_guard<std::recursive_mutex> lock(tp_mutex_); |
| 110 | tp_ = std::move(tp); |
| 111 | } |
| 112 | |
| 113 | SPDLOG_INLINE std::shared_ptr<thread_pool> registry::get_tp() { |
| 114 | std::lock_guard<std::recursive_mutex> lock(tp_mutex_); |
| 115 | return tp_; |
| 116 | } |
| 117 | |
| 118 | // Set global formatter. Each sink in each logger will get a clone of this object |
| 119 | SPDLOG_INLINE void registry::set_formatter(std::unique_ptr<formatter> formatter) { |
| 120 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 121 | formatter_ = std::move(formatter); |
| 122 | for (auto &l : loggers_) { |
| 123 | l.second->set_formatter(formatter_->clone()); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | SPDLOG_INLINE void registry::enable_backtrace(size_t n_messages) { |
| 128 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 129 | backtrace_n_messages_ = n_messages; |
| 130 | |
| 131 | for (auto &l : loggers_) { |
| 132 | l.second->enable_backtrace(n_messages); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | SPDLOG_INLINE void registry::disable_backtrace() { |
| 137 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 138 | backtrace_n_messages_ = 0; |
| 139 | for (auto &l : loggers_) { |
| 140 | l.second->disable_backtrace(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | SPDLOG_INLINE void registry::set_level(level::level_enum log_level) { |
| 145 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 146 | for (auto &l : loggers_) { |
| 147 | l.second->set_level(log_level); |
| 148 | } |
| 149 | global_log_level_ = log_level; |
| 150 | } |
| 151 | |
| 152 | SPDLOG_INLINE void registry::flush_on(level::level_enum log_level) { |
| 153 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 154 | for (auto &l : loggers_) { |
| 155 | l.second->flush_on(log_level); |
| 156 | } |
| 157 | flush_level_ = log_level; |
| 158 | } |
| 159 | |
| 160 | SPDLOG_INLINE void registry::set_error_handler(err_handler handler) { |
| 161 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 162 | for (auto &l : loggers_) { |
| 163 | l.second->set_error_handler(handler); |
| 164 | } |
| 165 | err_handler_ = std::move(handler); |
| 166 | } |
| 167 | |
| 168 | SPDLOG_INLINE void registry::apply_all( |
| 169 | const std::function<void(const std::shared_ptr<logger>)> &fun) { |
| 170 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 171 | for (auto &l : loggers_) { |
| 172 | fun(l.second); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | SPDLOG_INLINE void registry::flush_all() { |
| 177 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 178 | for (auto &l : loggers_) { |
| 179 | l.second->flush(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | SPDLOG_INLINE void registry::drop(const std::string &logger_name) { |
| 184 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 185 | auto is_default_logger = default_logger_ && default_logger_->name() == logger_name; |
| 186 | loggers_.erase(x: logger_name); |
| 187 | if (is_default_logger) { |
| 188 | default_logger_.reset(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | SPDLOG_INLINE void registry::drop_all() { |
| 193 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 194 | loggers_.clear(); |
| 195 | default_logger_.reset(); |
| 196 | } |
| 197 | |
| 198 | // clean all resources and threads started by the registry |
| 199 | SPDLOG_INLINE void registry::shutdown() { |
| 200 | { |
| 201 | std::lock_guard<std::mutex> lock(flusher_mutex_); |
| 202 | periodic_flusher_.reset(); |
| 203 | } |
| 204 | |
| 205 | drop_all(); |
| 206 | |
| 207 | { |
| 208 | std::lock_guard<std::recursive_mutex> lock(tp_mutex_); |
| 209 | tp_.reset(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | SPDLOG_INLINE std::recursive_mutex ®istry::tp_mutex() { return tp_mutex_; } |
| 214 | |
| 215 | SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registration) { |
| 216 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 217 | automatic_registration_ = automatic_registration; |
| 218 | } |
| 219 | |
| 220 | SPDLOG_INLINE void registry::set_levels(log_levels levels, level::level_enum *global_level) { |
| 221 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 222 | log_levels_ = std::move(levels); |
| 223 | auto global_level_requested = global_level != nullptr; |
| 224 | global_log_level_ = global_level_requested ? *global_level : global_log_level_; |
| 225 | |
| 226 | for (auto &logger : loggers_) { |
| 227 | auto logger_entry = log_levels_.find(x: logger.first); |
| 228 | if (logger_entry != log_levels_.end()) { |
| 229 | logger.second->set_level(logger_entry->second); |
| 230 | } else if (global_level_requested) { |
| 231 | logger.second->set_level(*global_level); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | SPDLOG_INLINE registry ®istry::instance() { |
| 237 | static registry s_instance; |
| 238 | return s_instance; |
| 239 | } |
| 240 | |
| 241 | SPDLOG_INLINE void registry::apply_logger_env_levels(std::shared_ptr<logger> new_logger) { |
| 242 | std::lock_guard<std::mutex> lock(logger_map_mutex_); |
| 243 | auto it = log_levels_.find(x: new_logger->name()); |
| 244 | auto new_level = it != log_levels_.end() ? it->second : global_log_level_; |
| 245 | new_logger->set_level(new_level); |
| 246 | } |
| 247 | |
| 248 | SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name) { |
| 249 | if (loggers_.find(x: logger_name) != loggers_.end()) { |
| 250 | throw_spdlog_ex(msg: "logger with name '"+ logger_name + "' already exists"); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | SPDLOG_INLINE void registry::register_logger_(std::shared_ptr<logger> new_logger) { |
| 255 | auto logger_name = new_logger->name(); |
| 256 | throw_if_exists_(logger_name); |
| 257 | loggers_[logger_name] = std::move(new_logger); |
| 258 | } |
| 259 | |
| 260 | } // namespace details |
| 261 | } // namespace spdlog |
| 262 |