spdlog
Loading...
Searching...
No Matches
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
20namespace spdlog {
21class logger;
22
23namespace details {
24class thread_pool;
25class periodic_worker;
26
28{
29public:
31 registry(const registry &) = delete;
32 registry &operator=(const registry &) = delete;
33
36 std::shared_ptr<logger> get(const std::string &logger_name);
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
52
53 // Set global formatter. Each sink in each logger will get a clone of this object
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
88private:
89 registry();
91
92 void throw_if_exists_(const std::string &logger_name);
93 void register_logger_(std::shared_ptr<logger> new_logger);
95 std::mutex logger_map_mutex_, flusher_mutex_;
100 spdlog::level::level_enum global_log_level_ = level::info;
101 level::level_enum flush_level_ = level::off;
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
std::recursive_mutex tp_mutex_
Definition registry.h:96
std::shared_ptr< thread_pool > tp_
Definition registry.h:103
std::unique_ptr< periodic_worker > periodic_flusher_
Definition registry.h:104
std::unique_ptr< formatter > formatter_
Definition registry.h:99
registry & operator=(const registry &)=delete
registry(const registry &)=delete
std::unordered_map< std::string, std::shared_ptr< logger > > loggers_
Definition registry.h:97
std::shared_ptr< logger > default_logger_
Definition registry.h:105
bool set_level_from_cfg_(logger *logger)
std::mutex flusher_mutex_
Definition registry.h:95
#define SPDLOG_API
Definition common.h:31
Definition async.h:25
SPDLOG_INLINE void disable_backtrace()
Definition spdlog-inl.h:40
SPDLOG_INLINE void register_logger(std::shared_ptr< logger > logger)
Definition spdlog-inl.h:80
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition async.h:89
SPDLOG_INLINE void flush_every(std::chrono::seconds interval)
Definition spdlog-inl.h:70
SPDLOG_INLINE void initialize_logger(std::shared_ptr< logger > logger)
Definition spdlog-inl.h:15
SPDLOG_INLINE std::shared_ptr< logger > get(const std::string &name)
Definition spdlog-inl.h:20
SPDLOG_INLINE void set_level(level::level_enum log_level)
Definition spdlog-inl.h:60
SPDLOG_INLINE void enable_backtrace(size_t n_messages)
Definition spdlog-inl.h:35
SPDLOG_INLINE void shutdown()
Definition spdlog-inl.h:100
SPDLOG_INLINE void set_error_handler(void(*handler)(const std::string &msg))
Definition spdlog-inl.h:75
SPDLOG_INLINE void set_automatic_registration(bool automatic_registration)
Definition spdlog-inl.h:105
SPDLOG_INLINE std::shared_ptr< spdlog::logger > default_logger()
Definition spdlog-inl.h:110
SPDLOG_INLINE void drop(const std::string &name)
Definition spdlog-inl.h:90
SPDLOG_INLINE void flush_on(level::level_enum log_level)
Definition spdlog-inl.h:65
SPDLOG_INLINE void set_formatter(std::unique_ptr< spdlog::formatter > formatter)
Definition spdlog-inl.h:25
SPDLOG_INLINE void apply_all(const std::function< void(std::shared_ptr< logger >)> &fun)
Definition spdlog-inl.h:85
SPDLOG_INLINE void set_default_logger(std::shared_ptr< spdlog::logger > default_logger)
Definition spdlog-inl.h:120
SPDLOG_INLINE void drop_all()
Definition spdlog-inl.h:95