LogStream Class Reference

Log Stream Class. More...

#include <OpenMS/CONCEPT/LogStream.h>

Inheritance diagram for LogStream:
Collaboration diagram for LogStream:

Public Member Functions

Constructors and Destructors
 LogStream (LogStreamBuf *buf=nullptr, bool delete_buf=true, std::ostream *stream=nullptr)
 ~LogStream () override
 Clears all message buffers. More...
Stream Methods
LogStreamBufrdbuf ()
LogStreamBufoperator-> ()
 Arrow operator. More...
Level methods
void setLevel (std::string level)
std::string getLevel ()

Associating Streams

typedef std::list< LogStreamBuf::StreamStruct >::iterator StreamIterator
bool delete_buffer_
void insert (std::ostream &s)
void remove (std::ostream &s)
void removeAllStreams ()
void insertNotification (std::ostream &s, LogStreamNotifier &target)
 Add a notification target. More...
void setPrefix (const std::ostream &s, const std::string &prefix)
void setPrefix (const std::string &prefix)
 Set prefix of all output streams, details see setPrefix method with ostream. More...
void flush ()
StreamIterator findStream_ (const std::ostream &stream)
bool hasStream_ (std::ostream &stream)
bool bound_ () const

Detailed Description

Log Stream Class.

Defines a log stream which features a cache and some formatting. For the developer, however, only some macros are of interest which will push the message that follows them into the appropriate stream:


  • OPENMS_LOG_ERROR (non-fatal error are reported (processing continues))
  • OPENMS_LOG_WARN (warning, a piece of information which should be read by the user, should be logged)
  • OPENMS_LOG_INFO (information, e.g. a status should be reported)
  • OPENMS_LOG_DEBUG (general debugging information - output be written to cout if debug_level > 0)

To use a specific logger of a log level simply use it as cerr or cout:
OPENMS_LOG_ERROR << " A bad error occurred ..."
Which produces an error message in the log.

The log stream macros are thread safe and can be used in a multithreaded environment, the global variables are not! The macros are protected by a OPENMS_THREAD_CRITICAL directive (which translates to an OpenMP critical pragma), however there may be a small performance penalty to this.

Member Typedef Documentation

◆ StreamIterator

typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator

Constructor & Destructor Documentation

◆ LogStream()

LogStream ( LogStreamBuf buf = nullptr,
bool  delete_buf = true,
std::ostream *  stream = nullptr 

Creates a new LogStream object that is not associated with any stream. If the argument stream is set to an output stream (e.g. cout) all output is send to that stream.


◆ ~LogStream()

~LogStream ( )

Clears all message buffers.

Member Function Documentation

◆ bound_()

bool bound_ ( ) const

◆ findStream_()

StreamIterator findStream_ ( const std::ostream &  stream)

◆ flush()

void flush ( )

◆ getLevel()

std::string getLevel ( )

Returns the LogLevel of this LogStream

◆ hasStream_()

bool hasStream_ ( std::ostream &  stream)

◆ insert()

void insert ( std::ostream &  s)

Associate a new stream with this logstream. This method inserts a new stream into the list of associated streams and sets the corresponding minimum and maximum log levels. Any message that is subsequently logged, will be copied to this stream if its log level is between min_level and max_level. If min_level and max_level are omitted, all messages are copied to this stream. If min_level and max_level are equal, this function can be used to listen to a specified channel.

sa reference to the stream to be associated

Referenced by main().

◆ insertNotification()

void insertNotification ( std::ostream &  s,
LogStreamNotifier target 

Add a notification target.

◆ operator->()

LogStreamBuf* operator-> ( )

Arrow operator.

◆ rdbuf()

LogStreamBuf* rdbuf ( )

rdbuf method of ostream. This method is needed to access the LogStreamBuf object.

See also
std::ostream::rdbuf for more details.

◆ remove()

void remove ( std::ostream &  s)

Remove an association with a stream.

Remove a stream from the stream list and avoid the copying of new messages to this stream.

If the stream was not in the list of associated streams nothing will happen.
sthe stream to be removed

◆ removeAllStreams()

void removeAllStreams ( )

Remove all streams associated to this LogStream, effectively silencing it.

◆ setLevel()

void setLevel ( std::string  level)

Set the level of the LogStream

levelThe new LogLevel

◆ setPrefix() [1/2]

void setPrefix ( const std::ostream &  s,
const std::string &  prefix 

Set prefix for output to this stream. Each line written to the stream will be prefixed by this string. The string may also contain trivial format specifiers to include loglevel and time/date of the logged message.

The following format tags are recognized:
  • y message type ("Error", "Warning", "Information", "-")
  • T time (HH:MM:SS)
  • t time in short format (HH:MM)
  • D date (YYYY/MM/DD)
  • d date in short format (MM/DD)
  • S time and date (YYYY/MM/DD, HH:MM:SS)
  • s time and date in short format (MM/DD, HH:MM)
  • %% percent sign (escape sequence)
sThe stream that will be prefixed.
prefixThe prefix used for the stream.

◆ setPrefix() [2/2]

void setPrefix ( const std::string &  prefix)

Set prefix of all output streams, details see setPrefix method with ostream.

Member Data Documentation

◆ delete_buffer_

bool delete_buffer_

flag needed by the destructor to decide whether the streambuf has to be deleted. If the default ctor is used to create the LogStreamBuf, delete_buffer_ is set to true and the ctor also deletes the buffer.