Files
spdlog/include/spdlog/logger.h

296 lines
11 KiB
C
Raw Normal View History

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.
2023-09-29 00:20:26 +03:00
#include <cassert>
#include <vector>
2023-09-28 23:45:45 +03:00
#include "common.h"
#include "details/log_msg.h"
#include "sinks/sink.h"
2019-04-05 16:44:17 +03:00
#ifndef SPDLOG_NO_EXCEPTIONS
#define SPDLOG_LOGGER_CATCH(location) \
catch (const std::exception &ex) { \
if (!location.empty()) { \
err_handler_( \
fmt_lib::format("{} [{}({})]", ex.what(), location.filename, location.line)); \
} else { \
err_handler_(ex.what()); \
} \
} \
catch (...) { \
err_handler_("Rethrowing unknown exception in logger"); \
throw; \
2021-06-26 17:58:45 +03:00
}
#else
2023-09-25 02:35:55 +03:00
#define SPDLOG_LOGGER_CATCH(location)
#endif
2019-04-05 23:05:46 +03:00
namespace spdlog {
2019-08-26 19:59:16 +03:00
2023-09-25 02:35:55 +03:00
class SPDLOG_API logger {
2019-04-05 23:05:46 +03:00
public:
// Empty logger
2019-06-04 00:09:16 +03:00
explicit logger(std::string name)
2023-09-25 02:35:55 +03:00
: name_(std::move(name)) {}
// Logger with range on sinks
2023-09-25 02:35:55 +03:00
template <typename It>
2019-04-27 02:34:50 +03:00
logger(std::string name, It begin, It end)
2023-09-25 16:19:10 +03:00
: name_(std::move(name)),
sinks_(begin, end) {}
2019-04-27 02:34:50 +03:00
// Logger with single sink
2019-04-27 02:34:50 +03:00
logger(std::string name, sink_ptr single_sink)
2023-09-25 02:35:55 +03:00
: logger(std::move(name), {std::move(single_sink)}) {}
// Logger with sinks init list
2019-04-27 02:34:50 +03:00
logger(std::string name, sinks_init_list sinks)
2023-09-25 02:35:55 +03:00
: logger(std::move(name), sinks.begin(), sinks.end()) {}
2019-04-27 02:34:50 +03:00
2023-09-15 21:07:09 +03:00
logger(const logger &other) noexcept;
logger(logger &&other) noexcept;
virtual ~logger() = default;
2019-04-27 02:34:50 +03:00
// log functions
2023-09-25 02:35:55 +03:00
template <typename... Args>
void log(source_loc loc, level lvl, format_string_t<Args...> fmt, Args &&...args) {
if (should_log(lvl)) {
2023-09-16 17:23:04 +03:00
log_with_format_(loc, lvl, details::to_string_view(fmt), std::forward<Args>(args)...);
2023-09-08 16:56:39 +03:00
}
2019-04-27 02:34:50 +03:00
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void log(level lvl, format_string_t<Args...> fmt, Args &&...args) {
if (should_log(lvl)) {
log_with_format_(source_loc{}, lvl, details::to_string_view(fmt),
std::forward<Args>(args)...);
}
2019-04-27 02:34:50 +03:00
}
2023-09-25 02:35:55 +03:00
template <typename S, typename = is_convertible_to_sv<S>, typename... Args>
void log(source_loc loc, level lvl, S fmt, Args &&...args) {
if (should_log(lvl)) {
log_with_format_(loc, lvl, fmt, std::forward<Args>(args)...);
}
2019-04-27 02:34:50 +03:00
}
// log with no format string, just string message
2023-09-25 02:35:55 +03:00
void log(source_loc loc, level lvl, string_view_t msg) {
if (should_log(lvl)) {
sink_it_(details::log_msg(loc, name_, lvl, msg));
}
}
2023-09-25 02:35:55 +03:00
void log(level lvl, string_view_t msg) {
if (should_log(lvl)) {
2023-09-15 16:08:04 +03:00
sink_it_(details::log_msg(source_loc{}, name_, lvl, msg));
}
}
// support for custom time
2023-09-25 02:35:55 +03:00
void log(log_clock::time_point log_time, source_loc loc, level lvl, string_view_t msg) {
if (should_log(lvl)) {
2023-07-28 17:37:19 +03:00
sink_it_(details::log_msg(log_time, loc, name_, lvl, msg));
}
}
2023-09-25 16:45:11 +03:00
#ifdef SPDLOG_SOURCE_LOCATION // off by default. define SPDLOG_SOURCE_LOCATION before including
// spdlog to enable.
2023-09-25 02:35:55 +03:00
template <typename... Args>
void trace(loc_with_fmt fmt, Args &&...args) {
log(fmt.loc, level::trace, fmt.fmt_string, std::forward<Args>(args)...);
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void debug(loc_with_fmt fmt, Args &&...args) {
log(fmt.loc, level::debug, fmt.fmt_string, std::forward<Args>(args)...);
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void info(loc_with_fmt fmt, Args &&...args) {
log(fmt.loc, level::info, fmt.fmt_string, std::forward<Args>(args)...);
2019-04-27 02:34:50 +03:00
}
2021-07-27 09:19:02 +02:00
2023-09-25 02:35:55 +03:00
template <typename... Args>
void warn(loc_with_fmt fmt, Args &&...args) {
log(fmt.loc, level::warn, fmt.fmt_string, std::forward<Args>(args)...);
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void error(loc_with_fmt fmt, Args &&...args) {
log(fmt.loc, level::err, fmt.fmt_string, std::forward<Args>(args)...);
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void critical(loc_with_fmt fmt, Args &&...args) {
log(fmt.loc, level::critical, fmt.fmt_string, std::forward<Args>(args)...);
}
2023-09-15 16:08:04 +03:00
// log functions with no format string, just string
void trace(string_view_t msg, source_loc loc = source_loc::current()) {
log(loc, level::trace, msg);
}
void debug(string_view_t msg, source_loc loc = source_loc::current()) {
log(loc, level::debug, msg);
}
void info(string_view_t msg, source_loc loc = source_loc::current()) {
log(loc, level::info, msg);
}
void warn(string_view_t msg, source_loc loc = source_loc::current()) {
log(loc, level::warn, msg);
}
void error(string_view_t msg, source_loc loc = source_loc::current()) {
log(loc, level::err, msg);
}
void critical(string_view_t msg, source_loc loc = source_loc::current()) {
log(loc, level::critical, msg);
}
2023-09-25 16:45:11 +03:00
#else // without source location
2023-09-25 02:35:55 +03:00
template <typename... Args>
void trace(format_string_t<Args...> fmt, Args &&...args) {
log(level::trace, fmt, std::forward<Args>(args)...);
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void debug(format_string_t<Args...> fmt, Args &&...args) {
log(level::debug, fmt, std::forward<Args>(args)...);
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void info(format_string_t<Args...> fmt, Args &&...args) {
log(level::info, fmt, std::forward<Args>(args)...);
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void warn(format_string_t<Args...> fmt, Args &&...args) {
log(level::warn, fmt, std::forward<Args>(args)...);
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void error(format_string_t<Args...> fmt, Args &&...args) {
log(level::err, fmt, std::forward<Args>(args)...);
}
2023-09-25 02:35:55 +03:00
template <typename... Args>
void critical(format_string_t<Args...> fmt, Args &&...args) {
log(level::critical, fmt, std::forward<Args>(args)...);
}
2023-09-15 16:08:04 +03:00
// log functions with no format string, just string
2023-09-25 02:35:55 +03:00
void trace(string_view_t msg) { log(level::trace, msg); }
void debug(string_view_t msg) { log(level::debug, msg); }
void info(string_view_t msg) { log(level::info, msg); }
2023-09-25 16:45:11 +03:00
void warn(string_view_t msg) { log(level::warn, msg); }
2023-09-25 02:35:55 +03:00
void error(string_view_t msg) { log(level::err, msg); }
void critical(string_view_t msg) { log(level::critical, msg); }
2023-09-25 16:45:11 +03:00
#endif // SPDLOG_SOURCE_LOCATION
2023-09-16 02:23:26 +03:00
// return true if logging is enabled for the given level.
[[nodiscard]] bool should_log(level msg_level) const {
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
void set_level(level level);
2018-07-23 00:13:52 +03:00
2023-07-28 17:33:00 +03:00
// return the active log level
[[nodiscard]] level log_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.
// 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
// 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);
[[nodiscard]] level 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;
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:
std::string name_;
std::vector<sink_ptr> sinks_;
spdlog::atomic_level_t level_{level::info};
spdlog::atomic_level_t flush_level_{level::off};
err_handler custom_err_handler_{nullptr};
2019-04-05 13:26:33 +03:00
// common implementation for after templated public api has been resolved to format string and
// args
2023-09-25 02:35:55 +03:00
template <typename... Args>
void log_with_format_(source_loc loc, level lvl, string_view_t fmt, Args &&...args) {
2023-09-08 16:56:39 +03:00
assert(should_log(lvl));
2023-09-25 02:35:55 +03:00
SPDLOG_TRY {
#ifdef SPDLOG_USE_STD_FORMAT
2023-09-17 20:05:00 +03:00
auto formatted = std::vformat(fmt, std::make_format_args(args...));
sink_it_(details::log_msg(loc, name_, lvl, formatted));
2023-09-25 16:40:05 +03:00
#else // use {fmt} lib
2023-09-17 20:05:00 +03:00
memory_buf_t buf;
fmt::vformat_to(std::back_inserter(buf), fmt, fmt::make_format_args(args...));
2023-07-28 17:33:00 +03:00
sink_it_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())));
2023-09-17 20:05:00 +03:00
#endif
}
SPDLOG_LOGGER_CATCH(loc)
}
2023-07-28 17:33:00 +03:00
// log the given message (if the given log level is high enough)
2023-09-25 02:35:55 +03:00
virtual void sink_it_(const details::log_msg &msg) {
assert(should_log(msg.log_level));
2023-09-25 02:35:55 +03:00
for (auto &sink : sinks_) {
if (sink->should_log(msg.log_level)) {
SPDLOG_TRY { sink->log(msg); }
2023-07-28 18:55:52 +03:00
SPDLOG_LOGGER_CATCH(msg.source)
}
}
2023-09-25 02:35:55 +03:00
if (should_flush_(msg)) {
2023-07-28 18:55:52 +03:00
flush_();
}
}
2019-04-27 02:34:50 +03:00
virtual void flush_();
bool should_flush_(const details::log_msg &msg);
// 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
};
2023-09-25 16:40:05 +03:00
} // namespace spdlog