Simple-Log  alpha-v0.7
sl::log::OStreamSink< TRecord > Class Template Reference

An std::ostream orientated Sink class which extends BasicSink. More...

#include <OStreamSink.hpp>

Inheritance diagram for sl::log::OStreamSink< TRecord >:
sl::log::BasicSink< TRecord > sl::log::ISink< TRecord > sl::log::ConsoleSink< TRecord > sl::log::FileSink< TRecord >

Public Types

using FlushPolicy_t = std::unique_ptr< detail::AbstractFlushPolicyWrapper< Record_t > >
 
using Record_t = std::remove_cvref_t< TRecord >
 Used Record type. More...
 
using Projections_t = RecordGetters< Record_t >
 
using Formatter_t = std::function< std::string(const Record_t &)>
 
using Filter_t = std::function< bool(const Record_t &)>
 

Public Member Functions

 OStreamSink (std::ostream &stream)
 Constructor. More...
 
 ~OStreamSink () noexcept=default
 Default destructor. More...
 
 OStreamSink (const OStreamSink &)=delete
 Deleted copy constructor. More...
 
OStreamSinkoperator= (const OStreamSink &)=delete
 Deleted copy assign operator. More...
 
 OStreamSink (OStreamSink &&)=delete
 Deleted move constructor. More...
 
OStreamSinkoperator= (OStreamSink &&)=delete
 Deleted move assign operator. More...
 
template<FlushPolicyFor< Record_t > TPolicy>
void setFlushPolicy (TPolicy &&policy)
 Sets the active Flush-Policy. More...
 
void removeFlushPolicy ()
 Replaces the current Flush-Policy with the default one. More...
 
void flush ()
 Flushes all pending output of the internal stream. More...
 
void log (const Record_t &record) final override
 Handles the given Record object. More...
 
void setEnabled (bool enable=true) noexcept final override
 Enables or disables the Sink object. More...
 
bool isEnabled () const noexcept final override
 Checks if the Sink object is enabled. More...
 
template<RecordFormatterFor< Record_t > TFormatter>
void setFormatter (TFormatter &&formatter)
 Sets the active formatter. More...
 
void removeFormatter ()
 Replaces the active formatter with the default one. More...
 
template<RecordFilterFor< Record_t > TFilter>
void setFilter (TFilter &&filter)
 Sets the active filter. More...
 
void removeFilter ()
 Replaces the active filter with the default one. More...
 

Protected Member Functions

template<class TData >
void writeToStream (TData &&data)
 Writes directly to the internal stream. More...
 
virtual void beforeMessageWrite (const Record_t &record, std::string_view message)
 Virtual method which will be called before the actual message is written to the stream. More...
 
virtual void afterMessageWrite (const Record_t &record, std::string_view message)
 Virtual method which will be called after the actual message is written to the stream. More...
 

Static Protected Member Functions

static constexpr FlushPolicy_t defaultFlushPolicy () noexcept
 
static constexpr Formatter_t defaultFormatter () noexcept
 
static constexpr Filter_t defaultFilter () noexcept
 

Detailed Description

template<Record TRecord>
class sl::log::OStreamSink< TRecord >

An std::ostream orientated Sink class which extends BasicSink.

Template Parameters
TRecordUsed Record type.

This Sink class uses a std::ostream reference for printing each recorded message and offers options to manipulate its behaviour: e.g. filtering and formatting messages. Users which would like to print messages onto the console, there is already an existing class ConsoleSink. For file related logging FileSink might be more suitable.

Member Typedef Documentation

◆ Filter_t

template<Record TRecord>
using sl::log::BasicSink< TRecord >::Filter_t = std::function<bool(const Record_t&)>
inherited

◆ FlushPolicy_t

template<Record TRecord>
using sl::log::OStreamSink< TRecord >::FlushPolicy_t = std::unique_ptr<detail::AbstractFlushPolicyWrapper<Record_t> >

◆ Formatter_t

template<Record TRecord>
using sl::log::BasicSink< TRecord >::Formatter_t = std::function<std::string(const Record_t&)>
inherited

◆ Projections_t

template<Record TRecord>
using sl::log::BasicSink< TRecord >::Projections_t = RecordGetters<Record_t>
inherited

◆ Record_t

template<Record TRecord>
using sl::log::ISink< TRecord >::Record_t = std::remove_cvref_t<TRecord>

Used Record type.

Constructor & Destructor Documentation

◆ OStreamSink() [1/3]

template<Record TRecord>
sl::log::OStreamSink< TRecord >::OStreamSink ( std::ostream &  stream)
inlineexplicit

Constructor.

Parameters
streamThe stream object, which will receive finally formatted messages

◆ ~OStreamSink()

template<Record TRecord>
sl::log::OStreamSink< TRecord >::~OStreamSink ( )
defaultnoexcept

Default destructor.

Destructor does not perform any actions on the internal stream objects, due to it's potential dangling state. Derived classes must handle closing and flushing themselves.

◆ OStreamSink() [2/3]

template<Record TRecord>
sl::log::OStreamSink< TRecord >::OStreamSink ( const OStreamSink< TRecord > &  )
delete

Deleted copy constructor.

◆ OStreamSink() [3/3]

template<Record TRecord>
sl::log::OStreamSink< TRecord >::OStreamSink ( OStreamSink< TRecord > &&  )
delete

Deleted move constructor.

Member Function Documentation

◆ afterMessageWrite()

template<Record TRecord>
virtual void sl::log::OStreamSink< TRecord >::afterMessageWrite ( const Record_t record,
std::string_view  message 
)
inlineprotectedvirtual

Virtual method which will be called after the actual message is written to the stream.

