2019-06-04 05:09:16 +08:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
2019-05-12 01:06:17 +08:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-05-13 05:09:00 +08:00
|
|
|
#ifndef SPDLOG_HEADER_ONLY
|
|
|
|
#include "spdlog/logger.h"
|
|
|
|
#endif
|
|
|
|
|
2019-04-05 21:44:17 +08:00
|
|
|
#include "spdlog/sinks/sink.h"
|
2019-08-23 07:28:52 +08:00
|
|
|
#include "spdlog/sinks/backtrace-sink.h"
|
2019-04-05 21:44:17 +08:00
|
|
|
#include "spdlog/details/pattern_formatter.h"
|
|
|
|
|
2019-07-17 23:05:01 +08:00
|
|
|
#include <cstdio>
|
|
|
|
|
2019-05-12 05:22:39 +08:00
|
|
|
namespace spdlog {
|
2019-04-05 21:44:17 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
// public methods
|
|
|
|
SPDLOG_INLINE logger::logger(const logger &other)
|
|
|
|
: name_(other.name_)
|
|
|
|
, sinks_(other.sinks_)
|
|
|
|
, level_(other.level_.load(std::memory_order_relaxed))
|
|
|
|
, flush_level_(other.flush_level_.load(std::memory_order_relaxed))
|
|
|
|
, custom_err_handler_(other.custom_err_handler_)
|
2019-08-25 22:24:17 +08:00
|
|
|
, backtrace_sink_(other.backtrace_sink_)
|
2019-06-18 22:05:27 +08:00
|
|
|
{}
|
|
|
|
|
2019-08-25 22:24:17 +08:00
|
|
|
SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT :
|
|
|
|
name_(std::move(other.name_)),
|
|
|
|
sinks_(std::move(other.sinks_)),
|
|
|
|
level_(other.level_.load(std::memory_order_relaxed)),
|
|
|
|
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
|
|
|
|
custom_err_handler_(std::move(other.custom_err_handler_)),
|
|
|
|
backtrace_sink_(std::move(other.backtrace_sink_))
|
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
{}
|
|
|
|
|
2019-08-18 22:45:09 +08:00
|
|
|
SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT
|
2019-06-18 22:05:27 +08:00
|
|
|
{
|
|
|
|
this->swap(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-18 22:45:09 +08:00
|
|
|
SPDLOG_INLINE void logger::swap(spdlog::logger &other) SPDLOG_NOEXCEPT
|
2019-06-18 22:05:27 +08:00
|
|
|
{
|
|
|
|
name_.swap(other.name_);
|
|
|
|
sinks_.swap(other.sinks_);
|
|
|
|
|
|
|
|
// swap level_
|
|
|
|
auto tmp = other.level_.load();
|
|
|
|
tmp = level_.exchange(tmp);
|
|
|
|
other.level_.store(tmp);
|
|
|
|
|
|
|
|
// swap flush level_
|
|
|
|
tmp = other.flush_level_.load();
|
|
|
|
tmp = flush_level_.exchange(tmp);
|
|
|
|
other.flush_level_.store(tmp);
|
|
|
|
|
|
|
|
custom_err_handler_.swap(other.custom_err_handler_);
|
2019-08-25 22:24:17 +08:00
|
|
|
backtrace_sink_.swap(other.backtrace_sink_);
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SPDLOG_INLINE void swap(logger &a, logger &b)
|
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2019-07-07 17:55:56 +08:00
|
|
|
SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, string_view_t msg)
|
2019-06-18 22:05:27 +08:00
|
|
|
{
|
|
|
|
if (!should_log(lvl))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-17 21:01:30 +08:00
|
|
|
details::log_msg log_msg(loc, string_view_t(name_), lvl, msg);
|
|
|
|
sink_it_(log_msg);
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
|
|
|
|
2019-07-07 17:55:56 +08:00
|
|
|
SPDLOG_INLINE void logger::log(level::level_enum lvl, string_view_t msg)
|
2019-06-18 22:05:27 +08:00
|
|
|
{
|
|
|
|
log(source_loc{}, lvl, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const
|
|
|
|
{
|
|
|
|
return msg_level >= level_.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
|
|
|
|
{
|
2019-08-25 22:24:17 +08:00
|
|
|
if(backtrace_sink_)
|
|
|
|
{
|
|
|
|
auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
|
|
|
|
tracer->set_filter_level(log_level);
|
|
|
|
level_.store(level::trace);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level_.store(log_level);
|
|
|
|
}
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SPDLOG_INLINE level::level_enum logger::level() const
|
|
|
|
{
|
|
|
|
return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDLOG_INLINE const std::string &logger::name() const
|
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set formatting for the sinks in this logger.
|
|
|
|
// each sink will get a seperate instance of the formatter object.
|
|
|
|
SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
|
|
|
|
{
|
|
|
|
for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
|
2019-04-05 21:44:17 +08:00
|
|
|
{
|
2019-06-18 22:05:27 +08:00
|
|
|
if (std::next(it) == sinks_.end())
|
2019-06-06 23:19:36 +08:00
|
|
|
{
|
2019-06-18 22:05:27 +08:00
|
|
|
// last element - we can be move it.
|
|
|
|
(*it)->set_formatter(std::move(f));
|
2019-06-06 23:19:36 +08:00
|
|
|
}
|
2019-06-18 22:05:27 +08:00
|
|
|
else
|
2019-06-16 00:46:41 +08:00
|
|
|
{
|
2019-06-18 22:05:27 +08:00
|
|
|
(*it)->set_formatter(f->clone());
|
2019-06-16 00:46:41 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
2019-06-16 00:46:41 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
|
|
|
|
{
|
|
|
|
auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
|
|
|
|
set_formatter(std::move(new_formatter));
|
|
|
|
}
|
2019-04-05 21:44:17 +08:00
|
|
|
|
2019-08-25 22:24:17 +08:00
|
|
|
// create new backtrace sink and move to it all our child sinks
|
2019-08-23 19:47:40 +08:00
|
|
|
SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages)
|
2019-08-23 07:28:52 +08:00
|
|
|
{
|
2019-08-25 22:24:17 +08:00
|
|
|
if (!backtrace_sink_)
|
|
|
|
{
|
|
|
|
auto new_backtrace_sink = new spdlog::sinks::backtrace_sink_mt(level(), n_messages);
|
|
|
|
new_backtrace_sink->set_sinks(std::move(sinks_));
|
|
|
|
backtrace_sink_.reset(new_backtrace_sink);
|
|
|
|
sinks().push_back(backtrace_sink_);
|
|
|
|
assert(sinks().size() == 1);
|
|
|
|
level_.store(level::trace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore orig sinks and level and delete the backtrace sink
|
|
|
|
SPDLOG_INLINE void logger::disable_backtrace()
|
|
|
|
{
|
|
|
|
if (backtrace_sink_)
|
2019-08-23 08:17:45 +08:00
|
|
|
{
|
2019-08-25 22:24:17 +08:00
|
|
|
auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
|
|
|
|
sinks_ = std::move(tracer->sinks());
|
|
|
|
level_.store(tracer->get_filter_level());
|
|
|
|
backtrace_sink_.reset();
|
2019-08-23 08:17:45 +08:00
|
|
|
}
|
2019-08-23 07:28:52 +08:00
|
|
|
}
|
|
|
|
|
2019-08-23 19:47:40 +08:00
|
|
|
SPDLOG_INLINE void logger::dump_backtrace()
|
|
|
|
{
|
2019-08-25 22:24:17 +08:00
|
|
|
if (backtrace_sink_)
|
2019-08-23 19:47:40 +08:00
|
|
|
{
|
2019-08-25 22:24:17 +08:00
|
|
|
auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
|
2019-08-25 22:44:08 +08:00
|
|
|
tracer->dump_backtrace(name());
|
2019-08-23 19:47:40 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-23 08:17:45 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
// flush functions
|
|
|
|
SPDLOG_INLINE void logger::flush()
|
|
|
|
{
|
2019-07-17 21:01:30 +08:00
|
|
|
flush_();
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
2019-04-05 21:44:17 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
SPDLOG_INLINE void logger::flush_on(level::level_enum log_level)
|
|
|
|
{
|
|
|
|
flush_level_.store(log_level);
|
|
|
|
}
|
2019-04-05 21:44:17 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
SPDLOG_INLINE level::level_enum logger::flush_level() const
|
|
|
|
{
|
|
|
|
return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
|
|
|
|
}
|
2019-04-05 21:44:17 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
// sinks
|
|
|
|
SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const
|
|
|
|
{
|
|
|
|
return sinks_;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
|
|
|
|
{
|
|
|
|
return sinks_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// error handler
|
|
|
|
SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
|
|
|
|
{
|
|
|
|
custom_err_handler_ = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new logger with same sinks and configuration.
|
|
|
|
SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
|
|
|
|
{
|
|
|
|
auto cloned = std::make_shared<logger>(*this);
|
|
|
|
cloned->name_ = std::move(logger_name);
|
|
|
|
return cloned;
|
|
|
|
}
|
|
|
|
|
|
|
|
// protected methods
|
2019-08-23 00:57:59 +08:00
|
|
|
SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
|
2019-06-18 22:05:27 +08:00
|
|
|
{
|
|
|
|
for (auto &sink : sinks_)
|
|
|
|
{
|
|
|
|
if (sink->should_log(msg.level))
|
2019-04-05 21:44:17 +08:00
|
|
|
{
|
2019-08-19 16:31:33 +08:00
|
|
|
SPDLOG_TRY
|
2019-07-17 21:01:30 +08:00
|
|
|
{
|
|
|
|
sink->log(msg);
|
|
|
|
}
|
|
|
|
SPDLOG_LOGGER_CATCH()
|
2019-04-05 21:44:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
if (should_flush_(msg))
|
2019-06-16 00:46:41 +08:00
|
|
|
{
|
2019-06-18 22:05:27 +08:00
|
|
|
flush_();
|
2019-06-16 00:46:41 +08:00
|
|
|
}
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
2019-06-16 00:46:41 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
SPDLOG_INLINE void logger::flush_()
|
|
|
|
{
|
|
|
|
for (auto &sink : sinks_)
|
2019-06-16 00:46:41 +08:00
|
|
|
{
|
2019-08-19 16:31:33 +08:00
|
|
|
SPDLOG_TRY
|
2019-07-17 21:01:30 +08:00
|
|
|
{
|
|
|
|
sink->flush();
|
|
|
|
}
|
|
|
|
SPDLOG_LOGGER_CATCH()
|
2019-06-16 00:46:41 +08:00
|
|
|
}
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
2019-06-16 00:46:41 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
|
|
|
|
{
|
|
|
|
auto flush_level = flush_level_.load(std::memory_order_relaxed);
|
|
|
|
return (msg.level >= flush_level) && (msg.level != level::off);
|
|
|
|
}
|
2019-06-16 00:46:41 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
|
|
|
{
|
2019-07-17 23:05:01 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
if (custom_err_handler_)
|
2019-04-05 21:44:17 +08:00
|
|
|
{
|
2019-06-18 22:05:27 +08:00
|
|
|
custom_err_handler_(msg);
|
2019-04-05 21:44:17 +08:00
|
|
|
}
|
2019-06-18 22:05:27 +08:00
|
|
|
else
|
2019-04-05 21:44:17 +08:00
|
|
|
{
|
2019-07-17 23:05:01 +08:00
|
|
|
using std::chrono::system_clock;
|
|
|
|
static std::mutex mutex;
|
|
|
|
static std::chrono::system_clock::time_point last_report_time;
|
|
|
|
static size_t err_counter = 0;
|
|
|
|
std::lock_guard<std::mutex> lk{mutex};
|
|
|
|
auto now = system_clock::now();
|
|
|
|
err_counter++;
|
2019-07-18 19:26:36 +08:00
|
|
|
if (now - last_report_time < std::chrono::seconds(1))
|
2019-07-17 23:05:01 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last_report_time = now;
|
|
|
|
auto tm_time = details::os::localtime(system_clock::to_time_t(now));
|
2019-06-18 22:05:27 +08:00
|
|
|
char date_buf[64];
|
|
|
|
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
2019-07-17 23:05:01 +08:00
|
|
|
fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
|
2019-04-05 21:44:17 +08:00
|
|
|
}
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
2019-06-19 06:24:06 +08:00
|
|
|
} // namespace spdlog
|