moved sync() to thread_pool and fixed bugs

pull/2819/head
irene 2 years ago
parent ba2f2cbc1c
commit 743f05634c

@ -90,35 +90,3 @@ SPDLOG_INLINE std::shared_ptr<spdlog::logger> 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<milliseconds>(now-start);
start=now;
timeoutMs-=passedMs.count();
}
} while ((!indef && timeoutMs>0 || indef) && !synced);
return synced;
}

@ -51,14 +51,6 @@ public:
std::shared_ptr<logger> 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;

@ -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<milliseconds>(now-start);
start=now;
timeoutMs-=static_cast<int>(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)

@ -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_;

Loading…
Cancel
Save