How asynchronous polling works in fmtlog
mainBecause fmtlog is asynchronous, log statements only push data into a queue. To ensure logs are actually written to their destination, you have two options:
- Manual Polling: Call
fmtlog::poll()periodically in your application. This gives you full control over when flushing occurs. Note that only one thread should callfmtlog::poll(). - Background Polling Thread: Call
fmtlog::startPollingThread(interval)to letfmtlogmanage a background thread that polls at the specified interval. Warning: If a background polling thread is running, you cannot callfmtlog::poll()manually.
You can also force an immediate flush by calling fmtlog::poll(true).
// Manual polling approach
logi("Message 1\n");
fmtlog::poll();
logi("Message 2\n");
fmtlog::poll();
// Background thread approach
fmtlog::startPollingThread(1000); // Poll every 1000ms
// Do NOT call fmtlog::poll() manually while this thread is running