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