spdlog
Loading...
Searching...
No Matches
spdlog.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// spdlog main header file.
5// see example.cpp for usage example
6
7#ifndef SPDLOG_H
8#define SPDLOG_H
9
10#pragma once
11
12#include <spdlog/common.h>
14#include <spdlog/logger.h>
15#include <spdlog/version.h>
17
18#include <chrono>
19#include <functional>
20#include <memory>
21#include <string>
22
23namespace spdlog {
24
26
27// Create and register a logger with a templated sink type
28// The logger's level, formatter and flush level will be set according the
29// global settings.
30//
31// Example:
32// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
33template<typename Sink, typename... SinkArgs>
34inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...sink_args)
35{
36 return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
37}
38
39// Initialize and register a logger,
40// formatter and flush level will be set according the global settings.
41//
42// Useful for initializing manually created loggers with the global settings.
43//
44// Example:
45// auto mylogger = std::make_shared<spdlog::logger>("mylogger", ...);
46// spdlog::initialize_logger(mylogger);
48
49// Return an existing logger or nullptr if a logger with such name doesn't
50// exist.
51// example: spdlog::get("my_logger")->info("hello {}", "world");
53
54// Set global formatter. Each sink in each logger will get a clone of this object
56
57// Set global format string.
58// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
60
61// enable global backtrace support
62SPDLOG_API void enable_backtrace(size_t n_messages);
63
64// disable global backtrace support
66
67// call dump backtrace on default logger
69
70// Get global logging level
72
73// Set global logging level
75
76// Determine whether the default logger should log messages with a certain level
78
79// Set global flush level
81
82// Start/Restart a periodic flusher thread
83// Warning: Use only if all your loggers are thread safe!
85
86// Set global error handler
87SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg));
88
89// Register the given logger with the given name
91
92// Apply a user defined function on all registered loggers
93// Example:
94// spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
96
97// Drop the reference to the given logger
98SPDLOG_API void drop(const std::string &name);
99
100// Drop all references from the registry
101SPDLOG_API void drop_all();
102
103// stop any running threads started by spdlog and clean registry loggers
104SPDLOG_API void shutdown();
105
106// Automatic registration of loggers when using spdlog::create() or spdlog::create_async
107SPDLOG_API void set_automatic_registration(bool automatic_registration);
108
109// API for using default logger (stdout_color_mt),
110// e.g: spdlog::info("Message {}", 1);
111//
112// The default logger object can be accessed using the spdlog::default_logger():
113// For example, to add another sink to it:
114// spdlog::default_logger()->sinks().push_back(some_sink);
115//
116// The default logger can replaced using spdlog::set_default_logger(new_logger).
117// For example, to replace it with a file logger.
118//
119// IMPORTANT:
120// The default API is thread safe (for _mt loggers), but:
121// set_default_logger() *should not* be used concurrently with the default API.
122// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
123
125
127
129
130template<typename... Args>
131inline void log(source_loc source, level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
132{
133 default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
134}
135
136template<typename... Args>
137inline void log(level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
138{
140}
141
142template<typename... Args>
143inline void trace(fmt::format_string<Args...> fmt, Args &&...args)
144{
146}
147
148template<typename... Args>
149inline void debug(fmt::format_string<Args...> fmt, Args &&...args)
150{
152}
153
154template<typename... Args>
155inline void info(fmt::format_string<Args...> fmt, Args &&...args)
156{
158}
159
160template<typename... Args>
161inline void warn(fmt::format_string<Args...> fmt, Args &&...args)
162{
164}
165
166template<typename... Args>
167inline void error(fmt::format_string<Args...> fmt, Args &&...args)
168{
170}
171
172template<typename... Args>
173inline void critical(fmt::format_string<Args...> fmt, Args &&...args)
174{
176}
177
178template<typename T>
179inline void log(source_loc source, level::level_enum lvl, const T &msg)
180{
181 default_logger_raw()->log(source, lvl, msg);
182}
183
184template<typename T>
185inline void log(level::level_enum lvl, const T &msg)
186{
187 default_logger_raw()->log(lvl, msg);
188}
189
190#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
191template<typename... Args>
192inline void log(source_loc source, level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
193{
194 default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
195}
196
197template<typename... Args>
198inline void log(level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
199{
200 default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
201}
202
203template<typename... Args>
204inline void trace(fmt::wformat_string<Args...> fmt, Args &&...args)
205{
207}
208
209template<typename... Args>
210inline void debug(fmt::wformat_string<Args...> fmt, Args &&...args)
211{
213}
214
215template<typename... Args>
216inline void info(fmt::wformat_string<Args...> fmt, Args &&...args)
217{
219}
220
221template<typename... Args>
222inline void warn(fmt::wformat_string<Args...> fmt, Args &&...args)
223{
225}
226
227template<typename... Args>
228inline void error(fmt::wformat_string<Args...> fmt, Args &&...args)
229{
231}
232
233template<typename... Args>
234inline void critical(fmt::wformat_string<Args...> fmt, Args &&...args)
235{
237}
238#endif
239
240template<typename T>
241inline void trace(const T &msg)
242{
244}
245
246template<typename T>
247inline void debug(const T &msg)
248{
250}
251
252template<typename T>
253inline void info(const T &msg)
254{
255 default_logger_raw()->info(msg);
256}
257
258template<typename T>
259inline void warn(const T &msg)
260{
261 default_logger_raw()->warn(msg);
262}
263
264template<typename T>
265inline void error(const T &msg)
266{
268}
269
270template<typename T>
271inline void critical(const T &msg)
272{
274}
275
276} // namespace spdlog
277
278//
279// enable/disable log calls at compile time according to global level.
280//
281// define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h):
282// SPDLOG_LEVEL_TRACE,
283// SPDLOG_LEVEL_DEBUG,
284// SPDLOG_LEVEL_INFO,
285// SPDLOG_LEVEL_WARN,
286// SPDLOG_LEVEL_ERROR,
287// SPDLOG_LEVEL_CRITICAL,
288// SPDLOG_LEVEL_OFF
289//
290
291#define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
292
293#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
294# define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)
295# define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__)
296#else
297# define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
298# define SPDLOG_TRACE(...) (void)0
299#endif
300
301#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
302# define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
303# define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__)
304#else
305# define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
306# define SPDLOG_DEBUG(...) (void)0
307#endif
308
309#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
310# define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
311# define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
312#else
313# define SPDLOG_LOGGER_INFO(logger, ...) (void)0
314# define SPDLOG_INFO(...) (void)0
315#endif
316
317#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
318# define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
319# define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__)
320#else
321# define SPDLOG_LOGGER_WARN(logger, ...) (void)0
322# define SPDLOG_WARN(...) (void)0
323#endif
324
325#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
326# define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
327# define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__)
328#else
329# define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
330# define SPDLOG_ERROR(...) (void)0
331#endif
332
333#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
334# define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
335# define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__)
336#else
337# define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
338# define SPDLOG_CRITICAL(...) (void)0
339#endif
340
341#ifdef SPDLOG_HEADER_ONLY
342# include "spdlog-inl.h"
343#endif
344
345#endif // SPDLOG_H
void info(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.h:156
void error(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.h:168
void warn(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.h:162
void trace(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.h:144
void debug(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.h:150
void log(source_loc loc, level::level_enum lvl, fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.h:81
void critical(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.h:174
#define SPDLOG_API
Definition common.h:31
T
Definition core.h:320
T make_shared(T... args)
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
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
void critical(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:173
SPDLOG_INLINE std::shared_ptr< logger > get(const std::string &name)
Definition spdlog-inl.h:20
void warn(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:161
void error(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:167
SPDLOG_INLINE void set_level(level::level_enum log_level)
Definition spdlog-inl.h:60
std::shared_ptr< spdlog::logger > create(std::string logger_name, SinkArgs &&...sink_args)
Definition spdlog.h:34
SPDLOG_INLINE spdlog::logger * default_logger_raw()
Definition spdlog-inl.h:115
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 dump_backtrace()
Definition spdlog-inl.h:45
SPDLOG_INLINE void set_automatic_registration(bool automatic_registration)
Definition spdlog-inl.h:105
void trace(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:143
SPDLOG_INLINE std::shared_ptr< spdlog::logger > default_logger()
Definition spdlog-inl.h:110
void info(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:155
void log(source_loc source, level::level_enum lvl, fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:131
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 level::level_enum get_level()
Definition spdlog-inl.h:50
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
pattern_time_type
Definition common.h:218
SPDLOG_INLINE bool should_log(level::level_enum log_level)
Definition spdlog-inl.h:55
SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type)
Definition spdlog-inl.h:30
void debug(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:149
SPDLOG_INLINE void drop_all()
Definition spdlog-inl.h:95