spdlog/include/spdlog/spdlog.h

296 lines
9.2 KiB
C
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2016-04-20 16:57:49 +08:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2019-05-12 01:06:17 +08:00
2016-04-20 16:57:49 +08:00
// spdlog main header file.
// see example.cpp for usage example
#ifndef SPDLOG_H
#define SPDLOG_H
2019-05-12 01:06:17 +08:00
2016-04-20 16:57:49 +08:00
#pragma once
#include <spdlog/common.h>
#include <spdlog/details/registry.h>
#include <spdlog/logger.h>
#include <spdlog/version.h>
#include <spdlog/details/synchronous_factory.h>
2016-04-20 16:57:49 +08:00
#include <chrono>
2018-03-09 21:26:33 +08:00
#include <functional>
#include <memory>
2016-04-20 16:57:49 +08:00
#include <string>
2018-03-09 21:26:33 +08:00
namespace spdlog {
2016-05-15 06:49:15 +08:00
using default_factory = synchronous_factory;
2018-04-29 05:42:24 +08:00
2018-04-14 08:34:57 +08:00
// Create and register a logger with a templated sink type
2018-07-22 04:48:07 +08:00
// The logger's level, formatter and flush level will be set according the
// global settings.
2019-06-19 22:13:48 +08:00
//
2018-04-14 08:34:57 +08:00
// Example:
2019-06-19 22:13:48 +08:00
// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
2018-04-14 08:34:57 +08:00
template<typename Sink, typename... SinkArgs>
2020-09-26 20:34:05 +08:00
inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...sink_args)
2018-04-14 08:34:57 +08:00
{
return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
2018-04-14 08:34:57 +08:00
}
2019-03-25 11:46:35 +08:00
// Initialize and register a logger,
// formatter and flush level will be set according the global settings.
//
2020-02-26 18:52:46 +08:00
// Useful for initializing manually created loggers with the global settings.
2019-03-25 11:46:35 +08:00
//
// Example:
2020-02-26 18:54:31 +08:00
// auto mylogger = std::make_shared<spdlog::logger>("mylogger", ...);
// spdlog::initialize_logger(mylogger);
2020-03-10 03:02:16 +08:00
SPDLOG_API void initialize_logger(std::shared_ptr<logger> logger);
2019-03-25 11:46:35 +08:00
2018-07-22 04:48:07 +08:00
// Return an existing logger or nullptr if a logger with such name doesn't
// exist.
2016-09-03 19:08:55 +08:00
// example: spdlog::get("my_logger")->info("hello {}", "world");
2020-03-10 03:02:16 +08:00
SPDLOG_API std::shared_ptr<logger> get(const std::string &name);
2016-09-03 19:08:55 +08:00
2018-07-23 02:52:46 +08:00
// Set global formatter. Each sink in each logger will get a clone of this object
2020-03-10 03:02:16 +08:00
SPDLOG_API void set_formatter(std::unique_ptr<spdlog::formatter> formatter);
2018-07-23 02:52:46 +08:00
2018-07-23 05:13:52 +08:00
// Set global format string.
2016-04-20 16:57:49 +08:00
// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
2020-03-10 03:02:16 +08:00
SPDLOG_API void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
2018-04-14 08:34:57 +08:00
2019-08-23 07:28:52 +08:00
// enable global backtrace support
2020-03-10 03:02:16 +08:00
SPDLOG_API void enable_backtrace(size_t n_messages);
2019-08-23 07:28:52 +08:00
2019-08-25 22:24:17 +08:00
// disable global backtrace support
2020-03-10 03:02:16 +08:00
SPDLOG_API void disable_backtrace();
2019-08-25 22:24:17 +08:00
// call dump backtrace on default logger
2020-03-10 03:02:16 +08:00
SPDLOG_API void dump_backtrace();
2019-08-25 22:24:17 +08:00
// Get global logging level
SPDLOG_API level::level_enum get_level();
2018-01-03 23:19:56 +08:00
// Set global logging level
2020-03-10 03:02:16 +08:00
SPDLOG_API void set_level(level::level_enum log_level);
2018-01-03 23:19:56 +08:00
// Determine whether the default logger should log messages with a certain level
SPDLOG_API bool should_log(level::level_enum lvl);
2018-01-03 23:19:56 +08:00
// Set global flush level
2020-03-10 03:02:16 +08:00
SPDLOG_API void flush_on(level::level_enum log_level);
2016-04-20 16:57:49 +08:00
// Start/Restart a periodic flusher thread
// Warning: Use only if all your loggers are thread safe!
2020-03-10 03:02:16 +08:00
SPDLOG_API void flush_every(std::chrono::seconds interval);
2016-08-05 08:56:40 +08:00
// Set global error handler
2020-03-10 03:02:16 +08:00
SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg));
2016-08-05 08:56:40 +08:00
2018-04-14 08:34:57 +08:00
// Register the given logger with the given name
2020-03-10 03:02:16 +08:00
SPDLOG_API void register_logger(std::shared_ptr<logger> logger);
2018-04-14 08:34:57 +08:00
// Apply a user defined function on all registered loggers
// Example:
// spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
2020-03-10 03:02:16 +08:00
SPDLOG_API void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun);
2018-04-14 08:34:57 +08:00
// Drop the reference to the given logger
2020-03-10 03:02:16 +08:00
SPDLOG_API void drop(const std::string &name);
2016-04-20 16:57:49 +08:00
2018-04-14 08:34:57 +08:00
// Drop all references from the registry
2020-03-10 03:02:16 +08:00
SPDLOG_API void drop_all();
2016-04-20 16:57:49 +08:00
2018-07-25 03:59:34 +08:00
// stop any running threads started by spdlog and clean registry loggers
2020-03-10 03:02:16 +08:00
SPDLOG_API void shutdown();
2018-07-25 03:59:34 +08:00
// Automatic registration of loggers when using spdlog::create() or spdlog::create_async
2020-03-10 03:02:16 +08:00
SPDLOG_API void set_automatic_registration(bool automatic_registration);
2018-10-12 08:04:55 +08:00
// API for using default logger (stdout_color_mt),
// e.g: spdlog::info("Message {}", 1);
//
2018-10-14 00:23:11 +08:00
// The default logger object can be accessed using the spdlog::default_logger():
2018-10-12 08:04:55 +08:00
// For example, to add another sink to it:
2020-10-20 08:49:09 +08:00
// spdlog::default_logger()->sinks().push_back(some_sink);
2018-10-12 08:04:55 +08:00
//
// The default logger can replaced using spdlog::set_default_logger(new_logger).
2018-10-14 00:23:11 +08:00
// For example, to replace it with a file logger.
2018-10-12 08:04:55 +08:00
//
2018-10-14 00:23:11 +08:00
// IMPORTANT:
// The default API is thread safe (for _mt loggers), but:
// set_default_logger() *should not* be used concurrently with the default API.
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
2018-10-12 08:04:55 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API std::shared_ptr<spdlog::logger> default_logger();
2018-10-12 08:04:55 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API spdlog::logger *default_logger_raw();
2018-10-12 08:04:55 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);
2018-10-12 08:04:55 +08:00
template<typename FormatString, typename... Args>
2020-09-26 20:34:05 +08:00
inline void log(source_loc source, level::level_enum lvl, const FormatString &fmt, const Args &...args)
2018-11-23 00:47:50 +08:00
{
default_logger_raw()->log(source, lvl, fmt, args...);
2018-11-23 00:47:50 +08:00
}
template<typename FormatString, typename... Args>
2020-09-26 20:34:05 +08:00
inline void log(level::level_enum lvl, const FormatString &fmt, const Args &...args)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->log(source_loc{}, lvl, fmt, args...);
2018-10-12 08:04:55 +08:00
}
template<typename FormatString, typename... Args>
2020-09-26 20:34:05 +08:00
inline void trace(const FormatString &fmt, const Args &...args)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->trace(fmt, args...);
2018-10-12 08:04:55 +08:00
}
template<typename FormatString, typename... Args>
2020-09-26 20:34:05 +08:00
inline void debug(const FormatString &fmt, const Args &...args)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->debug(fmt, args...);
2018-10-12 08:04:55 +08:00
}
template<typename FormatString, typename... Args>
2020-09-26 20:34:05 +08:00
inline void info(const FormatString &fmt, const Args &...args)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->info(fmt, args...);
2018-10-12 08:04:55 +08:00
}
template<typename FormatString, typename... Args>
2020-09-26 20:34:05 +08:00
inline void warn(const FormatString &fmt, const Args &...args)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->warn(fmt, args...);
2018-10-12 08:04:55 +08:00
}
template<typename FormatString, typename... Args>
2020-09-26 20:34:05 +08:00
inline void error(const FormatString &fmt, const Args &...args)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->error(fmt, args...);
2018-10-12 08:04:55 +08:00
}
template<typename FormatString, typename... Args>
2020-09-26 20:34:05 +08:00
inline void critical(const FormatString &fmt, const Args &...args)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->critical(fmt, args...);
2018-10-12 08:04:55 +08:00
}
template<typename T>
inline void log(source_loc source, level::level_enum lvl, const T &msg)
{
default_logger_raw()->log(source, lvl, msg);
}
2018-10-12 08:04:55 +08:00
template<typename T>
inline void log(level::level_enum lvl, const T &msg)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->log(lvl, msg);
2018-10-12 08:04:55 +08:00
}
template<typename T>
inline void trace(const T &msg)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->trace(msg);
2018-10-12 08:04:55 +08:00
}
template<typename T>
inline void debug(const T &msg)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->debug(msg);
2018-10-12 08:04:55 +08:00
}
template<typename T>
inline void info(const T &msg)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->info(msg);
2018-10-12 08:04:55 +08:00
}
template<typename T>
inline void warn(const T &msg)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->warn(msg);
2018-10-12 08:04:55 +08:00
}
template<typename T>
inline void error(const T &msg)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->error(msg);
2018-10-12 08:04:55 +08:00
}
template<typename T>
inline void critical(const T &msg)
2018-10-12 08:04:55 +08:00
{
default_logger_raw()->critical(msg);
2018-10-12 08:04:55 +08:00
}
} // namespace spdlog
2018-11-12 18:01:10 +08:00
//
2018-11-21 20:21:26 +08:00
// enable/disable log calls at compile time according to global level.
2018-11-12 18:01:10 +08:00
//
2018-11-21 20:21:26 +08:00
// define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h):
// SPDLOG_LEVEL_TRACE,
// SPDLOG_LEVEL_DEBUG,
// SPDLOG_LEVEL_INFO,
// SPDLOG_LEVEL_WARN,
// SPDLOG_LEVEL_ERROR,
// SPDLOG_LEVEL_CRITICAL,
// SPDLOG_LEVEL_OFF
//
2019-11-11 18:41:58 +08:00
#define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
2019-01-10 23:04:27 +08:00
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
2019-01-10 23:04:27 +08:00
#define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)
#define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__)
#else
#define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
#define SPDLOG_TRACE(...) (void)0
#endif
2016-04-20 16:57:49 +08:00
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
2019-01-10 23:04:27 +08:00
#define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
#define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__)
2018-03-09 21:26:33 +08:00
#else
#define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
#define SPDLOG_DEBUG(...) (void)0
2018-03-09 21:26:33 +08:00
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
2019-01-10 23:04:27 +08:00
#define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
#define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
#else
#define SPDLOG_LOGGER_INFO(logger, ...) (void)0
#define SPDLOG_INFO(...) (void)0
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
2019-01-10 23:04:27 +08:00
#define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
2018-11-24 23:15:58 +08:00
#define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__)
#else
#define SPDLOG_LOGGER_WARN(logger, ...) (void)0
#define SPDLOG_WARN(...) (void)0
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
2019-01-10 23:04:27 +08:00
#define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
#define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__)
2017-08-25 23:19:29 +08:00
#else
#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
#define SPDLOG_ERROR(...) (void)0
2016-04-20 16:57:49 +08:00
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
2019-01-10 23:04:27 +08:00
#define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
#define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__)
2016-04-20 16:57:49 +08:00
#else
#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
#define SPDLOG_CRITICAL(...) (void)0
2016-04-20 16:57:49 +08:00
#endif
2019-05-13 04:08:14 +08:00
#ifdef SPDLOG_HEADER_ONLY
#include "spdlog-inl.h"
#endif
2018-10-12 08:04:55 +08:00
#endif // SPDLOG_H