Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented async_logger::sync() to solve issue #1696 #2819

Open
wants to merge 3 commits into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/spdlog/async_logger-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <memory>
#include <string>

#include <chrono>

SPDLOG_INLINE spdlog::async_logger::async_logger(
std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
: async_logger(std::move(logger_name), sinks_list.begin(), sinks_list.end(), std::move(tp), overflow_policy)
Expand Down
26 changes: 26 additions & 0 deletions include/spdlog/details/thread_pool-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions include/spdlog/details/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class SPDLOG_API thread_pool
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_;

Expand Down