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-29 05:05:17 +08:00
|
|
|
#include "spdlog/logger.h"
|
2019-04-05 21:44:17 +08:00
|
|
|
|
2019-07-17 23:05:01 +08:00
|
|
|
#include <cstdio>
|
2023-09-25 01:26:32 +08:00
|
|
|
#include <mutex>
|
2019-07-17 23:05:01 +08:00
|
|
|
|
2023-09-29 05:20:26 +08:00
|
|
|
#include "spdlog/pattern_formatter.h"
|
|
|
|
#include "spdlog/sinks/sink.h"
|
|
|
|
|
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
|
2023-09-25 01:43:14 +08:00
|
|
|
logger::logger(const logger &other) noexcept
|
2023-09-25 21:19:10 +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-06-18 22:05:27 +08:00
|
|
|
|
2023-09-25 01:43:14 +08:00
|
|
|
logger::logger(logger &&other) noexcept
|
2023-09-25 21:19:10 +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
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
void logger::set_level(level level) { level_.store(level); }
|
2019-06-18 22:05:27 +08:00
|
|
|
|
2023-09-25 11:15:14 +08:00
|
|
|
level logger::log_level() const { return level_.load(std::memory_order_relaxed); }
|
2023-09-25 07:35:55 +08:00
|
|
|
|
|
|
|
const std::string &logger::name() const { return name_; }
|
2019-06-18 22:05:27 +08:00
|
|
|
|
|
|
|
// set formatting for the sinks in this logger.
|
2019-10-19 15:42:28 +08:00
|
|
|
// each sink will get a separate instance of the formatter object.
|
2023-09-25 07:35:55 +08:00
|
|
|
void logger::set_formatter(std::unique_ptr<formatter> f) {
|
|
|
|
for (auto it = sinks_.begin(); it != sinks_.end(); ++it) {
|
|
|
|
if (std::next(it) == sinks_.end()) {
|
2023-10-01 23:11:02 +08:00
|
|
|
// last element - we can move it.
|
2019-06-18 22:05:27 +08:00
|
|
|
(*it)->set_formatter(std::move(f));
|
2023-09-25 21:40:05 +08:00
|
|
|
break; // to prevent clang-tidy warning
|
2019-06-16 00:46:41 +08:00
|
|
|
}
|
2024-11-30 17:36:43 +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
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
void logger::set_pattern(std::string pattern, pattern_time_type time_type) {
|
2023-09-01 23:22:40 +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
|
2023-09-25 07:35:55 +08:00
|
|
|
void logger::flush() { flush_(); }
|
2019-04-05 21:44:17 +08:00
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
void logger::flush_on(level level) { flush_level_.store(level); }
|
2019-04-05 21:44:17 +08:00
|
|
|
|
2023-09-25 11:18:00 +08:00
|
|
|
level logger::flush_level() const { return flush_level_.load(std::memory_order_relaxed); }
|
2019-04-05 21:44:17 +08:00
|
|
|
|
2019-06-18 22:05:27 +08:00
|
|
|
// sinks
|
2023-09-25 07:35:55 +08:00
|
|
|
const std::vector<sink_ptr> &logger::sinks() const { return sinks_; }
|
2019-06-18 22:05:27 +08:00
|
|
|
|
2023-09-25 07:35:55 +08:00
|
|
|
std::vector<sink_ptr> &logger::sinks() { return sinks_; }
|
2019-06-18 22:05:27 +08:00
|
|
|
|
|
|
|
// error handler
|
2023-09-25 07:35:55 +08:00
|
|
|
void logger::set_error_handler(err_handler handler) { 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.
|
2023-09-25 07:35:55 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-14 19:58:16 +08:00
|
|
|
// private/protected methods
|
2023-09-25 07:35:55 +08:00
|
|
|
void logger::flush_() {
|
|
|
|
for (auto &sink : sinks_) {
|
2024-12-02 05:59:09 +08:00
|
|
|
try {
|
|
|
|
sink->flush();
|
|
|
|
}
|
2021-08-17 21:26:59 +08:00
|
|
|
SPDLOG_LOGGER_CATCH(source_loc())
|
2019-06-16 00:46:41 +08:00
|
|
|
}
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
2019-06-16 00:46:41 +08:00
|
|
|
|
2024-11-29 22:11:13 +08:00
|
|
|
bool logger::should_flush_(const details::log_msg &msg) const {
|
2019-06-18 22:05:27 +08:00
|
|
|
auto flush_level = flush_level_.load(std::memory_order_relaxed);
|
2023-09-23 22:59:51 +08:00
|
|
|
return (msg.log_level >= flush_level) && (msg.log_level != level::off);
|
2019-06-18 22:05:27 +08:00
|
|
|
}
|
2019-06-16 00:46:41 +08:00
|
|
|
|
2024-01-14 19:56:55 +08:00
|
|
|
void logger::err_handler_(const std::string &msg) {
|
|
|
|
if (custom_err_handler_) {
|
|
|
|
custom_err_handler_(msg);
|
|
|
|
} else {
|
|
|
|
using std::chrono::system_clock;
|
|
|
|
auto now = system_clock::now();
|
|
|
|
auto tm_time = details::os::localtime(system_clock::to_time_t(now));
|
|
|
|
char date_buf[64];
|
|
|
|
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
2023-09-25 21:40:05 +08:00
|
|
|
#if defined(USING_R) && defined(R_R_H) // if in R environment
|
2024-01-14 19:56:55 +08:00
|
|
|
REprintf("[*** LOG ERROR ***] [%s] [%s] %s\n", date_buf, name().c_str(), msg.c_str());
|
2020-09-30 22:29:44 +08:00
|
|
|
#else
|
2024-01-14 19:56:55 +08:00
|
|
|
std::fprintf(stderr, "[*** LOG ERROR ***] [%s] [%s] %s\n", 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
|
|
|
}
|
2024-01-14 19:56:55 +08:00
|
|
|
}
|
2023-09-25 21:40:05 +08:00
|
|
|
} // namespace spdlog
|