Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:43

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 #ifndef SPDLOG_HEADER_ONLY
0007 #    include <spdlog/logger.h>
0008 #endif
0009 
0010 #include <spdlog/sinks/sink.h>
0011 #include <spdlog/details/backtracer.h>
0012 #include <spdlog/pattern_formatter.h>
0013 
0014 #include <cstdio>
0015 
0016 namespace spdlog {
0017 
0018 // public methods
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 
0028 SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT : 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 {
0039     this->swap(other);
0040     return *this;
0041 }
0042 
0043 SPDLOG_INLINE void logger::swap(spdlog::logger &other) SPDLOG_NOEXCEPT
0044 {
0045     name_.swap(other.name_);
0046     sinks_.swap(other.sinks_);
0047 
0048     // swap level_
0049     auto other_level = other.level_.load();
0050     auto my_level = level_.exchange(other_level);
0051     other.level_.store(my_level);
0052 
0053     // swap flush level_
0054     other_level = other.flush_level_.load();
0055     my_level = flush_level_.exchange(other_level);
0056     other.flush_level_.store(my_level);
0057 
0058     custom_err_handler_.swap(other.custom_err_handler_);
0059     std::swap(tracer_, other.tracer_);
0060 }
0061 
0062 SPDLOG_INLINE void swap(logger &a, logger &b)
0063 {
0064     a.swap(b);
0065 }
0066 
0067 SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
0068 {
0069     level_.store(log_level);
0070 }
0071 
0072 SPDLOG_INLINE level::level_enum logger::level() const
0073 {
0074     return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
0075 }
0076 
0077 SPDLOG_INLINE const std::string &logger::name() const
0078 {
0079     return name_;
0080 }
0081 
0082 // set formatting for the sinks in this logger.
0083 // each sink will get a separate instance of the formatter object.
0084 SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
0085 {
0086     for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
0087     {
0088         if (std::next(it) == sinks_.end())
0089         {
0090             // last element - we can be move it.
0091             (*it)->set_formatter(std::move(f));
0092             break; // to prevent clang-tidy warning
0093         }
0094         else
0095         {
0096             (*it)->set_formatter(f->clone());
0097         }
0098     }
0099 }
0100 
0101 SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
0102 {
0103     auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
0104     set_formatter(std::move(new_formatter));
0105 }
0106 
0107 // create new backtrace sink and move to it all our child sinks
0108 SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages)
0109 {
0110     tracer_.enable(n_messages);
0111 }
0112 
0113 // restore orig sinks and level and delete the backtrace sink
0114 SPDLOG_INLINE void logger::disable_backtrace()
0115 {
0116     tracer_.disable();
0117 }
0118 
0119 SPDLOG_INLINE void logger::dump_backtrace()
0120 {
0121     dump_backtrace_();
0122 }
0123 
0124 // flush functions
0125 SPDLOG_INLINE void logger::flush()
0126 {
0127     flush_();
0128 }
0129 
0130 SPDLOG_INLINE void logger::flush_on(level::level_enum log_level)
0131 {
0132     flush_level_.store(log_level);
0133 }
0134 
0135 SPDLOG_INLINE level::level_enum logger::flush_level() const
0136 {
0137     return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
0138 }
0139 
0140 // sinks
0141 SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const
0142 {
0143     return sinks_;
0144 }
0145 
0146 SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
0147 {
0148     return sinks_;
0149 }
0150 
0151 // error handler
0152 SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
0153 {
0154     custom_err_handler_ = std::move(handler);
0155 }
0156 
0157 // create new logger with same sinks and configuration.
0158 SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
0159 {
0160     auto cloned = std::make_shared<logger>(*this);
0161     cloned->name_ = std::move(logger_name);
0162     return cloned;
0163 }
0164 
0165 // protected methods
0166 SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled)
0167 {
0168     if (log_enabled)
0169     {
0170         sink_it_(log_msg);
0171     }
0172     if (traceback_enabled)
0173     {
0174         tracer_.push_back(log_msg);
0175     }
0176 }
0177 
0178 SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
0179 {
0180     for (auto &sink : sinks_)
0181     {
0182         if (sink->should_log(msg.level))
0183         {
0184             SPDLOG_TRY
0185             {
0186                 sink->log(msg);
0187             }
0188             SPDLOG_LOGGER_CATCH(msg.source)
0189         }
0190     }
0191 
0192     if (should_flush_(msg))
0193     {
0194         flush_();
0195     }
0196 }
0197 
0198 SPDLOG_INLINE void logger::flush_()
0199 {
0200     for (auto &sink : sinks_)
0201     {
0202         SPDLOG_TRY
0203         {
0204             sink->flush();
0205         }
0206         SPDLOG_LOGGER_CATCH(source_loc())
0207     }
0208 }
0209 
0210 SPDLOG_INLINE void logger::dump_backtrace_()
0211 {
0212     using details::log_msg;
0213     if (tracer_.enabled())
0214     {
0215         sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
0216         tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); });
0217         sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"});
0218     }
0219 }
0220 
0221 SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
0222 {
0223     auto flush_level = flush_level_.load(std::memory_order_relaxed);
0224     return (msg.level >= flush_level) && (msg.level != level::off);
0225 }
0226 
0227 SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
0228 {
0229     if (custom_err_handler_)
0230     {
0231         custom_err_handler_(msg);
0232     }
0233     else
0234     {
0235         using std::chrono::system_clock;
0236         static std::mutex mutex;
0237         static std::chrono::system_clock::time_point last_report_time;
0238         static size_t err_counter = 0;
0239         std::lock_guard<std::mutex> lk{mutex};
0240         auto now = system_clock::now();
0241         err_counter++;
0242         if (now - last_report_time < std::chrono::seconds(1))
0243         {
0244             return;
0245         }
0246         last_report_time = now;
0247         auto tm_time = details::os::localtime(system_clock::to_time_t(now));
0248         char date_buf[64];
0249         std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
0250 #if defined(USING_R) && defined(R_R_H) // if in R environment
0251         REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
0252 #else
0253         std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
0254 #endif
0255     }
0256 }
0257 } // namespace spdlog