spdlog/include/spdlog/details/logger_impl.h

342 lines
7.9 KiB
C
Raw Normal View History

2016-09-02 22:06:00 +08:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include <memory>
#include <string>
// create logger with given name, sinks and the default pattern formatter
// all other ctors will call this one
2018-03-16 23:13:50 +08:00
template<class It>
2018-03-09 21:26:33 +08:00
inline spdlog::logger::logger(std::string logger_name, const It &begin, const It &end)
: name_(std::move(logger_name))
, sinks_(begin, end)
, formatter_(std::make_shared<pattern_formatter>("%+"))
, level_(level::info)
, flush_level_(level::off)
, last_err_time_(0)
, msg_counter_(1) // message counter will start from 1. 0-message id will be reserved for controll messages
2018-03-09 21:26:33 +08:00
{
err_handler_ = [this](const std::string &msg) { this->default_err_handler_(msg); };
2016-09-02 22:06:00 +08:00
}
// ctor with sinks as init list
2018-03-09 21:26:33 +08:00
inline spdlog::logger::logger(const std::string &logger_name, sinks_init_list sinks_list)
: logger(logger_name, sinks_list.begin(), sinks_list.end())
{
}
2016-09-02 22:06:00 +08:00
// ctor with single sink
2018-03-09 21:26:33 +08:00
inline spdlog::logger::logger(const std::string &logger_name, spdlog::sink_ptr single_sink)
: logger(logger_name, {std::move(single_sink)})
2017-05-21 06:56:56 +08:00
{
2018-03-09 21:26:33 +08:00
}
2016-09-02 22:06:00 +08:00
inline spdlog::logger::~logger() = default;
inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
{
formatter_ = std::move(msg_formatter);
2016-09-02 22:06:00 +08:00
}
2018-03-09 21:26:33 +08:00
inline void spdlog::logger::set_pattern(const std::string &pattern, pattern_time_type pattern_time)
2016-09-02 22:06:00 +08:00
{
formatter_ = std::make_shared<pattern_formatter>(pattern, pattern_time);
2016-09-02 22:06:00 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Args &... args)
2016-09-02 22:06:00 +08:00
{
2018-03-09 21:26:33 +08:00
if (!should_log(lvl))
{
2018-03-09 21:26:33 +08:00
return;
}
2016-09-02 22:06:00 +08:00
try
{
details::log_msg log_msg(&name_, lvl);
#if defined(SPDLOG_FMT_PRINTF)
fmt::printf(log_msg.raw, fmt, args...);
#else
2016-09-02 22:06:00 +08:00
log_msg.raw.write(fmt, args...);
#endif
sink_it_(log_msg);
2016-09-02 22:06:00 +08:00
}
2018-05-26 23:48:39 +08:00
SPDLOG_CATCH_AND_HANDLE
2016-09-02 22:06:00 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
2016-09-02 22:06:00 +08:00
{
2018-03-09 21:26:33 +08:00
if (!should_log(lvl))
{
2018-03-09 21:26:33 +08:00
return;
}
2016-09-02 22:06:00 +08:00
try
{
details::log_msg log_msg(&name_, lvl);
2016-09-02 22:06:00 +08:00
log_msg.raw << msg;
sink_it_(log_msg);
2016-09-02 22:06:00 +08:00
}
2018-05-26 23:48:39 +08:00
SPDLOG_CATCH_AND_HANDLE
2016-09-02 22:06:00 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename T>
inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
2016-09-02 22:06:00 +08:00
{
2018-03-09 21:26:33 +08:00
if (!should_log(lvl))
{
2018-03-09 21:26:33 +08:00
return;
}
2016-09-02 22:06:00 +08:00
try
{
details::log_msg log_msg(&name_, lvl);
2016-09-02 22:06:00 +08:00
log_msg.raw << msg;
sink_it_(log_msg);
2016-09-02 22:06:00 +08:00
}
2018-05-26 23:48:39 +08:00
SPDLOG_CATCH_AND_HANDLE
2016-09-02 22:06:00 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename Arg1, typename... Args>
inline void spdlog::logger::trace(const char *fmt, const Arg1 &arg1, const Args &... args)
2016-09-02 22:06:00 +08:00
{
log(level::trace, fmt, arg1, args...);
2016-09-02 22:06:00 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename Arg1, typename... Args>
inline void spdlog::logger::debug(const char *fmt, const Arg1 &arg1, const Args &... args)
2016-09-02 22:06:00 +08:00
{
log(level::debug, fmt, arg1, args...);
2016-09-02 22:06:00 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename Arg1, typename... Args>
inline void spdlog::logger::info(const char *fmt, const Arg1 &arg1, const Args &... args)
2016-09-02 22:06:00 +08:00
{
log(level::info, fmt, arg1, args...);
2016-09-02 22:06:00 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename Arg1, typename... Args>
inline void spdlog::logger::warn(const char *fmt, const Arg1 &arg1, const Args &... args)
2016-09-02 22:06:00 +08:00
{
log(level::warn, fmt, arg1, args...);
2016-09-02 22:06:00 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename Arg1, typename... Args>
inline void spdlog::logger::error(const char *fmt, const Arg1 &arg1, const Args &... args)
2017-06-17 22:05:25 +08:00
{
log(level::err, fmt, arg1, args...);
}
2018-03-16 23:35:56 +08:00
template<typename Arg1, typename... Args>
inline void spdlog::logger::critical(const char *fmt, const Arg1 &arg1, const Args &... args)
2017-06-17 22:05:25 +08:00
{
log(level::critical, fmt, arg1, args...);
}
2018-03-16 23:35:56 +08:00
template<typename T>
inline void spdlog::logger::trace(const T &msg)
2016-09-02 22:06:00 +08:00
{
log(level::trace, msg);
}
2018-03-16 23:35:56 +08:00
template<typename T>
inline void spdlog::logger::debug(const T &msg)
2016-09-02 22:06:00 +08:00
{
log(level::debug, msg);
}
2018-03-16 23:35:56 +08:00
template<typename T>
inline void spdlog::logger::info(const T &msg)
2016-09-02 22:06:00 +08:00
{
log(level::info, msg);
}
2018-03-16 23:35:56 +08:00
template<typename T>
inline void spdlog::logger::warn(const T &msg)
2016-09-02 22:06:00 +08:00
{
log(level::warn, msg);
}
2018-03-16 23:35:56 +08:00
template<typename T>
inline void spdlog::logger::error(const T &msg)
2017-06-17 22:05:25 +08:00
{
log(level::err, msg);
}
2018-03-16 23:35:56 +08:00
template<typename T>
inline void spdlog::logger::critical(const T &msg)
2017-06-17 22:05:25 +08:00
{
log(level::critical, msg);
}
2017-04-01 23:37:16 +08:00
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
#include <codecvt>
#include <locale>
2017-04-01 23:37:16 +08:00
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *msg)
2017-04-01 23:37:16 +08:00
{
2018-03-09 21:26:33 +08:00
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
2017-04-01 23:37:16 +08:00
2017-05-17 05:06:11 +08:00
log(lvl, conv.to_bytes(msg));
2017-04-01 23:37:16 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *fmt, const Args &... args)
2017-04-01 23:37:16 +08:00
{
2017-05-17 05:06:11 +08:00
fmt::WMemoryWriter wWriter;
2017-04-01 23:37:16 +08:00
2017-05-17 05:06:11 +08:00
wWriter.write(fmt, args...);
log(lvl, wWriter.c_str());
2017-04-01 23:37:16 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::trace(const wchar_t *fmt, const Args &... args)
2017-04-01 23:37:16 +08:00
{
2017-05-17 05:06:11 +08:00
log(level::trace, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::debug(const wchar_t *fmt, const Args &... args)
2017-04-01 23:37:16 +08:00
{
2017-05-17 05:06:11 +08:00
log(level::debug, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::info(const wchar_t *fmt, const Args &... args)
2017-04-01 23:37:16 +08:00
{
2017-05-17 05:06:11 +08:00
log(level::info, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::warn(const wchar_t *fmt, const Args &... args)
2017-04-01 23:37:16 +08:00
{
2017-05-17 05:06:11 +08:00
log(level::warn, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::error(const wchar_t *fmt, const Args &... args)
2017-04-01 23:37:16 +08:00
{
2017-05-17 05:06:11 +08:00
log(level::err, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 23:35:56 +08:00
template<typename... Args>
inline void spdlog::logger::critical(const wchar_t *fmt, const Args &... args)
2017-04-01 23:37:16 +08:00
{
2017-05-17 05:06:11 +08:00
log(level::critical, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2017-06-17 23:24:16 +08:00
2017-04-01 23:37:16 +08:00
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
2016-09-02 22:06:00 +08:00
//
// name and level
//
2018-03-09 21:26:33 +08:00
inline const std::string &spdlog::logger::name() const
2016-09-02 22:06:00 +08:00
{
return name_;
2016-09-02 22:06:00 +08:00
}
inline void spdlog::logger::set_level(spdlog::level::level_enum log_level)
{
level_.store(log_level);
2016-09-02 22:06:00 +08:00
}
inline void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler)
{
err_handler_ = std::move(err_handler);
2016-09-02 22:06:00 +08:00
}
inline spdlog::log_err_handler spdlog::logger::error_handler()
{
return err_handler_;
2016-09-02 22:06:00 +08:00
}
2018-04-14 08:34:57 +08:00
inline void spdlog::logger::flush()
{
2018-05-26 23:48:39 +08:00
try
{
flush_();
2018-05-26 23:48:39 +08:00
}
SPDLOG_CATCH_AND_HANDLE
2018-04-14 08:34:57 +08:00
}
2016-09-02 22:06:00 +08:00
inline void spdlog::logger::flush_on(level::level_enum log_level)
{
flush_level_.store(log_level);
2016-09-02 22:06:00 +08:00
}
inline bool spdlog::logger::should_flush_(const details::log_msg &msg)
2018-04-14 08:34:57 +08:00
{
auto flush_level = flush_level_.load(std::memory_order_relaxed);
2018-04-14 08:34:57 +08:00
return (msg.level >= flush_level) && (msg.level != level::off);
}
2016-09-02 22:06:00 +08:00
inline spdlog::level::level_enum spdlog::logger::level() const
{
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
2016-09-02 22:06:00 +08:00
}
inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) const
{
return msg_level >= level_.load(std::memory_order_relaxed);
2016-09-02 22:06:00 +08:00
}
//
// protected virtual called at end of each user log call (if enabled) by the line_logger
//
inline void spdlog::logger::sink_it_(details::log_msg &msg)
2016-09-02 22:06:00 +08:00
{
2017-05-21 07:45:08 +08:00
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
incr_msg_counter_(msg);
2017-05-21 07:45:08 +08:00
#endif
formatter_->format(msg);
for (auto &sink : sinks_)
2016-09-15 05:38:21 +08:00
{
2018-03-09 21:26:33 +08:00
if (sink->should_log(msg.level))
2016-09-15 05:38:21 +08:00
{
sink->log(msg);
}
}
2016-09-02 22:06:00 +08:00
if (should_flush_(msg))
{
2016-09-02 22:06:00 +08:00
flush();
}
2016-09-02 22:06:00 +08:00
}
inline void spdlog::logger::flush_()
2016-09-02 22:06:00 +08:00
{
for (auto &sink : sinks_)
{
2016-09-02 22:06:00 +08:00
sink->flush();
}
2016-09-02 22:06:00 +08:00
}
inline void spdlog::logger::default_err_handler_(const std::string &msg)
2016-09-02 22:06:00 +08:00
{
auto now = time(nullptr);
if (now - last_err_time_ < 60)
{
2016-09-02 22:06:00 +08:00
return;
}
last_err_time_ = now;
2016-09-02 22:06:00 +08:00
auto tm_time = details::os::localtime(now);
char date_buf[100];
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
2018-04-14 08:34:57 +08:00
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
2016-09-02 22:06:00 +08:00
}
2016-09-25 03:14:05 +08:00
inline void spdlog::logger::incr_msg_counter_(details::log_msg &msg)
2017-10-13 00:48:04 +08:00
{
msg.msg_id = msg_counter_.fetch_add(1, std::memory_order_relaxed);
2017-10-13 00:48:04 +08:00
}
2018-03-09 21:26:33 +08:00
inline const std::vector<spdlog::sink_ptr> &spdlog::logger::sinks() const
2016-09-25 03:14:05 +08:00
{
return sinks_;
2016-09-25 03:14:05 +08:00
}