Agora  1.2.0
Agora project
registry.h
Go to the documentation of this file.
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 // Loggers registry of unique name->logger pointer
7 // An attempt to create a logger with an already existing name will result with spdlog_ex exception.
8 // If user requests a non existing logger, nullptr will be returned
9 // This class is thread safe
10 
11 #include <spdlog/common.h>
12 
13 #include <chrono>
14 #include <functional>
15 #include <memory>
16 #include <string>
17 #include <unordered_map>
18 #include <mutex>
19 
20 namespace spdlog {
21 class logger;
22 
23 namespace details {
24 class thread_pool;
25 class periodic_worker;
26 
28 {
29 public:
30  using log_levels = std::unordered_map<std::string, level::level_enum>;
31  registry(const registry &) = delete;
32  registry &operator=(const registry &) = delete;
33 
34  void register_logger(std::shared_ptr<logger> new_logger);
35  void initialize_logger(std::shared_ptr<logger> new_logger);
36  std::shared_ptr<logger> get(const std::string &logger_name);
37  std::shared_ptr<logger> default_logger();
38 
39  // Return raw ptr to the default logger.
40  // To be used directly by the spdlog default api (e.g. spdlog::info)
41  // This make the default API faster, but cannot be used concurrently with set_default_logger().
42  // e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
43  logger *get_default_raw();
44 
45  // set default logger.
46  // default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
47  void set_default_logger(std::shared_ptr<logger> new_default_logger);
48 
49  void set_tp(std::shared_ptr<thread_pool> tp);
50 
51  std::shared_ptr<thread_pool> get_tp();
52 
53  // Set global formatter. Each sink in each logger will get a clone of this object
54  void set_formatter(std::unique_ptr<formatter> formatter);
55 
56  void enable_backtrace(size_t n_messages);
57 
58  void disable_backtrace();
59 
60  void set_level(level::level_enum log_level);
61 
62  void flush_on(level::level_enum log_level);
63 
64  void flush_every(std::chrono::seconds interval);
65 
66  void set_error_handler(err_handler handler);
67 
68  void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun);
69 
70  void flush_all();
71 
72  void drop(const std::string &logger_name);
73 
74  void drop_all();
75 
76  // clean all resources and threads started by the registry
77  void shutdown();
78 
79  std::recursive_mutex &tp_mutex();
80 
81  void set_automatic_registration(bool automatic_registration);
82 
83  // set levels for all existing/future loggers. global_level can be null if should not set.
84  void set_levels(log_levels levels, level::level_enum *global_level);
85 
86  static registry &instance();
87 
88 private:
89  registry();
90  ~registry();
91 
92  void throw_if_exists_(const std::string &logger_name);
93  void register_logger_(std::shared_ptr<logger> new_logger);
94  bool set_level_from_cfg_(logger *logger);
95  std::mutex logger_map_mutex_, flusher_mutex_;
96  std::recursive_mutex tp_mutex_;
97  std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
99  std::unique_ptr<formatter> formatter_;
103  std::shared_ptr<thread_pool> tp_;
104  std::unique_ptr<periodic_worker> periodic_flusher_;
105  std::shared_ptr<logger> default_logger_;
106  bool automatic_registration_ = true;
107  size_t backtrace_n_messages_ = 0;
108 };
109 
110 } // namespace details
111 } // namespace spdlog
112 
113 #ifdef SPDLOG_HEADER_ONLY
114 # include "registry-inl.h"
115 #endif
spdlog::formatter
Definition: formatter.h:11
spdlog::details::registry::loggers_
std::unordered_map< std::string, std::shared_ptr< logger > > loggers_
Definition: registry.h:97
spdlog::set_error_handler
SPDLOG_INLINE void set_error_handler(void(*handler)(const std::string &msg))
Definition: spdlog-inl.h:75
spdlog::details::registry::log_levels_
log_levels log_levels_
Definition: registry.h:98
spdlog::details::registry::tp_
std::shared_ptr< thread_pool > tp_
Definition: registry.h:103
spdlog::level::off
@ off
Definition: common.h:219
spdlog::initialize_logger
SPDLOG_INLINE void initialize_logger(std::shared_ptr< logger > logger)
Definition: spdlog-inl.h:15
spdlog::flush_every
SPDLOG_INLINE void flush_every(std::chrono::seconds interval)
Definition: spdlog-inl.h:70
spdlog::default_logger
SPDLOG_INLINE std::shared_ptr< spdlog::logger > default_logger()
Definition: spdlog-inl.h:110
spdlog::set_level
SPDLOG_INLINE void set_level(level::level_enum log_level)
Definition: spdlog-inl.h:60
spdlog::logger
Definition: logger.h:54
spdlog::details::registry
Definition: registry.h:27
spdlog::level::info
@ info
Definition: common.h:215
spdlog::level::level_enum
level_enum
Definition: common.h:211
spdlog::set_automatic_registration
SPDLOG_INLINE void set_automatic_registration(bool automatic_registration)
Definition: spdlog-inl.h:105
spdlog::details::registry::err_handler_
err_handler err_handler_
Definition: registry.h:102
spdlog::details::registry::log_levels
std::unordered_map< std::string, level::level_enum > log_levels
Definition: registry.h:30
spdlog::apply_all
SPDLOG_INLINE void apply_all(const std::function< void(std::shared_ptr< logger >)> &fun)
Definition: spdlog-inl.h:85
spdlog
Definition: async.h:25
spdlog::enable_backtrace
SPDLOG_INLINE void enable_backtrace(size_t n_messages)
Definition: spdlog-inl.h:35
spdlog::drop
SPDLOG_INLINE void drop(const std::string &name)
Definition: spdlog-inl.h:90
spdlog::details::registry::logger_map_mutex_
std::mutex logger_map_mutex_
Definition: registry.h:95
spdlog::get
SPDLOG_INLINE std::shared_ptr< logger > get(const std::string &name)
Definition: spdlog-inl.h:20
spdlog::details::registry::periodic_flusher_
std::unique_ptr< periodic_worker > periodic_flusher_
Definition: registry.h:104
spdlog::err_handler
std::function< void(const std::string &err_msg)> err_handler
Definition: common.h:129
spdlog::register_logger
SPDLOG_INLINE void register_logger(std::shared_ptr< logger > logger)
Definition: spdlog-inl.h:80
spdlog::set_default_logger
SPDLOG_INLINE void set_default_logger(std::shared_ptr< spdlog::logger > default_logger)
Definition: spdlog-inl.h:120
spdlog::details::registry::formatter_
std::unique_ptr< formatter > formatter_
Definition: registry.h:99
function
function[avg_proc_duration, std_proc_duration]
Definition: parse_dl_file.m:1
common.h
spdlog::disable_backtrace
SPDLOG_INLINE void disable_backtrace()
Definition: spdlog-inl.h:40
spdlog::set_formatter
SPDLOG_INLINE void set_formatter(std::unique_ptr< spdlog::formatter > formatter)
Definition: spdlog-inl.h:25
spdlog::thread_pool
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition: async.h:95
spdlog::shutdown
SPDLOG_INLINE void shutdown()
Definition: spdlog-inl.h:100
registry-inl.h
SPDLOG_API
#define SPDLOG_API
Definition: common.h:40
spdlog::drop_all
SPDLOG_INLINE void drop_all()
Definition: spdlog-inl.h:95
spdlog::flush_on
SPDLOG_INLINE void flush_on(level::level_enum log_level)
Definition: spdlog-inl.h:65
spdlog::details::registry::default_logger_
std::shared_ptr< logger > default_logger_
Definition: registry.h:105
spdlog::details::registry::tp_mutex_
std::recursive_mutex tp_mutex_
Definition: registry.h:96