| 1 | // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. |
| 2 | // Distributed under the MIT License (http://opensource.org/licenses/MIT) |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #ifndef SPDLOG_HEADER_ONLY |
| 7 | #include <spdlog/async_logger.h> |
| 8 | #endif |
| 9 | |
| 10 | #include <spdlog/details/thread_pool.h> |
| 11 | #include <spdlog/sinks/sink.h> |
| 12 | |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | |
| 16 | SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name, |
| 17 | sinks_init_list sinks_list, |
| 18 | std::weak_ptr<details::thread_pool> tp, |
| 19 | async_overflow_policy overflow_policy) |
| 20 | : async_logger(std::move(logger_name), |
| 21 | sinks_list.begin(), |
| 22 | sinks_list.end(), |
| 23 | std::move(tp), |
| 24 | overflow_policy) {} |
| 25 | |
| 26 | SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name, |
| 27 | sink_ptr single_sink, |
| 28 | std::weak_ptr<details::thread_pool> tp, |
| 29 | async_overflow_policy overflow_policy) |
| 30 | : async_logger( |
| 31 | std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {} |
| 32 | |
| 33 | // send the log message to the thread pool |
| 34 | SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){ |
| 35 | SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){ |
| 36 | pool_ptr->post_log(worker_ptr: shared_from_this(), msg, overflow_policy: overflow_policy_); |
| 37 | } |
| 38 | else { |
| 39 | throw_spdlog_ex(msg: "async log: thread pool doesn't exist anymore" ); |
| 40 | } |
| 41 | } |
| 42 | SPDLOG_LOGGER_CATCH(msg.source) |
| 43 | } |
| 44 | |
| 45 | // send flush request to the thread pool |
| 46 | SPDLOG_INLINE void spdlog::async_logger::flush_(){SPDLOG_TRY{auto pool_ptr = thread_pool_.lock(); |
| 47 | if (!pool_ptr) { |
| 48 | throw_spdlog_ex(msg: "async flush: thread pool doesn't exist anymore" ); |
| 49 | } |
| 50 | |
| 51 | std::future<void> future = pool_ptr->post_flush(worker_ptr: shared_from_this(), overflow_policy: overflow_policy_); |
| 52 | // Wait for the flush operation to complete. |
| 53 | // This might throw exception if the flush message get dropped because of overflow. |
| 54 | future.get(); |
| 55 | } |
| 56 | SPDLOG_LOGGER_CATCH(source_loc()) |
| 57 | } |
| 58 | |
| 59 | // |
| 60 | // backend functions - called from the thread pool to do the actual job |
| 61 | // |
| 62 | SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) { |
| 63 | for (auto &sink : sinks_) { |
| 64 | if (sink->should_log(msg_level: msg.level)) { |
| 65 | SPDLOG_TRY { sink->log(msg); } |
| 66 | SPDLOG_LOGGER_CATCH(msg.source) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (should_flush_(msg)) { |
| 71 | backend_flush_(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | SPDLOG_INLINE void spdlog::async_logger::backend_flush_() { |
| 76 | for (auto &sink : sinks_) { |
| 77 | SPDLOG_TRY { sink->flush(); } |
| 78 | SPDLOG_LOGGER_CATCH(source_loc()) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name) { |
| 83 | auto cloned = std::make_shared<spdlog::async_logger>(args&: *this); |
| 84 | cloned->name_ = std::move(new_name); |
| 85 | return cloned; |
| 86 | } |
| 87 | |