Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize > Class Template Reference

Asynchronous background logging service executing on a dedicated C++20 std::jthread. Consumes/flushes log events or periodic status messages asynchronously without blocking the main event loop. More...

#include <LogBackgroundService.hpp>

+ Inheritance diagram for corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize >:
+ Collaboration diagram for corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize >:

Public Types

using Base = Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >
 
using EventVariant = typename Base::EventVariant
 
using IncomingBus = BasicEventBus< EventVariant, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >
 
using LoggerType = LoggerT< LogSink, MaxMessageSize >
 

Public Member Functions

template<typename... Args>
void info (const char *fmt, Args &&... args) const
 Log background heartbeat or event message.
 
void join () noexcept
 Join background std::jthread cleanly.
 
 LogBackgroundService (const char *category="LogService", LogLevel minLevel=LogLevel::Info, LogSink sink=LogSink{})
 
const LoggerTypelogger () const noexcept
 
LoggerTypelogger () noexcept
 Access embedded logger instance.
 
EventSinkT< EventVariantmainSink () const noexcept
 Get an EventSink handle targeting the main application event queue.
 
template<typename Handler >
bool on (Handler &&handler)
 Register an event handler for incoming events with automatic event type deduction.
 
bool processOne ()
 Process a single incoming event from the service queue.
 
std::size_t pump ()
 Pump all pending incoming events from the queue until empty.
 
std::size_t pump (std::size_t maxEvents)
 Pump up to maxEvents pending incoming events from the queue.
 
template<typename EventType , typename Handler >
bool registerHandler (Handler &&handler)
 Register an event handler for incoming events with explicit event type parameter.
 
void run (const std::stop_token &stopToken)
 Main background execution loop.
 
void setContext (ServiceContextT< EventVariant > context) noexcept
 
EventSinkT< EventVariantsink () noexcept
 Get an EventSink handle targeting this service's incoming event queue.
 
template<typename Derived >
void startThread (Derived *derived)
 Start execution loop on dedicated std::jthread.
 
void stop () noexcept
 Request cancellation on worker std::jthread.
 
template<typename Rep , typename Period >
std::size_t waitAndPump (const std::stop_token &stopToken, const std::chrono::duration< Rep, Period > &timeout)
 Wait for incoming events or timeout, then pump all available incoming events. Safe for use inside worker thread run(std::stop_token).
 

Protected Member Functions

const ServiceContextT< EventVariant > & context () const noexcept
 
ServiceContextT< EventVariant > & context () noexcept
 
const IncomingBusincomingBus () const noexcept
 
IncomingBusincomingBus () noexcept
 
template<typename EventType >
void post (EventType &&event, EventPriority priority=EventPriority::Normal) const
 Post an event into the main application event queue.
 
template<typename EventType >
void postHighPriority (EventType &&event) const
 Post a high-priority event into the main application event queue.
 
template<typename TargetService , typename EventType >
bool sendToService (EventType &&event, EventPriority priority=EventPriority::Normal) const
 Post an event directly to another registered service.
 

Detailed Description

template<typename LogSink = sinks::ConsoleLogSink, typename EventVariant = DefaultEvents, std::size_t MaxMessageSize = 256>
class corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize >

Asynchronous background logging service executing on a dedicated C++20 std::jthread. Consumes/flushes log events or periodic status messages asynchronously without blocking the main event loop.

Template Parameters
LogSinkLog sink backend type (e.g. ConsoleLogSink, FileLogSink, NullLogSink).
EventVariantEvent variant list supported by the runtime context.
MaxMessageSizeInline capacity size for log events.

Member Typedef Documentation

◆ Base

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
using corium::BackgroundService< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::Base = Service<EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy>
inherited

◆ EventVariant

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
using corium::BackgroundService< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::EventVariant = typename Base::EventVariant
inherited

◆ IncomingBus

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
using corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::IncomingBus = BasicEventBus<EventVariant, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy>
inherited

◆ LoggerType

template<typename LogSink = sinks::ConsoleLogSink, typename EventVariant = DefaultEvents, std::size_t MaxMessageSize = 256>
using corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize >::LoggerType = LoggerT<LogSink, MaxMessageSize>

Constructor & Destructor Documentation

◆ LogBackgroundService()

template<typename LogSink = sinks::ConsoleLogSink, typename EventVariant = DefaultEvents, std::size_t MaxMessageSize = 256>
corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize >::LogBackgroundService ( const char *  category = "LogService",
LogLevel  minLevel = LogLevel::Info,
LogSink  sink = LogSink{} 
)
inlineexplicit

Member Function Documentation

◆ context() [1/2]

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
const ServiceContextT< EventVariant > & corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::context ( ) const
inlineprotectednoexceptinherited

◆ context() [2/2]

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
ServiceContextT< EventVariant > & corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::context ( )
inlineprotectednoexceptinherited
+ Here is the caller graph for this function:

◆ incomingBus() [1/2]

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
const IncomingBus & corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::incomingBus ( ) const
inlineprotectednoexceptinherited

