Belle II Software  release-08-01-10
InterceptOutput< STDOUT, STDERR > Class Template Reference

Class to intercept stdout and stderr and either capture, discard or keep them unmodified depending on the template arguments. More...

#include <IOIntercept.h>

Inheritance diagram for InterceptOutput< STDOUT, STDERR >:
Collaboration diagram for InterceptOutput< STDOUT, STDERR >:

Public Member Functions

bool start ()
 Start intercepting the output. More...
 
bool finish ()
 Finish intercepting the output. More...
 
const std::string & getStdOut () const
 Return the captured stdout output if any. More...
 
const std::string & getStdErr () const
 Return the captured stderr output if any. More...
 

Private Attributes

STDOUT m_stdout {std::cout, stdout}
 StreamInterceptor for stdout.
 
STDERR m_stderr {std::cerr, stderr}
 StreamInterceptor for stderr.
 

Detailed Description

template<class STDOUT, class STDERR>
class Belle2::IOIntercept::InterceptOutput< STDOUT, STDERR >

Class to intercept stdout and stderr and either capture, discard or keep them unmodified depending on the template arguments.

For example

IOIntercept::InterceptOutput<IOIntercept::CaptureStream, IOIntercept::DiscardStream> capture;
capture.start();
// here all output will be intercepted. Output to stdout will be buffered while
// output to stderr will be discarded immediately.
capture.finish();
// output restored to normal. Buffered stdout can now be retrieved
std::cout << "Output was: " << capture.getStdOut() << std::endl;

Shorthand classes are defined for all use cases, so in the above example we could have just used IOIntercept::CaptureStdOutDiscardStdErr;

Warning
the default capacity for capturing output is 64 kB but might be shorter on certain systems. If there is more output than this to either stdout or stderr while capturing it the captured output will be truncated and processes who check if output was successful might fail.
See also
CaptureStdOutStdErr, CaptureStdOut, CaptureStdErr, DiscardStdOutStdErr, DiscardStdOut, DiscardStdErr, CaptureStdOutDiscardStdErr, DiscardStdOutCaptureStdErr

Definition at line 151 of file IOIntercept.h.

Member Function Documentation

◆ finish()

bool finish ( )
inline

Finish intercepting the output.

This function can be called multiple times.

Returns
true if interception was active and could be removed successfully

Definition at line 158 of file IOIntercept.h.

◆ getStdErr()

const std::string& getStdErr ( ) const
inline

Return the captured stderr output if any.

Only filled after finish() is called

Definition at line 166 of file IOIntercept.h.

◆ getStdOut()

const std::string& getStdOut ( ) const
inline

Return the captured stdout output if any.

Only filled after finish() is called

Definition at line 160 of file IOIntercept.h.

◆ start()

bool start ( )
inline

Start intercepting the output.

This function can be called multiple times.

Returns
true if interception could be started successfully or is already active

Definition at line 155 of file IOIntercept.h.


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