Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:41

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 // Loggers registry of unique name->logger pointer
0007 // An attempt to create a logger with an already existing name will result with spdlog_ex exception.
0008 // If user requests a non existing logger, nullptr will be returned
0009 // This class is thread safe
0010 
0011 #include <spdlog/common.h>
0012 #include <spdlog/details/periodic_worker.h>
0013 
0014 #include <chrono>
0015 #include <functional>
0016 #include <memory>
0017 #include <string>
0018 #include <unordered_map>
0019 #include <mutex>
0020 
0021 namespace spdlog {
0022 class logger;
0023 
0024 namespace details {
0025 class thread_pool;
0026 
0027 class SPDLOG_API registry
0028 {
0029 public:
0030     using log_levels = std::unordered_map<std::string, level::level_enum>;
0031     registry(const registry &) = delete;
0032     registry &operator=(const registry &) = delete;
0033 
0034     void register_logger(std::shared_ptr<logger> new_logger);
0035     void initialize_logger(std::shared_ptr<logger> new_logger);
0036     std::shared_ptr<logger> get(const std::string &logger_name);
0037     std::shared_ptr<logger> default_logger();
0038 
0039     // Return raw ptr to the default logger.
0040     // To be used directly by the spdlog default api (e.g. spdlog::info)
0041     // This make the default API faster, but cannot be used concurrently with set_default_logger().
0042     // e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
0043     logger *get_default_raw();
0044 
0045     // set default logger.
0046     // default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
0047     void set_default_logger(std::shared_ptr<logger> new_default_logger);
0048 
0049     void set_tp(std::shared_ptr<thread_pool> tp);
0050 
0051     std::shared_ptr<thread_pool> get_tp();
0052 
0053     // Set global formatter. Each sink in each logger will get a clone of this object
0054     void set_formatter(std::unique_ptr<formatter> formatter);
0055 
0056     void enable_backtrace(size_t n_messages);
0057 
0058     void disable_backtrace();
0059 
0060     void set_level(level::level_enum log_level);
0061 
0062     void flush_on(level::level_enum log_level);
0063 
0064     template<typename Rep, typename Period>
0065     void flush_every(std::chrono::duration<Rep, Period> interval)
0066     {
0067         std::lock_guard<std::mutex> lock(flusher_mutex_);
0068         auto clbk = [this]() { this->flush_all(); };
0069         periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval);
0070     }
0071 
0072     void set_error_handler(err_handler handler);
0073 
0074     void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun);
0075 
0076     void flush_all();
0077 
0078     void drop(const std::string &logger_name);
0079 
0080     void drop_all();
0081 
0082     // clean all resources and threads started by the registry
0083     void shutdown();
0084 
0085     std::recursive_mutex &tp_mutex();
0086 
0087     void set_automatic_registration(bool automatic_registration);
0088 
0089     // set levels for all existing/future loggers. global_level can be null if should not set.
0090     void set_levels(log_levels levels, level::level_enum *global_level);
0091 
0092     static registry &instance();
0093 
0094 private:
0095     registry();
0096     ~registry();
0097 
0098     void throw_if_exists_(const std::string &logger_name);
0099     void register_logger_(std::shared_ptr<logger> new_logger);
0100     bool set_level_from_cfg_(logger *logger);
0101     std::mutex logger_map_mutex_, flusher_mutex_;
0102     std::recursive_mutex tp_mutex_;
0103     std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
0104     log_levels log_levels_;
0105     std::unique_ptr<formatter> formatter_;
0106     spdlog::level::level_enum global_log_level_ = level::info;
0107     level::level_enum flush_level_ = level::off;
0108     err_handler err_handler_;
0109     std::shared_ptr<thread_pool> tp_;
0110     std::unique_ptr<periodic_worker> periodic_flusher_;
0111     std::shared_ptr<logger> default_logger_;
0112     bool automatic_registration_ = true;
0113     size_t backtrace_n_messages_ = 0;
0114 };
0115 
0116 } // namespace details
0117 } // namespace spdlog
0118 
0119 #ifdef SPDLOG_HEADER_ONLY
0120 #    include "registry-inl.h"
0121 #endif