From 743f05634c8193c59b8eebac5e30f391f9deb857 Mon Sep 17 00:00:00 2001 From: irene Date: Sun, 23 Jul 2023 13:36:25 +0800 Subject: [PATCH] moved sync() to thread_pool and fixed bugs --- include/spdlog/async_logger-inl.h | 32 ------------------------ include/spdlog/async_logger.h | 8 ------ include/spdlog/details/thread_pool-inl.h | 26 +++++++++++++++++++ include/spdlog/details/thread_pool.h | 3 +++ 4 files changed, 29 insertions(+), 40 deletions(-) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 36dec2ad..4fc54b7b 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -90,35 +90,3 @@ SPDLOG_INLINE std::shared_ptr spdlog::async_logger::clone(std::s cloned->name_ = std::move(new_name); return cloned; } - -SPDLOG_INLINE bool spdlog::async_logger::sync(int intervalMs, int timeoutMs) -{ - using namespace std::chrono; - - auto tp=thread_pool_.lock(); - assert(tp); - - if (tp->queue_size()==0) - return true; - - auto start=steady_clock::now(); - bool synced=false; - bool indef=(timeoutMs<=0); - do - { - if (!indef && intervalMs>timeoutMs) - intervalMs=timeoutMs; - - std::this_thread::sleep_for(milliseconds(intervalMs)); - synced=(tp->queue_size()==0); - - if (!indef) - { - auto now=steady_clock::now(); - auto passedMs=duration_cast(now-start); - start=now; - timeoutMs-=passedMs.count(); - } - } while ((!indef && timeoutMs>0 || indef) && !synced); - return synced; -} diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index c83b9db0..91a93fcb 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -51,14 +51,6 @@ public: std::shared_ptr clone(std::string new_name) override; - /** - * @brief block until all messages in thread_pool_ are processed - * @param intervalMs check whether should return or not on every 'intervalMs' milliseconds - * @param timeoutMs timeout in milliseconds. -1 means to wait indefinitely - * @return true: all messages in thread_pool_ are processed, false: otherwise - */ - bool sync(int intervalMs=100, int timeoutMs=-1); - protected: void sink_it_(const details::log_msg &msg) override; void flush_() override; diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h index dbd424ff..189f1785 100644 --- a/include/spdlog/details/thread_pool-inl.h +++ b/include/spdlog/details/thread_pool-inl.h @@ -85,6 +85,32 @@ size_t SPDLOG_INLINE thread_pool::queue_size() return q_.size(); } +SPDLOG_INLINE bool thread_pool::sync(int intervalMs, int timeoutMs) +{ + using namespace std::chrono; + + auto start=steady_clock::now(); + bool synced=false; + bool indef=(timeoutMs<=0); + do + { + if (!indef && intervalMs>timeoutMs) + intervalMs=timeoutMs; + + std::this_thread::sleep_for(milliseconds(intervalMs)); + synced=(q_.size()==0); + + if (!indef) + { + auto now=steady_clock::now(); + auto passedMs=duration_cast(now-start); + start=now; + timeoutMs-=static_cast(passedMs.count()); + } + } while (((!indef && timeoutMs>0) || indef) && !synced); + return synced; +} + void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy) { if (overflow_policy == async_overflow_policy::block) diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index 52c569b8..704a98d5 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -100,6 +100,9 @@ public: void reset_overrun_counter(); size_t queue_size(); + // block until all messages in thread pool are processed or the timer goes off. + bool sync(int intervalMs=100, int timeoutMs=-1); + private: q_type q_;