◆ incomingBus() [2/2]

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
IncomingBus & corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::incomingBus ( )
inlineprotectednoexceptinherited
+ Here is the caller graph for this function:

◆ info()

template<typename LogSink = sinks::ConsoleLogSink, typename EventVariant = DefaultEvents, std::size_t MaxMessageSize = 256>
template<typename... Args>
void corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize >::info ( const char *  fmt,
Args &&...  args 
) const
inline

Log background heartbeat or event message.

+ Here is the call graph for this function:

◆ join()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
void corium::BackgroundService< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::join ( )
inlinenoexceptinherited

Join background std::jthread cleanly.

+ Here is the caller graph for this function:

◆ logger() [1/2]

template<typename LogSink = sinks::ConsoleLogSink, typename EventVariant = DefaultEvents, std::size_t MaxMessageSize = 256>
const LoggerType & corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize >::logger ( ) const
inlinenoexcept

◆ logger() [2/2]

template<typename LogSink = sinks::ConsoleLogSink, typename EventVariant = DefaultEvents, std::size_t MaxMessageSize = 256>
LoggerType & corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize >::logger ( )
inlinenoexcept

Access embedded logger instance.

◆ mainSink()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
EventSinkT< EventVariant > corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::mainSink ( ) const
inlinenoexceptinherited

Get an EventSink handle targeting the main application event queue.

+ Here is the caller graph for this function:

◆ on()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
template<typename Handler >
bool corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::on ( Handler &&  handler)
inlineinherited

Register an event handler for incoming events with automatic event type deduction.

+ Here is the call graph for this function:

◆ post()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
template<typename EventType >
void corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::post ( EventType &&  event,
EventPriority  priority = EventPriority::Normal 
) const
inlineprotectedinherited

Post an event into the main application event queue.

◆ postHighPriority()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
template<typename EventType >
void corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::postHighPriority ( EventType &&  event) const
inlineprotectedinherited

Post a high-priority event into the main application event queue.

+ Here is the caller graph for this function:

◆ processOne()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
bool corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::processOne ( )
inlineinherited

Process a single incoming event from the service queue.

Returns
true if an event was popped and dispatched; false if queue was empty.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pump() [1/2]

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
std::size_t corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::pump ( )
inlineinherited

Pump all pending incoming events from the queue until empty.

Returns
Number of events processed.
+ Here is the call graph for this function:

◆ pump() [2/2]

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
std::size_t corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::pump ( std::size_t  maxEvents)
inlineinherited

Pump up to maxEvents pending incoming events from the queue.

Parameters
maxEventsMaximum number of events to process.
Returns
Number of events processed.
+ Here is the call graph for this function:

◆ registerHandler()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
template<typename EventType , typename Handler >
bool corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::registerHandler ( Handler &&  handler)
inlineinherited

Register an event handler for incoming events with explicit event type parameter.

◆ run()

template<typename LogSink = sinks::ConsoleLogSink, typename EventVariant = DefaultEvents, std::size_t MaxMessageSize = 256>
void corium::logging::LogBackgroundService< LogSink, EventVariant, MaxMessageSize >::run ( const std::stop_token &  stopToken)
inline

Main background execution loop.

+ Here is the call graph for this function:

◆ sendToService()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
template<typename TargetService , typename EventType >
bool corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::sendToService ( EventType &&  event,
EventPriority  priority = EventPriority::Normal 
) const
inlineprotectedinherited

Post an event directly to another registered service.

◆ setContext()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
void corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::setContext ( ServiceContextT< EventVariant context)
inlinenoexceptinherited
+ Here is the call graph for this function:

◆ sink()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
EventSinkT< EventVariant > corium::Service< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::sink ( )
inlinenoexceptinherited

Get an EventSink handle targeting this service's incoming event queue.

+ Here is the call graph for this function:

◆ startThread()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
template<typename Derived >
void corium::BackgroundService< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::startThread ( Derived *  derived)
inlineinherited

Start execution loop on dedicated std::jthread.

+ Here is the call graph for this function:

◆ stop()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
void corium::BackgroundService< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::stop ( )
inlinenoexceptinherited

Request cancellation on worker std::jthread.

+ Here is the caller graph for this function:

◆ waitAndPump()

template<typename EventVariantType = DefaultEvents, typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>, typename SignalPolicy = CallbackSignalPolicy, typename StoragePolicy = DefaultStoragePolicy, typename OverflowPolicy = DropNewestOverflowPolicy>
template<typename Rep , typename Period >
std::size_t corium::BackgroundService< EventVariantType, QueuePolicy, SignalPolicy, StoragePolicy, OverflowPolicy >::waitAndPump ( const std::stop_token &  stopToken,
const std::chrono::duration< Rep, Period > &  timeout 
)
inlineinherited

Wait for incoming events or timeout, then pump all available incoming events. Safe for use inside worker thread run(std::stop_token).

Template Parameters
RepDuration representation type.
PeriodDuration period type.
Parameters
stopTokenstd::stop_token from run(stopToken).
timeoutMaximum duration to wait if queue is empty.
Returns
Number of events processed.
+ Here is the call graph for this function:

The documentation for this class was generated from the following file: