spdlog/src/logger.cpp

160 lines
4.1 KiB
C++
Raw Normal View History

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)
2023-09-24 18:27:26 +08:00
#include <spdlog/logger.h>
#include <spdlog/sinks/sink.h>
#include <spdlog/pattern_formatter.h>
2019-04-05 21:44:17 +08:00
#include <cstdio>
2023-09-25 01:26:32 +08:00
#include <mutex>
namespace spdlog {
2019-04-05 21:44:17 +08:00
2019-06-18 22:05:27 +08:00
// public methods
logger::logger(const logger &other) noexcept
2019-06-18 22:05:27 +08:00
: 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-09-05 06:25:00 +08:00
{}
2019-06-18 22:05:27 +08:00
logger::logger(logger &&other) noexcept
2023-09-16 02:10:47 +08:00
: 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_))
2019-06-18 22:05:27 +08:00
{}
void logger::set_level(level level)
2019-06-18 22:05:27 +08:00
{
level_.store(level);
2019-06-18 22:05:27 +08:00
}
level logger::log_level() const
2019-06-18 22:05:27 +08:00
{
return static_cast<level>(level_.load(std::memory_order_relaxed));
2019-06-18 22:05:27 +08:00
}
const std::string &logger::name() const
2019-06-18 22:05:27 +08:00
{
return name_;
}
// set formatting for the sinks in this logger.
// each sink will get a separate instance of the formatter object.
void logger::set_formatter(std::unique_ptr<formatter> f)
2019-06-18 22:05:27 +08:00
{
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-18 22:05:27 +08:00
// last element - we can be move it.
(*it)->set_formatter(std::move(f));
2020-02-29 19:20:26 +08:00
break; // to prevent clang-tidy warning
}
2019-06-18 22:05:27 +08:00
else
{
2019-06-18 22:05:27 +08:00
(*it)->set_formatter(f->clone());
}
}
2019-06-18 22:05:27 +08:00
}
void logger::set_pattern(std::string pattern, pattern_time_type time_type)
2019-06-18 22:05:27 +08:00
{
auto new_formatter = std::make_unique<pattern_formatter>(std::move(pattern), time_type);
2019-06-18 22:05:27 +08:00
set_formatter(std::move(new_formatter));
}
2019-04-05 21:44:17 +08:00
2019-06-18 22:05:27 +08:00
// flush functions
void logger::flush()
2019-06-18 22:05:27 +08:00
{
flush_();
2019-06-18 22:05:27 +08:00
}
2019-04-05 21:44:17 +08:00
void logger::flush_on(level level)
2019-06-18 22:05:27 +08:00
{
flush_level_.store(level);
2019-06-18 22:05:27 +08:00
}
2019-04-05 21:44:17 +08:00
level logger::flush_level() const
2019-06-18 22:05:27 +08:00
{
return static_cast<level>(flush_level_.load(std::memory_order_relaxed));
2019-06-18 22:05:27 +08:00
}
2019-04-05 21:44:17 +08:00
2019-06-18 22:05:27 +08:00
// sinks
const std::vector<sink_ptr> &logger::sinks() const
2019-06-18 22:05:27 +08:00
{
return sinks_;
}
std::vector<sink_ptr> &logger::sinks()
2019-06-18 22:05:27 +08:00
{
return sinks_;
}
// error handler
void logger::set_error_handler(err_handler handler)
2019-06-18 22:05:27 +08:00
{
2019-11-08 22:20:41 +08:00
custom_err_handler_ = std::move(handler);
2019-06-18 22:05:27 +08:00
}
2019-08-29 06:05:23 +08:00
// create new logger with same sinks and configuration.
std::shared_ptr<logger> logger::clone(std::string logger_name)
2019-08-29 06:05:23 +08:00
{
auto cloned = std::make_shared<logger>(*this);
cloned->name_ = std::move(logger_name);
return cloned;
}
void logger::flush_()
2019-06-18 22:05:27 +08:00
{
for (auto &sink : sinks_)
{
SPDLOG_TRY
{
sink->flush();
}
SPDLOG_LOGGER_CATCH(source_loc())
}
2019-06-18 22:05:27 +08:00
}
bool logger::should_flush_(const details::log_msg &msg)
2019-06-18 22:05:27 +08:00
{
auto flush_level = flush_level_.load(std::memory_order_relaxed);
return (msg.log_level >= flush_level) && (msg.log_level != level::off);
2019-06-18 22:05:27 +08:00
}
void logger::err_handler_(const std::string &msg)
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
{
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))
{
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);
2020-09-30 22:29:44 +08:00
#if defined(USING_R) && defined(R_R_H) // if in R environment
REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), msg.c_str());
2020-09-30 22:29:44 +08:00
#else
std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), msg.c_str());
2020-09-29 07:20:43 +08:00
#endif
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