2016-04-20 16:57:49 +08:00
|
|
|
//
|
2018-04-14 08:34:57 +08:00
|
|
|
// Copyright(c) 2015-2018 Gabi Melman.
|
2016-04-20 16:57:49 +08:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
//
|
|
|
|
// spdlog main header file.
|
|
|
|
// see example.cpp for usage example
|
|
|
|
|
2018-10-12 00:14:02 +08:00
|
|
|
#ifndef SPDLOG_H
|
|
|
|
#define SPDLOG_H
|
2016-04-20 16:57:49 +08:00
|
|
|
#pragma once
|
|
|
|
|
2018-04-29 06:31:09 +08:00
|
|
|
#include "spdlog/common.h"
|
|
|
|
#include "spdlog/details/registry.h"
|
|
|
|
#include "spdlog/logger.h"
|
2018-07-25 05:06:10 +08:00
|
|
|
#include "spdlog/version.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
|
|
|
|
2018-04-14 08:34:57 +08:00
|
|
|
// Default logger factory- creates synchronous loggers
|
2018-07-07 17:12:45 +08:00
|
|
|
struct synchronous_factory
|
2018-04-14 08:34:57 +08:00
|
|
|
{
|
|
|
|
template<typename Sink, typename... SinkArgs>
|
2018-07-19 19:54:26 +08:00
|
|
|
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... args)
|
2018-04-14 08:34:57 +08:00
|
|
|
{
|
|
|
|
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
2018-07-19 19:54:26 +08:00
|
|
|
auto new_logger = std::make_shared<logger>(std::move(logger_name), std::move(sink));
|
2018-11-05 03:12:42 +08:00
|
|
|
details::registry::instance().init_with_global_defaults(new_logger);
|
|
|
|
#ifndef SPDLOG_DISABLE_GLOBAL_REGISTRATION
|
|
|
|
details::registry::instance().register_logger(new_logger);
|
|
|
|
#endif
|
2018-04-14 08:34:57 +08:00
|
|
|
return new_logger;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-07 17:12:45 +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.
|
2018-04-14 08:34:57 +08:00
|
|
|
// Example:
|
2018-07-23 02:52:46 +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>
|
2018-07-19 19:54:26 +08:00
|
|
|
inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... sink_args)
|
2018-04-14 08:34:57 +08:00
|
|
|
{
|
2018-07-19 19:54:26 +08:00
|
|
|
return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
|
2018-04-14 08:34:57 +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");
|
2018-04-14 08:34:57 +08:00
|
|
|
inline std::shared_ptr<logger> get(const std::string &name)
|
|
|
|
{
|
|
|
|
return details::registry::instance().get(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
|
|
|
|
inline void set_formatter(std::unique_ptr<spdlog::formatter> formatter)
|
|
|
|
{
|
2018-07-23 05:13:52 +08:00
|
|
|
details::registry::instance().set_formatter(std::move(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");
|
2018-07-23 02:52:46 +08:00
|
|
|
inline void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local)
|
2018-04-14 08:34:57 +08:00
|
|
|
{
|
2018-08-13 15:30:02 +08:00
|
|
|
set_formatter(std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type)));
|
2018-04-14 08:34:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-03 23:19:56 +08:00
|
|
|
// Set global logging level
|
2018-04-14 08:34:57 +08:00
|
|
|
inline void set_level(level::level_enum log_level)
|
|
|
|
{
|
|
|
|
details::registry::instance().set_level(log_level);
|
|
|
|
}
|
2018-01-03 23:19:56 +08:00
|
|
|
|
|
|
|
// Set global flush level
|
2018-04-14 08:34:57 +08:00
|
|
|
inline void flush_on(level::level_enum log_level)
|
|
|
|
{
|
|
|
|
details::registry::instance().flush_on(log_level);
|
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2018-07-22 04:30:26 +08:00
|
|
|
// Start/Restart a periodic flusher thread
|
|
|
|
// Warning: Use only if all your loggers are thread safe!
|
|
|
|
inline void flush_every(std::chrono::seconds interval)
|
|
|
|
{
|
|
|
|
details::registry::instance().flush_every(interval);
|
|
|
|
}
|
|
|
|
|
2016-08-05 08:56:40 +08:00
|
|
|
// Set global error handler
|
2018-04-14 08:34:57 +08:00
|
|
|
inline void set_error_handler(log_err_handler handler)
|
|
|
|
{
|
|
|
|
details::registry::instance().set_error_handler(std::move(handler));
|
|
|
|
}
|
2016-08-05 08:56:40 +08:00
|
|
|
|
2018-04-14 08:34:57 +08:00
|
|
|
// Register the given logger with the given name
|
|
|
|
inline void register_logger(std::shared_ptr<logger> logger)
|
|
|
|
{
|
|
|
|
details::registry::instance().register_logger(std::move(logger));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply a user defined function on all registered loggers
|
|
|
|
// Example:
|
|
|
|
// spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
|
2018-08-13 15:30:02 +08:00
|
|
|
inline void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun)
|
2018-04-14 08:34:57 +08:00
|
|
|
{
|
2018-08-13 15:30:02 +08:00
|
|
|
details::registry::instance().apply_all(fun);
|
2018-04-14 08:34:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the reference to the given logger
|
|
|
|
inline void drop(const std::string &name)
|
|
|
|
{
|
|
|
|
details::registry::instance().drop(name);
|
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2018-04-14 08:34:57 +08:00
|
|
|
// Drop all references from the registry
|
|
|
|
inline void drop_all()
|
|
|
|
{
|
|
|
|
details::registry::instance().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
|
2018-07-25 04:02:27 +08:00
|
|
|
inline void shutdown()
|
2018-07-25 03:59:34 +08:00
|
|
|
{
|
2018-07-25 05:06:10 +08:00
|
|
|
details::registry::instance().shutdown();
|
2018-07-25 03:59:34 +08:00
|
|
|
}
|
|
|
|
|
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:
|
2018-10-14 00:23:11 +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
|
|
|
|
2018-10-14 00:23:11 +08:00
|
|
|
inline std::shared_ptr<spdlog::logger> default_logger()
|
|
|
|
{
|
|
|
|
return details::registry::instance().default_logger();
|
|
|
|
}
|
2018-10-12 08:04:55 +08:00
|
|
|
|
2018-10-14 00:23:11 +08:00
|
|
|
inline spdlog::logger *default_logger_raw()
|
2018-10-12 08:04:55 +08:00
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
return details::registry::instance().get_default_raw();
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void set_default_logger(std::shared_ptr<spdlog::logger> default_logger)
|
|
|
|
{
|
|
|
|
details::registry::instance().set_default_logger(std::move(default_logger));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void log(level::level_enum lvl, const char *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->log(lvl, fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void trace(const char *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->trace(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void debug(const char *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->debug(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void info(const char *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->info(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void warn(const char *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->warn(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void error(const char *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->error(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void critical(const char *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->critical(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void log(level::level_enum lvl, const T &msg)
|
|
|
|
{
|
2018-10-14 00:23:11 +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-14 00:23:11 +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-14 00:23:11 +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-14 00:23:11 +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-14 00:23:11 +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-14 00:23:11 +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-14 00:23:11 +08:00
|
|
|
default_logger_raw()->critical(msg);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
|
|
|
template<typename... Args>
|
|
|
|
inline void log(level::level_enum lvl, const wchar_t *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->log(lvl, fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void trace(const wchar_t *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->trace(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void debug(const wchar_t *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->debug(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void info(const wchar_t *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->info(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void warn(const wchar_t *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->warn(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void error(const wchar_t *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->error(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void critical(const wchar_t *fmt, const Args &... args)
|
|
|
|
{
|
2018-10-14 00:23:11 +08:00
|
|
|
default_logger_raw()->critical(fmt, args...);
|
2018-10-12 08:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
|
|
|
|
|
|
|
//
|
|
|
|
// Trace & Debug can be switched on/off at compile time with zero cost.
|
2017-10-24 18:34:58 +08:00
|
|
|
// Uncomment SPDLOG_DEBUG_ON/SPDLOG_TRACE_ON in tweakme.h to enable.
|
2016-07-14 19:59:49 +08:00
|
|
|
// SPDLOG_TRACE(..) will also print current file and line.
|
2016-04-20 16:57:49 +08:00
|
|
|
//
|
|
|
|
// Example:
|
2016-07-15 22:55:34 +08:00
|
|
|
// spdlog::set_level(spdlog::level::trace);
|
2016-07-14 19:59:49 +08:00
|
|
|
// SPDLOG_TRACE(my_logger, "another trace message {} {}", 1, 2);
|
2018-10-12 08:04:55 +08:00
|
|
|
//
|
2016-04-20 16:57:49 +08:00
|
|
|
|
|
|
|
#ifdef SPDLOG_TRACE_ON
|
2018-03-09 21:26:33 +08:00
|
|
|
#define SPDLOG_STR_H(x) #x
|
|
|
|
#define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x)
|
|
|
|
#ifdef _MSC_VER
|
2018-10-12 00:14:02 +08:00
|
|
|
#define SPDLOG_TRACE(logger, ...) \
|
|
|
|
logger->trace("[ "__FILE__ \
|
|
|
|
"(" SPDLOG_STR_HELPER(__LINE__) ")] " __VA_ARGS__)
|
2018-03-09 21:26:33 +08:00
|
|
|
#else
|
2018-07-22 04:48:07 +08:00
|
|
|
#define SPDLOG_TRACE(logger, ...) \
|
2018-10-12 00:14:02 +08:00
|
|
|
logger->trace("[" __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) "]" \
|
|
|
|
" " __VA_ARGS__)
|
2018-03-09 21:26:33 +08:00
|
|
|
#endif
|
2017-08-25 23:19:29 +08:00
|
|
|
#else
|
2018-03-09 21:26:33 +08:00
|
|
|
#define SPDLOG_TRACE(logger, ...) (void)0
|
2016-04-20 16:57:49 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SPDLOG_DEBUG_ON
|
2018-03-09 21:26:33 +08:00
|
|
|
#define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__)
|
2016-04-20 16:57:49 +08:00
|
|
|
#else
|
2018-03-09 21:26:33 +08:00
|
|
|
#define SPDLOG_DEBUG(logger, ...) (void)0
|
2016-04-20 16:57:49 +08:00
|
|
|
#endif
|
|
|
|
|
2018-03-09 21:26:33 +08:00
|
|
|
} // namespace spdlog
|
2018-10-12 08:04:55 +08:00
|
|
|
#endif // SPDLOG_H
|