Files
spdlog/include/spdlog/details/logger_impl.h

388 lines
9.0 KiB
C
Raw Normal View History

2016-09-02 17:06:00 +03:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include "spdlog/details/fmt_helper.h"
2016-09-02 17:06:00 +03:00
#include <memory>
#include <string>
// create logger with given name, sinks and the default pattern formatter
// all other ctors will call this one
template<typename It>
inline spdlog::logger::logger(std::string logger_name, It begin, It end)
: name_(std::move(logger_name))
, sinks_(begin, end)
, level_(level::info)
, flush_level_(level::off)
, last_err_time_(0)
2018-07-21 23:48:07 +03:00
, msg_counter_(1) // message counter will start from 1. 0-message id will be
// reserved for control messages
2018-03-09 15:26:33 +02:00
{
err_handler_ = [this](const std::string &msg) { this->default_err_handler_(msg); };
2016-09-02 17:06:00 +03:00
}
// ctor with sinks as init list
inline spdlog::logger::logger(std::string logger_name, sinks_init_list sinks_list)
: logger(std::move(logger_name), sinks_list.begin(), sinks_list.end())
2018-03-09 15:26:33 +02:00
{
}
2016-09-02 17:06:00 +03:00
// ctor with single sink
inline spdlog::logger::logger(std::string logger_name, spdlog::sink_ptr single_sink)
: logger(std::move(logger_name), {std::move(single_sink)})
2017-05-21 01:56:56 +03:00
{
2018-03-09 15:26:33 +02:00
}
2016-09-02 17:06:00 +03:00
inline spdlog::logger::~logger() = default;
2018-07-22 21:52:46 +03:00
inline void spdlog::logger::set_formatter(std::unique_ptr<spdlog::formatter> f)
2016-09-02 17:06:00 +03:00
{
2018-07-23 00:13:52 +03:00
for (auto &sink : sinks_)
{
sink->set_formatter(f->clone());
}
2016-09-02 17:06:00 +03:00
}
inline void spdlog::logger::set_pattern(std::string pattern, pattern_time_type time_type)
2016-09-02 17:06:00 +03:00
{
auto new_formatter = details::make_unique<spdlog::pattern_formatter>(std::move(pattern), time_type);
2018-09-26 15:53:54 +03:00
set_formatter(std::move(new_formatter));
2016-09-02 17:06:00 +03:00
}
2018-03-16 17:35:56 +02:00
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Args &... args)
2016-09-02 17:06:00 +03:00
{
2018-03-09 15:26:33 +02:00
if (!should_log(lvl))
{
2018-03-09 15:26:33 +02:00
return;
}
2016-09-02 17:06:00 +03:00
try
{
details::log_msg log_msg(&name_, lvl);
2018-06-12 18:48:22 +03:00
fmt::format_to(log_msg.raw, fmt, args...);
sink_it_(log_msg);
2016-09-02 17:06:00 +03:00
}
2018-05-26 18:48:39 +03:00
SPDLOG_CATCH_AND_HANDLE
2016-09-02 17:06:00 +03:00
}
2018-03-16 17:35:56 +02:00
inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
{
logn(lvl, msg, std::char_traits<char>::length(msg));
}
inline void spdlog::logger::logn(level::level_enum lvl, const char *msg, size_t msg_len)
2016-09-02 17:06:00 +03:00
{
2018-03-09 15:26:33 +02:00
if (!should_log(lvl))
{
2018-03-09 15:26:33 +02:00
return;
}
2016-09-02 17:06:00 +03:00
try
{
2018-08-25 17:55:31 +03:00
details::log_msg log_msg(&name_, lvl);
log_msg.c_string = fmt::string_view(msg, msg_len);
sink_it_(log_msg);
2016-09-02 17:06:00 +03:00
}
2018-05-26 18:48:39 +03:00
SPDLOG_CATCH_AND_HANDLE
2016-09-02 17:06:00 +03:00
}
2018-03-16 17:35:56 +02:00
template<typename T>
inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
2016-09-02 17:06:00 +03:00
{
2018-03-09 15:26:33 +02:00
if (!should_log(lvl))
{
2018-03-09 15:26:33 +02:00
return;
}
2016-09-02 17:06:00 +03:00
try
{
details::log_msg log_msg(&name_, lvl);
2018-06-12 18:48:22 +03:00
fmt::format_to(log_msg.raw, "{}", msg);
sink_it_(log_msg);
2016-09-02 17:06:00 +03:00
}
2018-05-26 18:48:39 +03:00
SPDLOG_CATCH_AND_HANDLE
2016-09-02 17:06:00 +03:00
}
2018-07-07 16:40:29 +03:00
template<typename... Args>
2018-07-07 16:29:05 +03:00
inline void spdlog::logger::trace(const char *fmt, const Args &... args)
2016-09-02 17:06:00 +03:00
{
2018-07-07 16:29:05 +03:00
log(level::trace, fmt, args...);
2016-09-02 17:06:00 +03:00
}
2018-07-07 16:40:29 +03:00
template<typename... Args>
2018-07-07 16:29:05 +03:00
inline void spdlog::logger::debug(const char *fmt, const Args &... args)
2016-09-02 17:06:00 +03:00
{
2018-07-07 16:29:05 +03:00
log(level::debug, fmt, args...);
2016-09-02 17:06:00 +03:00
}
2018-07-07 16:40:29 +03:00
template<typename... Args>
2018-07-07 16:29:05 +03:00
inline void spdlog::logger::info(const char *fmt, const Args &... args)
2016-09-02 17:06:00 +03:00
{
2018-07-07 16:29:05 +03:00
log(level::info, fmt, args...);
2016-09-02 17:06:00 +03:00
}
2018-07-07 16:40:29 +03:00
template<typename... Args>
2018-07-07 16:29:05 +03:00
inline void spdlog::logger::warn(const char *fmt, const Args &... args)
2016-09-02 17:06:00 +03:00
{
2018-07-07 16:29:05 +03:00
log(level::warn, fmt, args...);
2016-09-02 17:06:00 +03:00
}
2018-07-07 16:40:29 +03:00
template<typename... Args>
2018-07-07 16:29:05 +03:00
inline void spdlog::logger::error(const char *fmt, const Args &... args)
2017-06-17 16:05:25 +02:00
{
2018-07-07 16:29:05 +03:00
log(level::err, fmt, args...);
2017-06-17 16:05:25 +02:00
}
2018-07-07 16:40:29 +03:00
template<typename... Args>
2018-07-07 16:29:05 +03:00
inline void spdlog::logger::critical(const char *fmt, const Args &... args)
2017-06-17 16:05:25 +02:00
{
2018-07-07 16:29:05 +03:00
log(level::critical, fmt, args...);
2017-06-17 16:05:25 +02:00
}
2018-03-16 17:35:56 +02:00
template<typename T>
inline void spdlog::logger::trace(const T &msg)
2016-09-02 17:06:00 +03:00
{
log(level::trace, msg);
}
2018-03-16 17:35:56 +02:00
template<typename T>
inline void spdlog::logger::debug(const T &msg)
2016-09-02 17:06:00 +03:00
{
log(level::debug, msg);
}
2018-03-16 17:35:56 +02:00
template<typename T>
inline void spdlog::logger::info(const T &msg)
2016-09-02 17:06:00 +03:00
{
log(level::info, msg);
}
2018-03-16 17:35:56 +02:00
template<typename T>
inline void spdlog::logger::warn(const T &msg)
2016-09-02 17:06:00 +03:00
{
log(level::warn, msg);
}
2018-03-16 17:35:56 +02:00
template<typename T>
inline void spdlog::logger::error(const T &msg)
2017-06-17 16:05:25 +02:00
{
log(level::err, msg);
}
2018-03-16 17:35:56 +02:00
template<typename T>
inline void spdlog::logger::critical(const T &msg)
2017-06-17 16:05:25 +02:00
{
log(level::critical, msg);
}
2017-04-01 23:37:16 +08:00
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
2018-10-04 02:20:47 +03:00
inline void wbuf_to_utf8buf(const fmt::wmemory_buffer &wbuf, fmt::memory_buffer &target)
{
int wbuf_size = static_cast<int>(wbuf.size());
if (wbuf_size == 0)
{
return;
}
auto result_size = ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), wbuf_size, NULL, 0, NULL, NULL);
2018-10-04 02:10:46 +03:00
if (result_size > 0)
{
target.resize(result_size);
::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), wbuf_size, &target.data()[0], result_size, NULL, NULL);
}
else
{
throw spdlog::spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
}
}
2018-03-16 17:35:56 +02:00
template<typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *fmt, const Args &... args)
2018-07-10 23:53:00 +03:00
{
if (!should_log(lvl))
{
return;
}
try
{
2018-10-04 02:10:46 +03:00
// format to wmemory_buffer and convert to utf8
details::log_msg log_msg(&name_, lvl);
fmt::wmemory_buffer wbuf;
2018-10-04 02:10:46 +03:00
fmt::format_to(wbuf, fmt, args...);
wbuf_to_utf8buf(wbuf, log_msg.raw);
sink_it_(log_msg);
2018-07-10 23:53:00 +03:00
}
SPDLOG_CATCH_AND_HANDLE
2017-04-01 23:37:16 +08:00
}
2018-03-16 17:35:56 +02: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 00:06:11 +03:00
log(level::trace, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 17:35:56 +02: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 00:06:11 +03:00
log(level::debug, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 17:35:56 +02: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 00:06:11 +03:00
log(level::info, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 17:35:56 +02: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 00:06:11 +03:00
log(level::warn, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 17:35:56 +02: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 00:06:11 +03:00
log(level::err, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2018-03-16 17:35:56 +02: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 00:06:11 +03:00
log(level::critical, fmt, args...);
2017-04-01 23:37:16 +08:00
}
2017-06-17 17:24:16 +02:00
2017-04-01 23:37:16 +08:00
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
2016-09-02 17:06:00 +03:00
//
// name and level
//
2018-03-09 15:26:33 +02:00
inline const std::string &spdlog::logger::name() const
2016-09-02 17:06:00 +03:00
{
return name_;
2016-09-02 17:06:00 +03:00
}
inline void spdlog::logger::set_level(spdlog::level::level_enum log_level)
{
level_.store(log_level);
2016-09-02 17:06:00 +03:00
}
inline void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler)
{
err_handler_ = std::move(err_handler);
2016-09-02 17:06:00 +03:00
}
inline spdlog::log_err_handler spdlog::logger::error_handler()
{
return err_handler_;
2016-09-02 17:06:00 +03:00
}
2018-04-14 03:34:57 +03:00
inline void spdlog::logger::flush()
{
2018-05-26 18:48:39 +03:00
try
{
flush_();
2018-05-26 18:48:39 +03:00
}
SPDLOG_CATCH_AND_HANDLE
2018-04-14 03:34:57 +03:00
}
2016-09-02 17:06:00 +03:00
inline void spdlog::logger::flush_on(level::level_enum log_level)
{
flush_level_.store(log_level);
2016-09-02 17:06:00 +03:00
}
2018-08-25 17:35:20 +03:00
inline spdlog::level::level_enum spdlog::logger::flush_level() const
{
return static_cast<spdlog::level::level_enum>(flush_level_.load(std::memory_order_relaxed));
}
inline bool spdlog::logger::should_flush_(const details::log_msg &msg)
2018-04-14 03:34:57 +03:00
{
auto flush_level = flush_level_.load(std::memory_order_relaxed);
2018-04-14 03:34:57 +03:00
return (msg.level >= flush_level) && (msg.level != level::off);
}
2016-09-02 17:06:00 +03: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 17:06:00 +03: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 17:06:00 +03:00
}
//
2018-07-21 23:48:07 +03:00
// protected virtual called at end of each user log call (if enabled) by the
// line_logger
2016-09-02 17:06:00 +03:00
//
inline void spdlog::logger::sink_it_(details::log_msg &msg)
2016-09-02 17:06:00 +03:00
{
2017-05-21 02:45:08 +03:00
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
incr_msg_counter_(msg);
2017-05-21 02:45:08 +03:00
#endif
for (auto &sink : sinks_)
2016-09-15 00:38:21 +03:00
{
2018-03-09 15:26:33 +02:00
if (sink->should_log(msg.level))
2016-09-15 00:38:21 +03:00
{
sink->log(msg);
}
}
2016-09-02 17:06:00 +03:00
if (should_flush_(msg))
{
2016-09-02 17:06:00 +03:00
flush();
}
2016-09-02 17:06:00 +03:00
}
inline void spdlog::logger::flush_()
2016-09-02 17:06:00 +03:00
{
for (auto &sink : sinks_)
{
2016-09-02 17:06:00 +03:00
sink->flush();
}
2016-09-02 17:06:00 +03:00
}
inline void spdlog::logger::default_err_handler_(const std::string &msg)
2016-09-02 17:06:00 +03:00
{
auto now = time(nullptr);
if (now - last_err_time_ < 60)
{
2016-09-02 17:06:00 +03:00
return;
}
last_err_time_ = now;
2016-09-02 17:06:00 +03: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 03:34:57 +03:00
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
2016-09-02 17:06:00 +03:00
}
2016-09-24 15:14:05 -04:00
inline void spdlog::logger::incr_msg_counter_(details::log_msg &msg)
2017-10-12 19:48:04 +03:00
{
msg.msg_id = msg_counter_.fetch_add(1, std::memory_order_relaxed);
2017-10-12 19:48:04 +03:00
}
2018-03-09 15:26:33 +02:00
inline const std::vector<spdlog::sink_ptr> &spdlog::logger::sinks() const
2016-09-24 15:14:05 -04:00
{
return sinks_;
2016-09-24 15:14:05 -04:00
}
2018-07-21 23:48:07 +03:00
inline std::vector<spdlog::sink_ptr> &spdlog::logger::sinks()
{
return sinks_;
}
2018-08-25 17:35:20 +03:00
inline std::shared_ptr<spdlog::logger> spdlog::logger::clone(std::string logger_name)
{
auto cloned = std::make_shared<spdlog::logger>(std::move(logger_name), sinks_.begin(), sinks_.end());
cloned->set_level(this->level());
cloned->flush_on(this->flush_level());
cloned->set_error_handler(this->error_handler());
2018-08-29 17:21:38 +02:00
return cloned;
}