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