Belle II Software light-2406-ragdoll
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 >:

Public Member Functions

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

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

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;
Class to intercept stdout and stderr and either capture, discard or keep them unmodified depending on...
Definition: IOIntercept.h:151
const std::string & getStdOut() const
Return the captured stdout output if any.
Definition: IOIntercept.h:160
bool start()
Start intercepting the output.
Definition: IOIntercept.h:155
bool finish()
Finish intercepting the output.
Definition: IOIntercept.h:158

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.

158{ return m_stdout.finish() && m_stderr.finish(); }
STDOUT m_stdout
StreamInterceptor for stdout.
Definition: IOIntercept.h:174
STDERR m_stderr
StreamInterceptor for stderr.
Definition: IOIntercept.h:176

◆ 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.

167 {
168 static_assert(std::is_same<STDERR, CaptureStream>::value, "Only valid if stderr is captured using CaptureStream");
169 return m_stderr.getOutput();
170 }

◆ 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.

161 {
162 static_assert(std::is_same<STDOUT, CaptureStream>::value, "Only valid if stdout is captured using CaptureStream");
163 return m_stdout.getOutput();
164 }

◆ 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.

155{ return m_stdout.start() && m_stderr.start(); }

Member Data Documentation

◆ m_stderr

STDERR m_stderr {std::cerr, stderr}
private

StreamInterceptor for stderr.

Definition at line 176 of file IOIntercept.h.

◆ m_stdout

STDOUT m_stdout {std::cout, stdout}
private

StreamInterceptor for stdout.

Definition at line 174 of file IOIntercept.h.


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