source: trunk/GDE/SINA/builddir/include/spdlog/spdlog.h

Last change on this file was 19170, checked in by westram, 2 years ago
  • sina source
    • unpack + remove tarball
    • no longer ignore sina builddir.
File size: 10.6 KB
Line 
1//
2// Copyright(c) 2015-2018 Gabi Melman.
3// Distributed under the MIT License (http://opensource.org/licenses/MIT)
4//
5// spdlog main header file.
6// see example.cpp for usage example
7
8#ifndef SPDLOG_H
9#define SPDLOG_H
10#pragma once
11
12#include "spdlog/common.h"
13#include "spdlog/details/registry.h"
14#include "spdlog/logger.h"
15#include "spdlog/version.h"
16
17#include <chrono>
18#include <functional>
19#include <memory>
20#include <string>
21
22namespace spdlog {
23
24// Default logger factory-  creates synchronous loggers
25struct synchronous_factory
26{
27    template<typename Sink, typename... SinkArgs>
28    static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... args)
29    {
30        auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
31        auto new_logger = std::make_shared<logger>(std::move(logger_name), std::move(sink));
32        details::registry::instance().initialize_logger(new_logger);
33        return new_logger;
34    }
35};
36
37using default_factory = synchronous_factory;
38
39// Create and register a logger with a templated sink type
40// The logger's level, formatter and flush level will be set according the
41// global settings.
42// Example:
43// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
44template<typename Sink, typename... SinkArgs>
45inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... sink_args)
46{
47    return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
48}
49
50// Return an existing logger or nullptr if a logger with such name doesn't
51// exist.
52// example: spdlog::get("my_logger")->info("hello {}", "world");
53inline std::shared_ptr<logger> get(const std::string &name)
54{
55    return details::registry::instance().get(name);
56}
57
58// Set global formatter. Each sink in each logger will get a clone of this object
59inline void set_formatter(std::unique_ptr<spdlog::formatter> formatter)
60{
61    details::registry::instance().set_formatter(std::move(formatter));
62}
63
64// Set global format string.
65// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
66inline void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local)
67{
68    set_formatter(std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type)));
69}
70
71// Set global logging level
72inline void set_level(level::level_enum log_level)
73{
74    details::registry::instance().set_level(log_level);
75}
76
77// Set global flush level
78inline void flush_on(level::level_enum log_level)
79{
80    details::registry::instance().flush_on(log_level);
81}
82
83// Start/Restart a periodic flusher thread
84// Warning: Use only if all your loggers are thread safe!
85inline void flush_every(std::chrono::seconds interval)
86{
87    details::registry::instance().flush_every(interval);
88}
89
90// Set global error handler
91inline void set_error_handler(log_err_handler handler)
92{
93    details::registry::instance().set_error_handler(std::move(handler));
94}
95
96// Register the given logger with the given name
97inline void register_logger(std::shared_ptr<logger> logger)
98{
99    details::registry::instance().register_logger(std::move(logger));
100}
101
102// Apply a user defined function on all registered loggers
103// Example:
104// spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
105inline void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun)
106{
107    details::registry::instance().apply_all(fun);
108}
109
110// Drop the reference to the given logger
111inline void drop(const std::string &name)
112{
113    details::registry::instance().drop(name);
114}
115
116// Drop all references from the registry
117inline void drop_all()
118{
119    details::registry::instance().drop_all();
120}
121
122// stop any running threads started by spdlog and clean registry loggers
123inline void shutdown()
124{
125    details::registry::instance().shutdown();
126}
127
128// Automatic registration of loggers when using spdlog::create() or spdlog::create_async
129inline void set_automatic_registration(bool automatic_registation)
130{
131    details::registry::instance().set_automatic_registration(automatic_registation);
132}
133
134// API for using default logger (stdout_color_mt),
135// e.g: spdlog::info("Message {}", 1);
136//
137// The default logger object can be accessed using the spdlog::default_logger():
138// For example, to add another sink to it:
139// spdlog::default_logger()->sinks()->push_back(some_sink);
140//
141// The default logger can replaced using spdlog::set_default_logger(new_logger).
142// For example, to replace it with a file logger.
143//
144// IMPORTANT:
145// The default API is thread safe (for _mt loggers), but:
146// set_default_logger() *should not* be used concurrently with the default API.
147// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
148
149inline std::shared_ptr<spdlog::logger> default_logger()
150{
151    return details::registry::instance().default_logger();
152}
153
154inline spdlog::logger *default_logger_raw()
155{
156    return details::registry::instance().get_default_raw();
157}
158
159inline void set_default_logger(std::shared_ptr<spdlog::logger> default_logger)
160{
161    details::registry::instance().set_default_logger(std::move(default_logger));
162}
163
164template<typename... Args>
165inline void log(source_loc source, level::level_enum lvl, const char *fmt, const Args &... args)
166{
167    default_logger_raw()->log(source, lvl, fmt, args...);
168}
169
170template<typename... Args>
171inline void log(level::level_enum lvl, const char *fmt, const Args &... args)
172{
173    default_logger_raw()->log(source_loc{}, lvl, fmt, args...);
174}
175
176template<typename... Args>
177inline void trace(const char *fmt, const Args &... args)
178{
179    default_logger_raw()->trace(fmt, args...);
180}
181
182template<typename... Args>
183inline void debug(const char *fmt, const Args &... args)
184{
185    default_logger_raw()->debug(fmt, args...);
186}
187
188template<typename... Args>
189inline void info(const char *fmt, const Args &... args)
190{
191    default_logger_raw()->info(fmt, args...);
192}
193
194template<typename... Args>
195inline void warn(const char *fmt, const Args &... args)
196{
197    default_logger_raw()->warn(fmt, args...);
198}
199
200template<typename... Args>
201inline void error(const char *fmt, const Args &... args)
202{
203    default_logger_raw()->error(fmt, args...);
204}
205
206template<typename... Args>
207inline void critical(const char *fmt, const Args &... args)
208{
209    default_logger_raw()->critical(fmt, args...);
210}
211
212template<typename T>
213inline void log(level::level_enum lvl, const T &msg)
214{
215    default_logger_raw()->log(lvl, msg);
216}
217
218template<typename T>
219inline void trace(const T &msg)
220{
221    default_logger_raw()->trace(msg);
222}
223
224template<typename T>
225inline void debug(const T &msg)
226{
227    default_logger_raw()->debug(msg);
228}
229
230template<typename T>
231inline void info(const T &msg)
232{
233    default_logger_raw()->info(msg);
234}
235
236template<typename T>
237inline void warn(const T &msg)
238{
239    default_logger_raw()->warn(msg);
240}
241
242template<typename T>
243inline void error(const T &msg)
244{
245    default_logger_raw()->error(msg);
246}
247
248template<typename T>
249inline void critical(const T &msg)
250{
251    default_logger_raw()->critical(msg);
252}
253
254#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
255template<typename... Args>
256inline void log(level::level_enum lvl, const wchar_t *fmt, const Args &... args)
257{
258    default_logger_raw()->log(lvl, fmt, args...);
259}
260
261template<typename... Args>
262inline void trace(const wchar_t *fmt, const Args &... args)
263{
264    default_logger_raw()->trace(fmt, args...);
265}
266
267template<typename... Args>
268inline void debug(const wchar_t *fmt, const Args &... args)
269{
270    default_logger_raw()->debug(fmt, args...);
271}
272
273template<typename... Args>
274inline void info(const wchar_t *fmt, const Args &... args)
275{
276    default_logger_raw()->info(fmt, args...);
277}
278
279template<typename... Args>
280inline void warn(const wchar_t *fmt, const Args &... args)
281{
282    default_logger_raw()->warn(fmt, args...);
283}
284
285template<typename... Args>
286inline void error(const wchar_t *fmt, const Args &... args)
287{
288    default_logger_raw()->error(fmt, args...);
289}
290
291template<typename... Args>
292inline void critical(const wchar_t *fmt, const Args &... args)
293{
294    default_logger_raw()->critical(fmt, args...);
295}
296
297#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
298
299} // namespace spdlog
300
301//
302// enable/disable log calls at compile time according to global level.
303//
304// define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h):
305// SPDLOG_LEVEL_TRACE,
306// SPDLOG_LEVEL_DEBUG,
307// SPDLOG_LEVEL_INFO,
308// SPDLOG_LEVEL_WARN,
309// SPDLOG_LEVEL_ERROR,
310// SPDLOG_LEVEL_CRITICAL,
311// SPDLOG_LEVEL_OFF
312//
313
314#define SPDLOG_LOGGER_CALL(logger, level, ...)                                                                                             \
315    if (logger->should_log(level))                                                                                                         \
316    logger->log(spdlog::source_loc{SPDLOG_FILE_BASENAME(__FILE__), __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
317
318#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
319#define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)
320#define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__)
321#else
322#define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
323#define SPDLOG_TRACE(...) (void)0
324#endif
325
326#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
327#define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
328#define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__)
329#else
330#define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
331#define SPDLOG_DEBUG(...) (void)0
332#endif
333
334#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
335#define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
336#define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
337#else
338#define SPDLOG_LOGGER_INFO(logger, ...) (void)0
339#define SPDLOG_INFO(...) (void)0
340#endif
341
342#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
343#define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
344#define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__)
345#else
346#define SPDLOG_LOGGER_WARN(logger, ...) (void)0
347#define SPDLOG_WARN(...) (void)0
348#endif
349
350#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
351#define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
352#define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__)
353#else
354#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
355#define SPDLOG_ERROR(...) (void)0
356#endif
357
358#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
359#define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
360#define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__)
361#else
362#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
363#define SPDLOG_CRITICAL(...) (void)0
364#endif
365
366#endif // SPDLOG_H
Note: See TracBrowser for help on using the repository browser.