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

RAII guard that temporarily removes an attached stream from a LogStream and re-inserts it on scope exit. More...

#include <OpenMS/CONCEPT/LogStream.h>

Public Member Functions

 LogSinkGuard (LogStream &log_stream, std::ostream &stream)
 Construct a guard that removes stream, if attached, and re-inserts it on destruction.
 
 ~LogSinkGuard ()
 Destructor discards any suppressed output, then re-inserts the sink.
 
 LogSinkGuard (const LogSinkGuard &)=delete
 
LogSinkGuard & operator= (const LogSinkGuard &)=delete
 
 LogSinkGuard (LogSinkGuard &&)=delete
 
LogSinkGuard & operator= (LogSinkGuard &&)=delete
 

Private Attributes

LogStream & log_stream_
 
std::ostream & stream_
 
const bool was_attached_
 was the sink attached when the guard was constructed? if not, the guard does nothing
 

Detailed Description

RAII guard that temporarily removes an attached stream from a LogStream and re-inserts it on scope exit.

This class provides exception-safe temporary removal of output streams from LogStream objects. Use this when you need to temporarily suppress logging to a specific stream (e.g., cout) during operations that may throw exceptions.

Note
The guard acts only on a stream that is attached when it is constructed; for an unattached stream it does nothing (will not insert on scope exit).
The OPENMS_LOG_* macros write to thread-local streams whose sink list is copied from the global one on first access. Guard the thread-local stream (getThreadLocalLog*()), not the global one, to suppress output reliably.

Example usage:

LogSinkGuard guard(getThreadLocalLogInfo(), cout); // Removes cout, will re-insert on scope exit
some_operation_that_may_throw();
// cout is automatically re-inserted when guard goes out of scope
RAII guard that temporarily removes an attached stream from a LogStream and re-inserts it on scope ex...
Definition LogStream.h:518
Logger::LogStream & getThreadLocalLogInfo()
Get thread-local info log stream.

Constructor & Destructor Documentation

◆ LogSinkGuard() [1/3]

LogSinkGuard ( LogStream &  log_stream,
std::ostream &  stream 
)
inline

Construct a guard that removes stream, if attached, and re-inserts it on destruction.

Parameters
log_streamThe LogStream to remove the stream from (and re-insert into on destruction)
streamThe stream to temporarily remove (e.g., std::cout); if it is not one of log_stream's destinations, the guard does nothing

◆ ~LogSinkGuard()

~LogSinkGuard ( )
inline

Destructor discards any suppressed output, then re-inserts the sink.

◆ LogSinkGuard() [2/3]

LogSinkGuard ( const LogSinkGuard &  )
delete

◆ LogSinkGuard() [3/3]

LogSinkGuard ( LogSinkGuard &&  )
delete

Member Function Documentation

◆ operator=() [1/2]

LogSinkGuard & operator= ( const LogSinkGuard &  )
delete

◆ operator=() [2/2]

LogSinkGuard & operator= ( LogSinkGuard &&  )
delete

Member Data Documentation

◆ log_stream_

LogStream& log_stream_
private

◆ stream_

std::ostream& stream_
private

◆ was_attached_

const bool was_attached_
private

was the sink attached when the guard was constructed? if not, the guard does nothing