OpenMS
Loading...
Searching...
No Matches
LogStreamBuf Class Reference

Stream buffer used by LogStream. More...

#include <OpenMS/CONCEPT/LogStream.h>

Inheritance diagram for LogStreamBuf:

Classes

struct  LogCacheStruct
 Holds a counter of occurrences and an index for the occurrence sequence of the corresponding log message. More...
 
struct  StreamStruct
 Holds a stream that is connected to the LogStream. It also includes the minimum and maximum level at which the LogStream redirects messages to this stream. More...
 

Public Member Functions

Constructors and Destructors
 LogStreamBuf (const std::string &log_level=UNKNOWN_LOG_LEVEL, Colorizer *col=nullptr)
 
 LogStreamBuf (LogStreamBuf *source_buf, Colorizer *col=nullptr)
 
 ~LogStreamBuf () override
 
Stream methods
int sync () override
 
int overflow (int c=-1) override
 

Static Public Attributes

Constants
static const time_t MAX_TIME
 
static const std::string UNKNOWN_LOG_LEVEL
 

Friends

class LogStream
 

Level methods

char * pbuf_ = nullptr
 
std::string level_
 
std::list< StreamStruct > stream_list_
 Destinations of this buffer (for a following buffer: derived from parent_, own_streams_ and hidden_ids_)
 
std::string incomplete_line_
 
LogStreamBuf * parent_ = nullptr
 Buffer whose destinations this buffer follows (the global buffer of a thread-local one), or nullptr.
 
std::atomic< Size > version_ {0}
 Incremented whenever stream_list_ changes, so that following buffers know when to update.
 
Size parent_version_ = 0
 version_ of parent_ that stream_list_ reflects
 
std::list< StreamStruct > own_streams_
 Destinations inserted into this following buffer; they replace parent_'s entry for the same stream.
 
std::vector< Size > hidden_ids_
 
std::vector< const std::ostream * > guarded_streams_
 Streams that a LogSinkGuard suppresses on this following buffer (one entry per guard), however they are inserted.
 
Colorizer * colorizer_ = nullptr
 
void setLevel (std::string level)
 
std::string getLevel ()
 
void clearCache ()
 
void distribute_ (const std::string &outstring)
 Distribute a new message to connected streams.
 
void distributeNew_ (const std::string &line)
 
std::string expandPrefix_ (const std::string &prefix, time_t time) const
 Interpret the prefix format string and return the expanded prefix.
 
std::list< StreamStruct > & getStreamList_ ()
 Returns the stream list (owned or shared)
 
const std::list< StreamStruct > & getStreamList_ () const
 
void updateFromParent_ ()
 Updates stream_list_ if parent_ changed its destinations since the last update. Locks the sink mutex.
 
void updateFromParentLocked_ ()
 Same as updateFromParent_(), but the caller holds the sink mutex.
 
void rebuildStreamList_ ()
 Rebuilds stream_list_ from parent_'s destinations and this buffer's own changes. The caller holds the sink mutex.
 
bool guarded_ (const std::ostream *stream) const
 Is stream suppressed by a LogSinkGuard on this following buffer (see guarded_streams_)?
 
