11 #include "errors/fatal.h"
12 #include "threads/thread.h"
22 _on_thread_initialize(on_thread_initialize),
23 _on_thread_clenup(on_thread_clenup)
49 _thread = CppCommon::Thread::Start([
this]() { ProcessThread(_on_thread_initialize, _on_thread_clenup); });
64 EnqueueRecord(
false, stop);
80 return EnqueueRecord(_discard, record);
83 bool AsyncWaitFreeProcessor::EnqueueRecord(
bool discard,
Record& record)
86 if (!_queue.Enqueue(record))
93 while (!_queue.Enqueue(record))
94 CppCommon::Thread::Yield();
100 void AsyncWaitFreeProcessor::ProcessThread(
const std::function<
void ()>& on_thread_initialize,
const std::function<
void ()>& on_thread_clenup)
103 assert((on_thread_initialize) &&
"Thread initialize handler must be valid!");
104 if (on_thread_initialize)
105 on_thread_initialize();
110 thread_local Record record;
111 thread_local uint64_t previous = CppCommon::Timestamp::utc();
116 bool empty = !_queue.Dequeue(record);
124 if (record.timestamp == 0)
128 if (record.timestamp == 1)
139 current = record.timestamp;
144 current = CppCommon::Timestamp::utc();
148 if (CppCommon::Timespan((int64_t)(current - previous)).seconds() > 1)
159 CppCommon::Thread::Sleep(100);
162 catch (
const std::exception& ex)
168 fatality(
"Asynchronous wait-free logging processor terminated!");
172 assert((on_thread_clenup) &&
"Thread cleanup handler must be valid!");
173 if (on_thread_clenup)
184 thread_local
Record flush;
188 EnqueueRecord(
false, flush);
Asynchronous wait-free logging processor definition.
bool ProcessRecord(Record &record) override
Process the given logging record through all child filters, layouts and appenders.
AsyncWaitFreeProcessor(const std::shared_ptr< Layout > &layout, bool auto_start=true, size_t capacity=8192, bool discard=false, const std::function< void()> &on_thread_initialize=[](){}, const std::function< void()> &on_thread_clenup=[](){})
Initialize asynchronous processor with a given layout interface, overflow policy and buffer capacity.
void Flush() override
Flush the current logging processor.
bool Stop() override
Stop the logging processor.
bool Start() override
Start the logging processor.
virtual ~AsyncWaitFreeProcessor()
Logging processor interface.
bool IsStarted() const noexcept override
Is the logging processor started?
bool Stop() override
Stop the logging processor.
virtual bool ProcessRecord(Record &record)
Process the given logging record through all child filters, layouts and appenders.
bool Start() override
Start the logging processor.
std::atomic< bool > _started
virtual void Flush()
Flush the current logging processor.
uint64_t timestamp
Timestamp of the logging record.
C++ Logging project definitions.