Skip to content

Commit

Permalink
Improve flush policy by flushing any unflushed log messages when the …
Browse files Browse the repository at this point in the history
…backend worker has no more messages to process. fixes #8
  • Loading branch information
odygrd committed Apr 11, 2020
1 parent 76d627a commit 68098bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* Rotating file handler. The file handler will rollover based on the size of the file
* MinGW compatibility
* Added a CMake option `QUILL_VERBOSE_MAKEFILE`. Building Quill as a master project now defaults to non verbose makefile output unless `-DQUILL_VERBOSE_MAKEFILE=ON` is passed to CMake.

* Flush policy improvement. Previously Quill backend worker thread would never `flush`. This made watching the live log of the application harder because the user has to wait for the operating system to flush or `quill::flush()` had to be called on the caller threads. This has now been fixed, when the backend thread worker has no more log messages to process it will automatically `flush`
## v1.0.0
Initial release
1 change: 1 addition & 0 deletions quill/quill/include/quill/detail/BackendWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class BackendWorker
std::thread _backend_worker_thread; /** the backend thread that is writing the log to the handlers */
std::chrono::nanoseconds _backend_thread_sleep_duration; /** backend_thread_sleep_duration from config **/
std::once_flag _start_init_once_flag; /** flag to start the thread only once, in case start() is called multiple times */
std::size_t _has_unflushed_messages{false}; /** There are messages that are buffered by the OS, but not yet flushed */
std::atomic<bool> _is_running{false}; /** The spawned backend thread status */
};
} // namespace detail
Expand Down
25 changes: 23 additions & 2 deletions quill/quill/src/detail/BackendWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,30 @@ void BackendWorker::_main_loop()

bool const processed_record = _process_record(cached_thread_contexts);

if (QUILL_UNLIKELY(!processed_record))
if (processed_record)
{
// we have found and processed a log record

// Since after processing a log record we never force flush and leave it up to the OS instead
// keep track of how unflushed messages we have
_has_unflushed_messages = true;
}
else
{
// None of the thread local queues had any log record to process, this means we have processed
// all messages in all queues We will force flush any unflushed messages and then sleep
if (_has_unflushed_messages)
{
// If we have buffered any messages then get all active handlers and call flush
std::vector<Handler*> const active_handlers = _handler_collection.active_handlers();
for (auto handler : active_handlers)
{
handler->flush();
}

_has_unflushed_messages = false;
}

// Sleep for the specified duration as we found no records in any of the queues to process
std::this_thread::sleep_for(_backend_thread_sleep_duration);
}
Expand Down Expand Up @@ -171,7 +193,6 @@ bool BackendWorker::_process_record(ThreadContextCollection::backend_thread_cont
desired_record_handle.data()->backend_process(desired_thread_id, obtain_active_handlers,
_rdtsc_clock.get());

// TODO:: When to flush on the handler ? Maybe only if user requested
return true;
}

Expand Down

0 comments on commit 68098bb

Please sign in to comment.