std::list< StreamStruct > & prefixableStreams_ ()
 Entries whose prefix may be changed: stream_list_, or own_streams_ for a following buffer (the prefix of parent_'s destinations is set on parent_)
 
void destinationsChanged_ ()
 Publishes a change of the destinations to following buffers (or rebuilds stream_list_). The caller holds the sink mutex.
 

Caching

Size log_cache_counter_ = 0
 
std::map< std::string, LogCacheStruct > log_cache_
 Cache of the last two log messages.
 
std::map< Size, std::string > log_time_cache_
 Cache of the occurrence sequence of the last two log messages.
 
bool isInCache_ (std::string const &line)
 Checks if the line is already in the cache.
 
std::string addToCache_ (std::string const &line)
 
Size getNextLogCounter_ ()
 Returns the next free index for a log message.
 
int syncLF_ ()
 Non-lock acquiring sync function called in the d'tor.
 
std::vector< std::string > repeatSummaries_ () const
 "<message> occurred N times" for each cached message that was repeated
 
void forgetRepeatsAfterChange_ (const std::vector< Size > &previous)
 
std::vector< Size > destinations_ () const
 The destinations in stream_list_ (their StreamStruct::id), sorted.
 
void write_ (StreamStruct &s, const std::string &line)
 Writes line to the destination s, with its prefix and color. The caller holds the sink mutex.
 

Detailed Description

Stream buffer used by LogStream.

This class implements the low level behavior of LogStream . It takes care of the buffers and stores the lines written into the LogStream object. It also contains a list of streams that are associated with the LogStream object. This list contains pointers to the streams and their minimum and maximum log level. Each line entered in the LogStream is marked with its time (in fact, the time LogStreamBuf::sync was called) and its loglevel. The loglevel is determined by either the current loglevel (as set by LogStream::setLevel or a temporary level (as set by LogStream::level for a single line only). For each line stored, the list of associated streams is checked whether the loglevel falls into the range declared by the stream's minimum and maximum level. If this condition is met, the logline (with its prefix, see LogStream::setPrefix ) is also copied to the associated stream and this stream is flushed, too.


Class Documentation

◆ OpenMS::Logger::LogStreamBuf::LogCacheStruct

struct OpenMS::Logger::LogStreamBuf::LogCacheStruct

Holds a counter of occurrences and an index for the occurrence sequence of the corresponding log message.

Class Members
int counter
Size timestamp

Constructor & Destructor Documentation

◆ LogStreamBuf() [1/2]

LogStreamBuf ( const std::string &  log_level = UNKNOWN_LOG_LEVEL,
Colorizer *  col = nullptr 
)

Create a new LogStreamBuf object and set the level to log_level

Parameters
[in]log_levelThe log level of the LogStreamBuf (default is unknown)
[in]colIf messages should be colored, provide a colorizer here

◆ LogStreamBuf() [2/2]

LogStreamBuf ( LogStreamBuf *  source_buf,
Colorizer *  col = nullptr 
)

Create a LogStreamBuf that writes to the destinations of another LogStreamBuf.

This constructor is used for thread-local logging: each thread has its own buffer (for thread safety) that follows the destinations of the global LogStream instance, including changes made after this buffer was created. Destinations inserted into or removed from the LogStream of this buffer only affect this buffer (see LogStream::insert()).

Parameters
[in]source_bufThe LogStreamBuf whose destinations should be followed
[in]colIf messages should be colored, provide a colorizer here

◆ ~LogStreamBuf()

~LogStreamBuf ( )
override

Destruct the buffer and free all stored messages strings.

Member Function Documentation

◆ addToCache_()

std::string addToCache_ ( std::string const &  line)
protected

Adds the new line to the cache and removes an old one if necessary

Parameters
[in]lineThe Log message that should be added to the cache
Returns
An additional massage if a re-occurring message was removed from the cache

◆ clearCache()

void clearCache ( )

Checks if some of the cached entries where sent more then once to the LogStream and (if necessary) prints a corresponding messages into all affected Logs

◆ destinations_()

std::vector< Size > destinations_ ( ) const
protected

The destinations in stream_list_ (their StreamStruct::id), sorted.

◆ destinationsChanged_()

void destinationsChanged_ ( )
protected

Publishes a change of the destinations to following buffers (or rebuilds stream_list_). The caller holds the sink mutex.

◆ distribute_()

void distribute_ ( const std::string &  outstring)
protected

Distribute a new message to connected streams.

◆ distributeNew_()

void distributeNew_ ( const std::string &  line)
protected

Adds line, which is not in the cache, to the cache and distributes it, after the repeat count of a message it evicts. Under the sink mutex and after applying changes of parent_'s destinations, so that the count goes only to destinations that got the repeated message.

◆ expandPrefix_()

std::string expandPrefix_ ( const std::string &  prefix,
time_t  time 
) const
protected

Interpret the prefix format string and return the expanded prefix.

◆ forgetRepeatsAfterChange_()

void forgetRepeatsAfterChange_ ( const std::vector< Size > &  previous)
protected

After stream_list_ changed from the destinations previous (see destinations_()): writes the pending repeat counts to the destinations that remain and clears the cache, so that a new destination gets every message. The caller holds the sink mutex.

◆ getLevel()

std::string getLevel ( )

Returns the LogLevel of this LogStream

◆ getNextLogCounter_()

Size getNextLogCounter_ ( )
protected

Returns the next free index for a log message.

◆ getStreamList_() [1/2]

std::list< StreamStruct > & getStreamList_ ( )
protected

Returns the stream list (owned or shared)

◆ getStreamList_() [2/2]

const std::list< StreamStruct > & getStreamList_ ( ) const
protected

◆ guarded_()

bool guarded_ ( const std::ostream *  stream) const
protected

Is stream suppressed by a LogSinkGuard on this following buffer (see guarded_streams_)?

◆ isInCache_()

bool isInCache_ ( std::string const &  line)
protected

Checks if the line is already in the cache.

◆ overflow()

int overflow ( int  c = -1)
override

This method calls sync and streambuf::overflow(c) to prevent a buffer overflow.

◆ prefixableStreams_()

std::list< StreamStruct > & prefixableStreams_ ( )
protected

Entries whose prefix may be changed: stream_list_, or own_streams_ for a following buffer (the prefix of parent_'s destinations is set on parent_)

◆ rebuildStreamList_()

void rebuildStreamList_ ( )
protected

Rebuilds stream_list_ from parent_'s destinations and this buffer's own changes. The caller holds the sink mutex.

◆ repeatSummaries_()

std::vector< std::string > repeatSummaries_ ( ) const
protected

"<message> occurred N times" for each cached message that was repeated

◆ setLevel()

void setLevel ( std::string  level)

Set the level of the LogStream

Parameters
[in]levelThe new LogLevel

◆ sync()

int sync ( )
override

This method is called as soon as the ostream is flushed (especially this method is called by flush or endl). It transfers the contents of the streambufs putbuffer into a logline if a newline or linefeed character is found in the buffer ("\n" or "\r" resp.). The line is then removed from the putbuffer. Incomplete lines (not terminated by "\n" / "\r" are stored in incomplete_line_.

◆ syncLF_()

int syncLF_ ( )
protected

Non-lock acquiring sync function called in the d'tor.

◆ updateFromParent_()

void updateFromParent_ ( )
protected

Updates stream_list_ if parent_ changed its destinations since the last update. Locks the sink mutex.

◆ updateFromParentLocked_()

void updateFromParentLocked_ ( )
protected

Same as updateFromParent_(), but the caller holds the sink mutex.

◆ write_()

void write_ ( StreamStruct &  s,
const std::string &  line 
)
protected

Writes line to the destination s, with its prefix and color. The caller holds the sink mutex.

Friends And Related Symbol Documentation

◆ LogStream

friend class LogStream
friend

Member Data Documentation

◆ colorizer_

Colorizer* colorizer_ = nullptr
protected

optional Colorizer to color the output to stdout/stdcerr (if attached)

◆ guarded_streams_

std::vector<const std::ostream*> guarded_streams_
protected

Streams that a LogSinkGuard suppresses on this following buffer (one entry per guard), however they are inserted.

◆ hidden_ids_

std::vector<Size> hidden_ids_
protected

Destinations of parent_ (their StreamStruct::id) removed from this following buffer. Kept when parent_ removes them: a LogSinkGuard on parent_ restores a destination with its id, and ids are never reused.

◆ incomplete_line_

std::string incomplete_line_
protected

◆ level_

std::string level_
protected

◆ log_cache_

std::map<std::string, LogCacheStruct> log_cache_
protected

Cache of the last two log messages.

◆ log_cache_counter_

Size log_cache_counter_ = 0
protected

Sequential counter to remember the sequence of occurrence of the cached log messages

◆ log_time_cache_

std::map<Size, std::string> log_time_cache_
protected

Cache of the occurrence sequence of the last two log messages.

◆ MAX_TIME

const time_t MAX_TIME
static

◆ own_streams_

std::list<StreamStruct> own_streams_
protected

Destinations inserted into this following buffer; they replace parent_'s entry for the same stream.

◆ parent_

LogStreamBuf* parent_ = nullptr
protected

Buffer whose destinations this buffer follows (the global buffer of a thread-local one), or nullptr.

◆ parent_version_

Size parent_version_ = 0
protected

version_ of parent_ that stream_list_ reflects

◆ pbuf_

char* pbuf_ = nullptr
protected

◆ stream_list_

std::list<StreamStruct> stream_list_
protected

Destinations of this buffer (for a following buffer: derived from parent_, own_streams_ and hidden_ids_)

◆ UNKNOWN_LOG_LEVEL

const std::string UNKNOWN_LOG_LEVEL
static

◆ version_

std::atomic<Size> version_ {0}
protected

Incremented whenever stream_list_ changes, so that following buffers know when to update.