Parameters
recordThe current handled Record object
messageThe final message
Remarks
The stream associated mutex is locked before this function gets invoked.
Version
since alpha-0.6

◆ beforeMessageWrite()

template<Record TRecord>
virtual void sl::log::OStreamSink< TRecord >::beforeMessageWrite ( const Record_t record,
std::string_view  message 
)
inlineprotectedvirtual

Virtual method which will be called before the actual message is written to the stream.

Parameters
recordThe current handled Record object
messageThe final message
Remarks
The stream associated mutex is locked before this function gets invoked.
Version
since alpha-0.6

◆ defaultFilter()

template<Record TRecord>
static constexpr Filter_t sl::log::BasicSink< TRecord >::defaultFilter ( )
inlinestaticconstexprprotectednoexceptinherited

◆ defaultFlushPolicy()

template<Record TRecord>
static constexpr FlushPolicy_t sl::log::OStreamSink< TRecord >::defaultFlushPolicy ( )
inlinestaticconstexprprotectednoexcept

◆ defaultFormatter()

template<Record TRecord>
static constexpr Formatter_t sl::log::BasicSink< TRecord >::defaultFormatter ( )
inlinestaticconstexprprotectednoexceptinherited

◆ flush()

template<Record TRecord>
void sl::log::OStreamSink< TRecord >::flush ( )
inline

Flushes all pending output of the internal stream.

Remarks
Internally locks the associated stream mutex.

◆ isEnabled()

template<Record TRecord>
bool sl::log::BasicSink< TRecord >::isEnabled ( ) const
inlinefinaloverridevirtualnoexceptinherited

Checks if the Sink object is enabled.

Returns
Returns true if object is enabled.

Implements sl::log::ISink< TRecord >.

◆ log()

template<Record TRecord>
void sl::log::BasicSink< TRecord >::log ( const Record_t record)
inlinefinaloverridevirtualinherited

Handles the given Record object.

Before the Record gets passed to the actual destination, it will be checked if the Sink object is enabled and if the Record should be filtered. If these checks are passed, the abstract writeMessage function will be invoked with the finally formatted message string.

Parameters
recordRecord object

Implements sl::log::ISink< TRecord >.

◆ operator=() [1/2]

template<Record TRecord>
OStreamSink& sl::log::OStreamSink< TRecord >::operator= ( const OStreamSink< TRecord > &  )
delete

Deleted copy assign operator.

◆ operator=() [2/2]

template<Record TRecord>
OStreamSink& sl::log::OStreamSink< TRecord >::operator= ( OStreamSink< TRecord > &&  )
delete

Deleted move assign operator.

◆ removeFilter()

template<Record TRecord>
void sl::log::BasicSink< TRecord >::removeFilter ( )
inlineinherited

Replaces the active filter with the default one.

◆ removeFlushPolicy()

template<Record TRecord>
void sl::log::OStreamSink< TRecord >::removeFlushPolicy ( )
inline

Replaces the current Flush-Policy with the default one.

The default Flush-Policy flushes after each handled Record.

◆ removeFormatter()

template<Record TRecord>
void sl::log::BasicSink< TRecord >::removeFormatter ( )
inlineinherited

Replaces the active formatter with the default one.

◆ setEnabled()

template<Record TRecord>
void sl::log::BasicSink< TRecord >::setEnabled ( bool  enable = true)
inlinefinaloverridevirtualnoexceptinherited

Enables or disables the Sink object.

Disabled Sinks will not handle any incoming Record s

Parameters
enableTrue will enable the Sink object.

Implements sl::log::ISink< TRecord >.

◆ setFilter()

template<Record TRecord>
template<RecordFilterFor< Record_t > TFilter>
void sl::log::BasicSink< TRecord >::setFilter ( TFilter &&  filter)
inlineinherited

Sets the active filter.

It's the filters job to decide, which Record will be printed (filter returns true) and which will be skipped (filter returns false). Therefore a filter must be an invokable of the following signature:

bool(const Record&)
concept Record
Concept which all the necessary concepts for Record types.
Definition: Record.hpp:204
Remarks
The filters return type doesn't have to be bool, but the returned object must at least be implicitly convertible to bool.
Template Parameters
TFilterType of the passed filter (automatically deduced)
Parameters
filterAn invokable filter object
Examples
FileLogging/main.cpp.

◆ setFlushPolicy()

template<Record TRecord>
template<FlushPolicyFor< Record_t > TPolicy>
void sl::log::OStreamSink< TRecord >::setFlushPolicy ( TPolicy &&  policy)
inline

Sets the active Flush-Policy.

Template Parameters
TPolicyType of the passed Flush-Policy (automatically deduced)
Parameters
policyThe new Flush-Policy object

◆ setFormatter()

template<Record TRecord>
template<RecordFormatterFor< Record_t > TFormatter>
void sl::log::BasicSink< TRecord >::setFormatter ( TFormatter &&  formatter)
inlineinherited

Sets the active formatter.

It's the formatters job to extract the necessary information from Records and built the final message string.

A custom formatter should use the following signature:

std::string(const Record&)
Template Parameters
TFormatterType of the passed formatter (automatically deduced)
Parameters
formatterAn invokable formatter object

◆ writeToStream()

template<Record TRecord>
template<class TData >
void sl::log::OStreamSink< TRecord >::writeToStream ( TData &&  data)
inlineprotected

Writes directly to the internal stream.

Template Parameters
TDataType of data (automatically deduced)
Parameters
dataData which will be written to the stream.

This functions writes directly to the stream object. No filter or formatter will be involved and stream will be flush afterwards. This might be useful for writing custom header or footer data to the stream.

Remarks
If not already locked, this function will lock the stream associated mutex.

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