Belle II Software  release-08-01-10
CaptureStream Class Reference

Class to capture anything written to stream into a string. More...

#include <IOIntercept.h>

Inheritance diagram for CaptureStream:
Collaboration diagram for CaptureStream:

Public Member Functions

 CaptureStream (std::ostream &stream, FILE *fileObject)
 Create a StreamInterceptor which writes into a pipe.
 
 ~CaptureStream ()
 Close file descriptors.
 
const std::string & getOutput () const
 Get the output, only set after finish()
 
bool start ()
 Start intercepting the output.
 
bool finish ()
 Restore the stream and get the output from the pipe.
 

Protected Member Functions

void setReplacementFD (int fd)
 set the replacement file descriptor, should be called in the constructor of derived classes
 
bool replaceFD (int fileDescriptor)
 Replace the file descriptor of m_fileObject with the one passed. More...
 

Static Protected Member Functions

static void readFD (int fd, std::string &out)
 Read the contents of a file descriptor until there is no more input and place them in out. More...
 

Protected Attributes

std::ostream & m_stream
 C++ stream object, only needed to flush before replacement.
 
FILE * m_fileObject
 File object of the file we want to replace, needed to obtain file descriptor and to flush.
 
int m_savedFD { -1}
 Saved file descriptor: a duplicate of the file descriptor of m_fileObject.
 
int m_replacementFD { -1}
 Replacement file descriptor to be used while capturing.
 
bool m_capturing {false}
 Check whether we are already capturing.
 

Private Attributes

int m_pipeReadFD { -1}
 file descriptor of the read end of the pipe
 
std::string m_outputStr
 string with the output, only filled after finish()
 

Friends

class CaptureStreamAbortHandler
 allow handling of SIGABRT
 

Detailed Description

Class to capture anything written to stream into a string.

Underlying this uses a pipe to buffer the output.

Warning
the default capacity for capturing output is 64 kB but might be smaller on certain systems. If there is more output than this while capturing it the captured output will be truncated and processes who check if output was successful might fail.

Definition at line 103 of file IOIntercept.h.

Member Function Documentation

◆ readFD()

void readFD ( int  fd,
std::string &  out 
)
staticprotectedinherited

Read the contents of a file descriptor until there is no more input and place them in out.

Parameters
fdfile descriptor to read, should be opened in non blocking mode O_NOBLOCK
outstring to be replaced with all the bytes read from fd

Definition at line 102 of file IOIntercept.cc.

◆ replaceFD()

bool replaceFD ( int  fileDescriptor)
protectedinherited

Replace the file descriptor of m_fileObject with the one passed.

Parameters
fileDescriptorfile descriptor to be set for m_fileObject using dup2()

Definition at line 120 of file IOIntercept.cc.


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