Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 08:52:25

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/async_logger.h>
0008 #endif
0009 
0010 #include <spdlog/details/thread_pool.h>
0011 #include <spdlog/sinks/sink.h>
0012 
0013 #include <memory>
0014 #include <string>
0015 
0016 SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
0017                                                  sinks_init_list sinks_list,
0018                                                  std::weak_ptr<details::thread_pool> tp,
0019                                                  async_overflow_policy overflow_policy)
0020     : async_logger(std::move(logger_name),
0021                    sinks_list.begin(),
0022                    sinks_list.end(),
0023                    std::move(tp),
0024                    overflow_policy) {}
0025 
0026 SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
0027                                                  sink_ptr single_sink,
0028                                                  std::weak_ptr<details::thread_pool> tp,
0029                                                  async_overflow_policy overflow_policy)
0030     : async_logger(
0031           std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {}
0032 
0033 // send the log message to the thread pool
0034 SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){
0035     SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
0036         pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
0037 }
0038 else {
0039     throw_spdlog_ex("async log: thread pool doesn't exist anymore");
0040 }
0041 }
0042 SPDLOG_LOGGER_CATCH(msg.source)
0043 }
0044 
0045 // send flush request to the thread pool
0046 SPDLOG_INLINE void spdlog::async_logger::flush_(){SPDLOG_TRY{auto pool_ptr = thread_pool_.lock();
0047 if (!pool_ptr) {
0048     throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
0049 }
0050 
0051 std::future<void> future = pool_ptr->post_flush(shared_from_this(), overflow_policy_);
0052 // Wait for the flush operation to complete.
0053 // This might throw exception if the flush message get dropped because of overflow.
0054 future.get();
0055 }
0056 SPDLOG_LOGGER_CATCH(source_loc())
0057 }
0058 
0059 //
0060 // backend functions - called from the thread pool to do the actual job
0061 //
0062 SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) {
0063     for (auto &sink : sinks_) {
0064         if (sink->should_log(msg.level)) {
0065             SPDLOG_TRY { sink->log(msg); }
0066             SPDLOG_LOGGER_CATCH(msg.source)
0067         }
0068     }
0069 
0070     if (should_flush_(msg)) {
0071         backend_flush_();
0072     }
0073 }
0074 
0075 SPDLOG_INLINE void spdlog::async_logger::backend_flush_() {
0076     for (auto &sink : sinks_) {
0077         SPDLOG_TRY { sink->flush(); }
0078         SPDLOG_LOGGER_CATCH(source_loc())
0079     }
0080 }
0081 
0082 SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name) {
0083     auto cloned = std::make_shared<spdlog::async_logger>(*this);
0084     cloned->name_ = std::move(new_name);
0085     return cloned;
0086 }