mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-13 09:20:25 +08:00
async flush now waits for queue to be empty before returning
This commit is contained in:
parent
dfa2c7a950
commit
4f52cc4dec
@ -60,7 +60,8 @@ public:
|
|||||||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||||
const std::function<void()>& worker_teardown_cb = nullptr);
|
const std::function<void()>& worker_teardown_cb = nullptr);
|
||||||
|
|
||||||
|
//Wait for the queue to be empty, and flush synchronously
|
||||||
|
//Warning: this can potentialy last forever as we wait it to complete
|
||||||
void flush() override;
|
void flush() override;
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(details::log_msg& msg) override;
|
void _sink_it(details::log_msg& msg) override;
|
||||||
|
@ -78,7 +78,7 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|||||||
txt = std::move(other.txt);
|
txt = std::move(other.txt);
|
||||||
msg_type = other.msg_type;
|
msg_type = other.msg_type;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// never copy or assign. should only be moved..
|
// never copy or assign. should only be moved..
|
||||||
async_msg(const async_msg&) = delete;
|
async_msg(const async_msg&) = delete;
|
||||||
@ -179,6 +179,9 @@ private:
|
|||||||
// sleep,yield or return immediatly using the time passed since last message as a hint
|
// sleep,yield or return immediatly using the time passed since last message as a hint
|
||||||
static void sleep_or_yield(const spdlog::log_clock::time_point& now, const log_clock::time_point& last_op_time);
|
static void sleep_or_yield(const spdlog::log_clock::time_point& now, const log_clock::time_point& last_op_time);
|
||||||
|
|
||||||
|
// wait until the queue is empty
|
||||||
|
void wait_empty_q();
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -249,12 +252,9 @@ inline void spdlog::details::async_log_helper::push_msg(details::async_log_helpe
|
|||||||
//wait for the queue be empty and request flush from its sinks
|
//wait for the queue be empty and request flush from its sinks
|
||||||
inline void spdlog::details::async_log_helper::flush()
|
inline void spdlog::details::async_log_helper::flush()
|
||||||
{
|
{
|
||||||
auto last_op = details::os::now();
|
wait_empty_q();
|
||||||
while (_q.approx_size() > 0)
|
|
||||||
{
|
|
||||||
sleep_or_yield(details::os::now(), last_op);
|
|
||||||
}
|
|
||||||
push_msg(async_msg(async_msg_type::flush));
|
push_msg(async_msg(async_msg_type::flush));
|
||||||
|
wait_empty_q(); //make sure the above flush message was processed
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::details::async_log_helper::worker_loop()
|
inline void spdlog::details::async_log_helper::worker_loop()
|
||||||
@ -366,6 +366,16 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_
|
|||||||
return sleep_for(milliseconds(200));
|
return sleep_for(milliseconds(200));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wait for the queue to be empty
|
||||||
|
inline void spdlog::details::async_log_helper::wait_empty_q()
|
||||||
|
{
|
||||||
|
auto last_op = details::os::now();
|
||||||
|
while (_q.approx_size() > 0) {
|
||||||
|
sleep_or_yield(details::os::now(), last_op);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user