File indexing completed on 2025-07-06 08:52:53
0001
0002
0003
0004 #pragma once
0005
0006 #ifndef SPDLOG_HEADER_ONLY
0007 #include <spdlog/logger.h>
0008 #endif
0009
0010 #include <spdlog/details/backtracer.h>
0011 #include <spdlog/pattern_formatter.h>
0012 #include <spdlog/sinks/sink.h>
0013
0014 #include <cstdio>
0015
0016 namespace spdlog {
0017
0018
0019 SPDLOG_INLINE logger::logger(const logger &other)
0020 : name_(other.name_),
0021 sinks_(other.sinks_),
0022 level_(other.level_.load(std::memory_order_relaxed)),
0023 flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
0024 custom_err_handler_(other.custom_err_handler_),
0025 tracer_(other.tracer_) {}
0026
0027 SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT
0028 : name_(std::move(other.name_)),
0029 sinks_(std::move(other.sinks_)),
0030 level_(other.level_.load(std::memory_order_relaxed)),
0031 flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
0032 custom_err_handler_(std::move(other.custom_err_handler_)),
0033 tracer_(std::move(other.tracer_))
0034
0035 {}
0036
0037 SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT {
0038 this->swap(other);
0039 return *this;
0040 }
0041
0042 SPDLOG_INLINE void logger::swap(spdlog::logger &other) SPDLOG_NOEXCEPT {
0043 name_.swap(other.name_);
0044 sinks_.swap(other.sinks_);
0045
0046
0047 auto other_level = other.level_.load();
0048 auto my_level = level_.exchange(other_level);
0049 other.level_.store(my_level);
0050
0051
0052 other_level = other.flush_level_.load();
0053 my_level = flush_level_.exchange(other_level);
0054 other.flush_level_.store(my_level);
0055
0056 custom_err_handler_.swap(other.custom_err_handler_);
0057 std::swap(tracer_, other.tracer_);
0058 }
0059
0060 SPDLOG_INLINE void swap(logger &a, logger &b) { a.swap(b); }
0061
0062 SPDLOG_INLINE void logger::set_level(level::level_enum log_level) { level_.store(log_level); }
0063
0064 SPDLOG_INLINE level::level_enum logger::level() const {
0065 return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
0066 }
0067
0068 SPDLOG_INLINE const std::string &logger::name() const { return name_; }
0069
0070
0071
0072 SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f) {
0073 for (auto it = sinks_.begin(); it != sinks_.end(); ++it) {
0074 if (std::next(it) == sinks_.end()) {
0075
0076 (*it)->set_formatter(std::move(f));
0077 break;
0078 } else {
0079 (*it)->set_formatter(f->clone());
0080 }
0081 }
0082 }
0083
0084 SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type) {
0085 auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
0086 set_formatter(std::move(new_formatter));
0087 }
0088
0089
0090 SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages) { tracer_.enable(n_messages); }
0091
0092
0093 SPDLOG_INLINE void logger::disable_backtrace() { tracer_.disable(); }
0094
0095 SPDLOG_INLINE void logger::dump_backtrace() { dump_backtrace_(); }
0096
0097
0098 SPDLOG_INLINE void logger::flush() { flush_(); }
0099
0100 SPDLOG_INLINE void logger::flush_on(level::level_enum log_level) { flush_level_.store(log_level); }
0101
0102 SPDLOG_INLINE level::level_enum logger::flush_level() const {
0103 return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
0104 }
0105
0106
0107 SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const { return sinks_; }
0108
0109 SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks() { return sinks_; }
0110
0111
0112 SPDLOG_INLINE void logger::set_error_handler(err_handler handler) {
0113 custom_err_handler_ = std::move(handler);
0114 }
0115
0116
0117 SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name) {
0118 auto cloned = std::make_shared<logger>(*this);
0119 cloned->name_ = std::move(logger_name);
0120 return cloned;
0121 }
0122
0123
0124 SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg,
0125 bool log_enabled,
0126 bool traceback_enabled) {
0127 if (log_enabled) {
0128 sink_it_(log_msg);
0129 }
0130 if (traceback_enabled) {
0131 tracer_.push_back(log_msg);
0132 }
0133 }
0134
0135 SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) {
0136 for (auto &sink : sinks_) {
0137 if (sink->should_log(msg.level)) {
0138 SPDLOG_TRY { sink->log(msg); }
0139 SPDLOG_LOGGER_CATCH(msg.source)
0140 }
0141 }
0142
0143 if (should_flush_(msg)) {
0144 flush_();
0145 }
0146 }
0147
0148 SPDLOG_INLINE void logger::flush_() {
0149 for (auto &sink : sinks_) {
0150 SPDLOG_TRY { sink->flush(); }
0151 SPDLOG_LOGGER_CATCH(source_loc())
0152 }
0153 }
0154
0155 SPDLOG_INLINE void logger::dump_backtrace_() {
0156 using details::log_msg;
0157 if (tracer_.enabled() && !tracer_.empty()) {
0158 sink_it_(
0159 log_msg{name(), level::info, "****************** Backtrace Start ******************"});
0160 tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); });
0161 sink_it_(
0162 log_msg{name(), level::info, "****************** Backtrace End ********************"});
0163 }
0164 }
0165
0166 SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg) {
0167 auto flush_level = flush_level_.load(std::memory_order_relaxed);
0168 return (msg.level >= flush_level) && (msg.level != level::off);
0169 }
0170
0171 SPDLOG_INLINE void logger::err_handler_(const std::string &msg) {
0172 if (custom_err_handler_) {
0173 custom_err_handler_(msg);
0174 } else {
0175 using std::chrono::system_clock;
0176 static std::mutex mutex;
0177 static std::chrono::system_clock::time_point last_report_time;
0178 static size_t err_counter = 0;
0179 std::lock_guard<std::mutex> lk{mutex};
0180 auto now = system_clock::now();
0181 err_counter++;
0182 if (now - last_report_time < std::chrono::seconds(1)) {
0183 return;
0184 }
0185 last_report_time = now;
0186 auto tm_time = details::os::localtime(system_clock::to_time_t(now));
0187 char date_buf[64];
0188 std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
0189 #if defined(USING_R) && defined(R_R_H)
0190 REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(),
0191 msg.c_str());
0192 #else
0193 std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf,
0194 name().c_str(), msg.c_str());
0195 #endif
0196 }
0197 }
0198 }