2019-06-04 00:09:16 +03:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
2016-09-02 17:06:00 +03:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-10-01 01:50:18 +03:00
|
|
|
// Thread safe logger (except for set_error_handler())
|
2016-09-02 17:06:00 +03:00
|
|
|
// Has name, log level, vector of std::shared sink pointers and formatter
|
|
|
|
|
// Upon each log write the logger:
|
2018-06-24 01:32:39 +03:00
|
|
|
// 1. Checks if its log level is enough to log the message and if yes:
|
|
|
|
|
// 2. Call the underlying sinks to do the job.
|
2018-07-21 23:48:07 +03:00
|
|
|
// 3. Each sink use its own private copy of a formatter to format the message
|
|
|
|
|
// and send to its destination.
|
2018-06-24 01:32:39 +03:00
|
|
|
//
|
2018-07-21 23:48:07 +03:00
|
|
|
// The use of private formatter per sink provides the opportunity to cache some
|
2019-04-05 16:44:17 +03:00
|
|
|
// formatted data, and support for different format per sink.
|
|
|
|
|
|
2019-11-06 19:09:30 +00:00
|
|
|
#include <spdlog/common.h>
|
|
|
|
|
#include <spdlog/details/log_msg.h>
|
2019-04-05 16:44:17 +03:00
|
|
|
|
2018-03-09 15:26:33 +02:00
|
|
|
#include <vector>
|
2021-06-24 17:07:14 +03:00
|
|
|
|
2019-08-19 11:31:33 +03:00
|
|
|
#ifndef SPDLOG_NO_EXCEPTIONS
|
2021-08-17 15:26:59 +02:00
|
|
|
# define SPDLOG_LOGGER_CATCH(location) \
|
2021-06-26 17:58:45 +03:00
|
|
|
catch (const std::exception &ex) \
|
|
|
|
|
{ \
|
2021-11-16 23:44:35 +02:00
|
|
|
if (location.filename) \
|
2021-08-17 15:26:59 +02:00
|
|
|
{ \
|
2022-05-18 16:30:36 -07:00
|
|
|
err_handler_(fmt_lib::format(SPDLOG_FMT_STRING("{} [{}({})]"), ex.what(), location.filename, location.line)); \
|
2021-08-17 15:26:59 +02:00
|
|
|
} \
|
|
|
|
|
else \
|
|
|
|
|
{ \
|
|
|
|
|
err_handler_(ex.what()); \
|
|
|
|
|
} \
|
2021-06-26 17:58:45 +03:00
|
|
|
} \
|
|
|
|
|
catch (...) \
|
|
|
|
|
{ \
|
|
|
|
|
err_handler_("Rethrowing unknown exception in logger"); \
|
|
|
|
|
throw; \
|
|
|
|
|
}
|
2019-08-19 11:31:33 +03:00
|
|
|
#else
|
2021-08-17 19:21:39 +03:00
|
|
|
# define SPDLOG_LOGGER_CATCH(location)
|
2019-08-19 11:31:33 +03:00
|
|
|
#endif
|
2019-07-17 16:01:30 +03:00
|
|
|
|
2019-04-05 23:05:46 +03:00
|
|
|
namespace spdlog {
|
2019-08-26 19:59:16 +03:00
|
|
|
|
2020-03-10 02:02:16 +07:00
|
|
|
class SPDLOG_API logger
|
2016-09-02 17:06:00 +03:00
|
|
|
{
|
2019-04-05 23:05:46 +03:00
|
|
|
public:
|
2019-06-01 14:57:57 +03:00
|
|
|
// Empty logger
|
2019-06-04 00:09:16 +03:00
|
|
|
explicit logger(std::string name)
|
2023-07-28 18:45:31 +03:00
|
|
|
: name_(std::move(name))
|
2019-06-03 22:49:21 +03:00
|
|
|
{}
|
2019-06-01 14:57:57 +03:00
|
|
|
|
|
|
|
|
// Logger with range on sinks
|
2019-04-27 02:34:50 +03:00
|
|
|
template<typename It>
|
|
|
|
|
logger(std::string name, It begin, It end)
|
|
|
|
|
: name_(std::move(name))
|
|
|
|
|
, sinks_(begin, end)
|
2019-05-12 00:22:39 +03:00
|
|
|
{}
|
2019-04-27 02:34:50 +03:00
|
|
|
|
2019-06-01 14:57:57 +03:00
|
|
|
// Logger with single sink
|
2019-04-27 02:34:50 +03:00
|
|
|
logger(std::string name, sink_ptr single_sink)
|
2019-08-28 23:53:00 +03:00
|
|
|
: logger(std::move(name), {std::move(single_sink)})
|
2019-05-12 00:22:39 +03:00
|
|
|
{}
|
2019-06-01 14:57:57 +03:00
|
|
|
|
|
|
|
|
// Logger with sinks init list
|
2019-04-27 02:34:50 +03:00
|
|
|
logger(std::string name, sinks_init_list sinks)
|
|
|
|
|
: logger(std::move(name), sinks.begin(), sinks.end())
|
2019-05-12 00:22:39 +03:00
|
|
|
{}
|
2019-04-27 02:34:50 +03:00
|
|
|
|
|
|
|
|
virtual ~logger() = default;
|
|
|
|
|
|
2019-08-19 11:31:33 +03:00
|
|
|
logger(const logger &other);
|
2023-07-28 18:27:05 +03:00
|
|
|
logger(logger &&other) noexcept;
|
|
|
|
|
logger &operator=(logger other) noexcept;
|
|
|
|
|
void swap(spdlog::logger &other) noexcept;
|
2019-04-27 02:34:50 +03:00
|
|
|
|
2021-07-22 16:19:48 +01:00
|
|
|
template<typename... Args>
|
2023-06-07 13:23:44 +03:00
|
|
|
void log(source_loc loc, level::level_enum lvl, format_string_t<Args...> fmt, Args &&...args)
|
2019-04-27 02:34:50 +03:00
|
|
|
{
|
2023-07-28 18:42:11 +03:00
|
|
|
log_with_format_(loc, lvl, details::to_string_view(fmt), std::forward<Args>(args)...);
|
2019-04-27 02:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 16:19:48 +01:00
|
|
|
template<typename... Args>
|
2023-06-07 13:23:44 +03:00
|
|
|
void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&...args)
|
2019-04-27 02:34:50 +03:00
|
|
|
{
|
2020-11-02 00:37:03 +00:00
|
|
|
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
|
2019-04-27 02:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2020-06-02 20:30:25 -05:00
|
|
|
void log(level::level_enum lvl, const T &msg)
|
2019-04-27 02:34:50 +03:00
|
|
|
{
|
2020-06-02 20:30:25 -05:00
|
|
|
log(source_loc{}, lvl, msg);
|
2019-04-27 02:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
2021-11-13 11:29:05 -05:00
|
|
|
// T cannot be statically converted to format string (including string_view/wstring_view)
|
2021-08-09 17:33:00 +01:00
|
|
|
template<class T, typename std::enable_if<!is_convertible_to_any_format_string<const T &>::value, int>::type = 0>
|
2021-07-27 00:26:32 +03:00
|
|
|
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
|
|
|
|
{
|
2021-07-27 09:19:02 +02:00
|
|
|
log(loc, lvl, "{}", msg);
|
2021-07-27 00:26:32 +03:00
|
|
|
}
|
|
|
|
|
|
2020-04-18 22:44:13 -05:00
|
|
|
void log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, string_view_t msg)
|
2020-04-18 08:58:11 -05:00
|
|
|
{
|
2023-07-28 17:38:54 +03:00
|
|
|
if (should_log(lvl))
|
2020-04-18 08:58:11 -05:00
|
|
|
{
|
2023-07-28 17:37:19 +03:00
|
|
|
sink_it_(details::log_msg(log_time, loc, name_, lvl, msg));
|
2020-04-18 08:58:11 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-08 11:11:04 +01:00
|
|
|
void log(source_loc loc, level::level_enum lvl, string_view_t msg)
|
2019-04-27 02:34:50 +03:00
|
|
|
{
|
2023-07-28 17:38:54 +03:00
|
|
|
if (should_log(lvl))
|
2019-08-26 19:59:16 +03:00
|
|
|
{
|
2023-07-28 17:37:19 +03:00
|
|
|
sink_it_(details::log_msg(loc, name_, lvl, msg));
|
2019-08-26 19:59:16 +03:00
|
|
|
}
|
2019-08-27 00:56:49 +03:00
|
|
|
}
|
2019-07-17 16:01:30 +03:00
|
|
|
|
2019-08-27 00:56:49 +03:00
|
|
|
void log(level::level_enum lvl, string_view_t msg)
|
|
|
|
|
{
|
|
|
|
|
log(source_loc{}, lvl, msg);
|
2019-04-27 02:34:50 +03:00
|
|
|
}
|
2021-07-27 09:19:02 +02:00
|
|
|
|
2021-07-22 16:19:48 +01:00
|
|
|
template<typename... Args>
|
2023-06-07 13:23:44 +03:00
|
|
|
void trace(format_string_t<Args...> fmt, Args &&...args)
|
2021-07-19 00:48:01 +03:00
|
|
|
{
|
|
|
|
|
log(level::trace, fmt, std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 16:19:48 +01:00
|
|
|
template<typename... Args>
|
2023-06-07 13:23:44 +03:00
|
|
|
void debug(format_string_t<Args...> fmt, Args &&...args)
|
2021-07-19 00:48:01 +03:00
|
|
|
{
|
|
|
|
|
log(level::debug, fmt, std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 16:19:48 +01:00
|
|
|
template<typename... Args>
|
2023-06-07 13:23:44 +03:00
|
|
|
void info(format_string_t<Args...> fmt, Args &&...args)
|
2021-07-19 00:48:01 +03:00
|
|
|
{
|
|
|
|
|
log(level::info, fmt, std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 16:19:48 +01:00
|
|
|
template<typename... Args>
|
2023-06-07 13:23:44 +03:00
|
|
|
void warn(format_string_t<Args...> fmt, Args &&...args)
|
2021-07-19 00:48:01 +03:00
|
|
|
{
|
|
|
|
|
log(level::warn, fmt, std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 16:19:48 +01:00
|
|
|
template<typename... Args>
|
2023-06-07 13:23:44 +03:00
|
|
|
void error(format_string_t<Args...> fmt, Args &&...args)
|
2021-07-19 00:48:01 +03:00
|
|
|
{
|
|
|
|
|
log(level::err, fmt, std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 16:19:48 +01:00
|
|
|
template<typename... Args>
|
2023-06-07 13:23:44 +03:00
|
|
|
void critical(format_string_t<Args...> fmt, Args &&...args)
|
2021-07-19 00:48:01 +03:00
|
|
|
{
|
|
|
|
|
log(level::critical, fmt, std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void trace(const T &msg)
|
|
|
|
|
{
|
|
|
|
|
log(level::trace, msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void debug(const T &msg)
|
|
|
|
|
{
|
|
|
|
|
log(level::debug, msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void info(const T &msg)
|
|
|
|
|
{
|
|
|
|
|
log(level::info, msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void warn(const T &msg)
|
|
|
|
|
{
|
|
|
|
|
log(level::warn, msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void error(const T &msg)
|
|
|
|
|
{
|
|
|
|
|
log(level::err, msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void critical(const T &msg)
|
|
|
|
|
{
|
|
|
|
|
log(level::critical, msg);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 21:41:02 +03:00
|
|
|
// return true logging is enabled for the given level.
|
2023-07-28 17:38:54 +03:00
|
|
|
[[nodiscard]] bool should_log(level::level_enum msg_level) const
|
2019-11-03 17:04:34 +02:00
|
|
|
{
|
|
|
|
|
return msg_level >= level_.load(std::memory_order_relaxed);
|
|
|
|
|
}
|
2019-04-05 13:26:33 +03:00
|
|
|
|
2023-07-28 17:33:00 +03:00
|
|
|
// set the level of logging
|
2019-04-27 02:34:50 +03:00
|
|
|
void set_level(level::level_enum log_level);
|
2018-07-23 00:13:52 +03:00
|
|
|
|
2023-07-28 17:33:00 +03:00
|
|
|
// return the active log level
|
2023-07-28 17:40:21 +03:00
|
|
|
[[nodiscard]] level::level_enum level() const;
|
2018-07-23 00:13:52 +03:00
|
|
|
|
2023-07-28 17:33:00 +03:00
|
|
|
// return the name of the logger
|
2023-07-28 17:40:21 +03:00
|
|
|
[[nodiscard]] const std::string &name() const;
|
2019-04-05 13:26:33 +03:00
|
|
|
|
2019-04-27 02:34:50 +03:00
|
|
|
// set formatting for the sinks in this logger.
|
2019-10-19 09:42:28 +02:00
|
|
|
// each sink will get a separate instance of the formatter object.
|
2019-04-27 02:34:50 +03:00
|
|
|
void set_formatter(std::unique_ptr<formatter> f);
|
2019-04-05 13:26:33 +03:00
|
|
|
|
2022-07-01 10:53:05 +03:00
|
|
|
// set formatting for the sinks in this logger.
|
|
|
|
|
// equivalent to
|
|
|
|
|
// set_formatter(make_unique<pattern_formatter>(pattern, time_type))
|
|
|
|
|
// Note: each sink will get a new instance of a formatter object, replacing the old one.
|
2019-04-27 02:34:50 +03:00
|
|
|
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
2016-09-02 17:06:00 +03:00
|
|
|
|
2019-04-27 02:34:50 +03:00
|
|
|
// flush functions
|
|
|
|
|
void flush();
|
|
|
|
|
void flush_on(level::level_enum log_level);
|
2023-07-28 17:40:21 +03:00
|
|
|
[[nodiscard]] level::level_enum flush_level() const;
|
2019-04-05 13:26:33 +03:00
|
|
|
|
2019-04-27 02:34:50 +03:00
|
|
|
// sinks
|
2023-07-28 17:40:21 +03:00
|
|
|
[[nodiscard]] const std::vector<sink_ptr> &sinks() const;
|
2018-07-23 00:13:52 +03:00
|
|
|
|
2023-07-28 18:45:31 +03:00
|
|
|
[[nodiscard]] std::vector<sink_ptr> &sinks();
|
2019-04-05 13:26:33 +03:00
|
|
|
|
2019-04-27 02:34:50 +03:00
|
|
|
// error handler
|
|
|
|
|
void set_error_handler(err_handler);
|
2016-09-02 17:06:00 +03:00
|
|
|
|
2019-08-29 01:05:23 +03:00
|
|
|
// create new logger with same sinks and configuration.
|
|
|
|
|
virtual std::shared_ptr<logger> clone(std::string logger_name);
|
|
|
|
|
|
2019-04-27 02:34:50 +03:00
|
|
|
protected:
|
2019-06-15 19:46:41 +03:00
|
|
|
std::string name_;
|
|
|
|
|
std::vector<sink_ptr> sinks_;
|
2019-08-16 19:04:49 +03:00
|
|
|
spdlog::level_t level_{level::info};
|
2019-06-15 19:46:41 +03:00
|
|
|
spdlog::level_t flush_level_{level::off};
|
|
|
|
|
err_handler custom_err_handler_{nullptr};
|
2019-04-05 13:26:33 +03:00
|
|
|
|
2023-07-28 18:42:11 +03:00
|
|
|
// common implementation for after templated public api has been resolved to format string and args
|
2021-07-22 16:19:48 +01:00
|
|
|
template<typename... Args>
|
2023-07-28 18:42:11 +03:00
|
|
|
void log_with_format_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&...args)
|
2020-05-31 13:15:40 -05:00
|
|
|
{
|
2023-07-28 18:42:11 +03:00
|
|
|
if (!should_log(lvl))
|
2020-05-31 13:15:40 -05:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SPDLOG_TRY
|
|
|
|
|
{
|
|
|
|
|
memory_buf_t buf;
|
2022-05-08 13:01:02 +03:00
|
|
|
#ifdef SPDLOG_USE_STD_FORMAT
|
2023-05-30 20:38:30 +03:00
|
|
|
fmt_lib::vformat_to(std::back_inserter(buf), fmt, fmt_lib::make_format_args(args...));
|
2022-05-08 13:01:02 +03:00
|
|
|
#else
|
2023-05-27 22:28:22 +03:00
|
|
|
fmt::vformat_to(fmt::appender(buf), fmt, fmt::make_format_args(args...));
|
2022-05-08 13:01:02 +03:00
|
|
|
#endif
|
2023-07-28 17:33:00 +03:00
|
|
|
sink_it_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())));
|
2020-05-31 13:15:40 -05:00
|
|
|
}
|
2021-08-17 15:26:59 +02:00
|
|
|
SPDLOG_LOGGER_CATCH(loc)
|
2020-05-31 13:15:40 -05:00
|
|
|
}
|
|
|
|
|
|
2023-07-28 17:33:00 +03:00
|
|
|
// log the given message (if the given log level is high enough)
|
2019-08-22 19:57:59 +03:00
|
|
|
virtual void sink_it_(const details::log_msg &msg);
|
2019-04-27 02:34:50 +03:00
|
|
|
virtual void flush_();
|
|
|
|
|
bool should_flush_(const details::log_msg &msg);
|
2019-04-05 23:34:55 +03:00
|
|
|
|
2019-07-17 18:05:01 +03:00
|
|
|
// handle errors during logging.
|
|
|
|
|
// default handler prints the error to stderr at max rate of 1 message/sec.
|
2019-04-27 02:34:50 +03:00
|
|
|
void err_handler_(const std::string &msg);
|
2019-04-05 23:05:46 +03:00
|
|
|
};
|
2019-06-15 19:46:41 +03:00
|
|
|
|
2019-06-18 17:05:27 +03:00
|
|
|
void swap(logger &a, logger &b);
|
2019-06-15 19:46:41 +03:00
|
|
|
|
2018-03-09 15:26:33 +02:00
|
|
|
} // namespace spdlog
|
2016-09-02 17:06:00 +03:00
|
|
|
|
2019-05-12 00:22:39 +03:00
|
|
|
#ifdef SPDLOG_HEADER_ONLY
|
2021-06-26 17:58:45 +03:00
|
|
|
# include "logger-inl.h"
|
2019-04-27 18:44:48 +03:00
|
|
|
#endif
|