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 writes output pending inside the scope to the other destinations, then restores 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
 
LogStream::GuardedRemoval removal_
 what the guard removed, restored on destruction
 

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). On a thread-local stream, it suppresses the stream for the calling thread until it ends, also if the global stream inserts it again meanwhile; then the thread-local stream has the destinations it would have without the guard (e.g. not a destination that the global stream removed meanwhile). On a global stream, it removes the destination and inserts it again, with its prefix, at the end (unless it was inserted again meanwhile); the stream must outlive the guard.
The OPENMS_LOG_* macros write to thread-local streams that follow the destinations of the global ones. Guarding a global stream (getGlobalLog*()) suppresses the sink for all threads; guarding a thread-local stream (getThreadLocalLog*()) only for the calling thread. Text pending before and inside the guarded scope goes to the destinations it was written for: for a global stream, this holds for the calling thread, not for other threads.

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:579
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 writes output pending inside the scope to the other destinations, then restores 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

◆ removal_

LogStream::GuardedRemoval removal_
private

what the guard removed, restored on destruction

◆ 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