1 //
2 // Copyright(c) 2015 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 //
9 // Global registry functions
10 //
11 #include "spdlog/spdlog.h"
12 #include "spdlog/details/registry.h"
13 #include "spdlog/sinks/file_sinks.h"
14 #include "spdlog/sinks/stdout_sinks.h"
15 #ifdef SPDLOG_ENABLE_SYSLOG
16 #include "spdlog/sinks/syslog_sink.h"
17 #endif
18 
19 #ifdef _WIN32
20 #include "spdlog/sinks/wincolor_sink.h"
21 #else
22 #include "spdlog/sinks/ansicolor_sink.h"
23 #endif
24 
25 
26 #ifdef __ANDROID__
27 #include "spdlog/sinks/android_sink.h"
28 #endif
29 
30 #include <chrono>
31 #include <functional>
32 #include <memory>
33 #include <string>
34 
register_logger(std::shared_ptr<logger> logger)35 inline void spdlog::register_logger(std::shared_ptr<logger> logger)
36 {
37     return details::registry::instance().register_logger(logger);
38 }
39 
get(const std::string& name)40 inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
41 {
42     return details::registry::instance().get(name);
43 }
44 
drop(const std::string &name)45 inline void spdlog::drop(const std::string &name)
46 {
47     details::registry::instance().drop(name);
48 }
49 
50 // Create multi/single threaded simple file logger
basic_logger_mt(const std::string& logger_name, const filename_t& filename, bool truncate)51 inline std::shared_ptr<spdlog::logger> spdlog::basic_logger_mt(const std::string& logger_name, const filename_t& filename, bool truncate)
52 {
53     return create<spdlog::sinks::simple_file_sink_mt>(logger_name, filename, truncate);
54 }
55 
basic_logger_st(const std::string& logger_name, const filename_t& filename, bool truncate)56 inline std::shared_ptr<spdlog::logger> spdlog::basic_logger_st(const std::string& logger_name, const filename_t& filename, bool truncate)
57 {
58     return create<spdlog::sinks::simple_file_sink_st>(logger_name, filename, truncate);
59 }
60 
61 // Create multi/single threaded rotating file logger
rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files)62 inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files)
63 {
64     return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files);
65 }
66 
rotating_logger_st(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files)67 inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files)
68 {
69     return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files);
70 }
71 
72 // Create file logger which creates new file at midnight):
daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour, int minute)73 inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour, int minute)
74 {
75     return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, hour, minute);
76 }
77 
daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour, int minute)78 inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour, int minute)
79 {
80     return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, hour, minute);
81 }
82 
83 
84 //
85 // stdout/stderr loggers
86 //
stdout_logger_mt(const std::string& logger_name)87 inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name)
88 {
89     return spdlog::details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_mt::instance());
90 }
91 
stdout_logger_st(const std::string& logger_name)92 inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name)
93 {
94     return spdlog::details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_st::instance());
95 }
96 
stderr_logger_mt(const std::string& logger_name)97 inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name)
98 {
99     return spdlog::details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_mt::instance());
100 }
101 
stderr_logger_st(const std::string& logger_name)102 inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name)
103 {
104     return spdlog::details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_st::instance());
105 }
106 
107 //
108 // stdout/stderr color loggers
109 //
110 #ifdef _WIN32
stdout_color_mt(const std::string& logger_name)111 inline std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt(const std::string& logger_name)
112 {
113     auto sink = std::make_shared<spdlog::sinks::wincolor_stdout_sink_mt>();
114     return spdlog::details::registry::instance().create(logger_name, sink);
115 }
116 
stdout_color_st(const std::string& logger_name)117 inline std::shared_ptr<spdlog::logger> spdlog::stdout_color_st(const std::string& logger_name)
118 {
119     auto sink = std::make_shared<spdlog::sinks::wincolor_stdout_sink_st>();
120     return spdlog::details::registry::instance().create(logger_name, sink);
121 }
122 
stderr_color_mt(const std::string& logger_name)123 inline std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt(const std::string& logger_name)
124 {
125     auto sink = std::make_shared<spdlog::sinks::wincolor_stderr_sink_mt>();
126     return spdlog::details::registry::instance().create(logger_name, sink);
127 }
128 
129 
stderr_color_st(const std::string& logger_name)130 inline std::shared_ptr<spdlog::logger> spdlog::stderr_color_st(const std::string& logger_name)
131 {
132     auto sink = std::make_shared<spdlog::sinks::wincolor_stderr_sink_st>();
133     return spdlog::details::registry::instance().create(logger_name, sink);
134 }
135 
136 #else //ansi terminal colors
137 
stdout_color_mt(const std::string& logger_name)138 inline std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt(const std::string& logger_name)
139 {
140     auto sink = std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>();
141     return spdlog::details::registry::instance().create(logger_name, sink);
142 }
143 
stdout_color_st(const std::string& logger_name)144 inline std::shared_ptr<spdlog::logger> spdlog::stdout_color_st(const std::string& logger_name)
145 {
146     auto sink = std::make_shared<spdlog::sinks::ansicolor_stdout_sink_st>();
147     return spdlog::details::registry::instance().create(logger_name, sink);
148 }
149 
stderr_color_mt(const std::string& logger_name)150 inline std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt(const std::string& logger_name)
151 {
152     auto sink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
153     return spdlog::details::registry::instance().create(logger_name, sink);
154 }
155 
stderr_color_st(const std::string& logger_name)156 inline std::shared_ptr<spdlog::logger> spdlog::stderr_color_st(const std::string& logger_name)
157 {
158     auto sink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_st>();
159     return spdlog::details::registry::instance().create(logger_name, sink);
160 }
161 #endif
162 
163 #ifdef SPDLOG_ENABLE_SYSLOG
164 // Create syslog logger
syslog_logger(const std::string& logger_name, const std::string& syslog_ident, int syslog_option)165 inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string& logger_name, const std::string& syslog_ident, int syslog_option)
166 {
167     return create<spdlog::sinks::syslog_sink>(logger_name, syslog_ident, syslog_option);
168 }
169 #endif
170 
171 #ifdef __ANDROID__
android_logger(const std::string& logger_name, const std::string& tag)172 inline std::shared_ptr<spdlog::logger> spdlog::android_logger(const std::string& logger_name, const std::string& tag)
173 {
174     return create<spdlog::sinks::android_sink>(logger_name, tag);
175 }
176 #endif
177 
178 // Create and register a logger a single sink
create(const std::string& logger_name, const spdlog::sink_ptr& sink)179 inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const spdlog::sink_ptr& sink)
180 {
181     return details::registry::instance().create(logger_name, sink);
182 }
183 
184 //Create logger with multiple sinks
185 
create(const std::string& logger_name, spdlog::sinks_init_list sinks)186 inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks)
187 {
188     return details::registry::instance().create(logger_name, sinks);
189 }
190 
191 
192 template <typename Sink, typename... Args>
create(const std::string& logger_name, Args... args)193 inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, Args... args)
194 {
195     sink_ptr sink = std::make_shared<Sink>(args...);
196     return details::registry::instance().create(logger_name, { sink });
197 }
198 
199 
200 template<class It>
create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)201 inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
202 {
203     return details::registry::instance().create(logger_name, sinks_begin, sinks_end);
204 }
205 
206 // Create and register an async logger with a single sink
create_async(const std::string& logger_name, const sink_ptr& sink, size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)207 inline std::shared_ptr<spdlog::logger> spdlog::create_async(const std::string& logger_name, const sink_ptr& sink, size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)
208 {
209     return details::registry::instance().create_async(logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, sink);
210 }
211 
212 // Create and register an async logger with multiple sinks
create_async(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb )213 inline std::shared_ptr<spdlog::logger> spdlog::create_async(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb )
214 {
215     return details::registry::instance().create_async(logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, sinks);
216 }
217 
218 template<class It>
create_async(const std::string& logger_name, const It& sinks_begin, const It& sinks_end, size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)219 inline std::shared_ptr<spdlog::logger> spdlog::create_async(const std::string& logger_name, const It& sinks_begin, const It& sinks_end, size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)
220 {
221     return details::registry::instance().create_async(logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, sinks_begin, sinks_end);
222 }
223 
set_formatter(spdlog::formatter_ptr f)224 inline void spdlog::set_formatter(spdlog::formatter_ptr f)
225 {
226     details::registry::instance().formatter(f);
227 }
228 
set_pattern(const std::string& format_string)229 inline void spdlog::set_pattern(const std::string& format_string)
230 {
231     return details::registry::instance().set_pattern(format_string);
232 }
233 
set_level(level::level_enum log_level)234 inline void spdlog::set_level(level::level_enum log_level)
235 {
236     return details::registry::instance().set_level(log_level);
237 }
238 
set_error_handler(log_err_handler handler)239 inline void spdlog::set_error_handler(log_err_handler handler)
240 {
241     return details::registry::instance().set_error_handler(handler);
242 }
243 
244 
set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)245 inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)
246 {
247     details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb);
248 }
249 
set_sync_mode()250 inline void spdlog::set_sync_mode()
251 {
252     details::registry::instance().set_sync_mode();
253 }
254 
apply_all(std::function<void(std::shared_ptr<logger>)> fun)255 inline void spdlog::apply_all(std::function<void(std::shared_ptr<logger>)> fun)
256 {
257     details::registry::instance().apply_all(fun);
258 }
259 
drop_all()260 inline void spdlog::drop_all()
261 {
262     details::registry::instance().drop_all();
263 }
264