Quick Links:

bal | bbl | bdl | bsl

Public Types | Public Member Functions | Static Public Attributes

bsls::OutputRedirector Class Reference

#include <bsls_outputredirector.h>

List of all members.

Public Types

enum  Stream { e_STDOUT_STREAM, e_STDERR_STREAM }

Public Member Functions

 OutputRedirector (Stream which, bool verbose=false, bool veryVerbose=false)
 ~OutputRedirector ()
void disable ()
void enable ()
bool load ()
void clear ()
int compare (const char *expected, size_t expectedLength) const
int compare (const char *expected) const
const char * getOutput () const
bool isOutputReady () const
bool isRedirecting () const
FILE * nonRedirectedStream () const
size_t outputSize () const
FILE * redirectedStream () const
OutputRedirector::Stream redirectedStreamId () const

Static Public Attributes

static const size_t k_OUTPUT_REDIRECTOR_BUFFER_SIZE = 4096
static const size_t k_PATH_BUFFER_SIZE = PATH_MAX + 1

Detailed Description

This class provides a facility for redirecting stdout and stderr to temporary files, retrieving output from the respective temporary file and comparing the output to user-supplied character buffers. An OutputRedirector object can be in an un-redirected state or a redirected state. If the redirector is in a redirected state, it will redirect either stdout or stderr, but not both simultaneously. An OutputRedirector object has the concept of a scratch buffer, where output captured from the process' stdout or stderr stream is stored when the OutputRedirector object is in the redirected state. Throughout this class, the term "captured output" refers to data that has been written to the stdout or stderr stream and is waiting to be loaded into the scratch buffer. Each time the load method is called, the scratch buffer is truncated, and the captured output is moved into the scratch buffer. When this is done, there is no longer any captured output.

See Component bsls_outputredirector


Member Enumeration Documentation

The enum Stream represents the specific stream that our object is responsible for redirecting.

Enumerator:
e_STDOUT_STREAM 
e_STDERR_STREAM 

Constructor & Destructor Documentation

bsls::OutputRedirector::OutputRedirector ( Stream  which,
bool  verbose = false,
bool  veryVerbose = false 
) [explicit]

Create an OutputRedirector in an un-redirected state, and with an empty scratch buffer. Upon a call to enable, this redirector will be responsible for redirecting the stream associated with the specified which to a temporary file. The behavior is undefined unless which is equal to OutputRedirector::e_STDOUT_STREAM or OutputRedirector::e_STDERR_STREAM.

bsls::OutputRedirector::~OutputRedirector (  ) 

Destroy this OutputRedirector object. If the object is in a redirected state, the original stream will be restored to its initial target and the temporary file to which the stream was redirected will be deleted.


Member Function Documentation

void bsls::OutputRedirector::disable (  ) 

If the redirector is in a redirected state, restore the original target of the redirected stream and close the temporary buffer. If the redirector is not in a redirected state, this method is a no-op. Calling this method invalidates all output in the temporary file, so a call to load after the next successful enable call will not load any output that was previously written to the file. This method does not clear the scratch buffer, so one may call load before calling disable, and the contents will be available after disable is called. If disable fails to disable the redirection, it will end the program by calling std::abort.

void bsls::OutputRedirector::enable (  ) 

If the Stream specified at construction was e_STDOUT_STREAM, redirect stdout to a temporary file. If the Stream specified at construction was e_STDERR_STREAM, redirect stderr to a temporary file. The temporary file to which the stream is redirected will be created the first time enable is called, and will be deleted when this object is destroyed. If enable fails to redirect either stdout or stderr it will end the program by calling std::abort.

bool bsls::OutputRedirector::load (  ) 

Read captured output into the scratch buffer. Return true if all captured output was successfully loaded, and false otherwise. Note that captured output is allowed to have zero length. The behavior is undefined unless enable has been previously called successfully (after the latest call to disable, if disable has been called successfully).

void bsls::OutputRedirector::clear (  ) 

Reset the scratch buffer to empty. The behavior is undefined unless enable has been previously called successfully (after the latest call to disable if disable has been called successfully).

int bsls::OutputRedirector::compare ( const char *  expected,
size_t  expectedLength 
) const

Compare the character buffer pointed to by the specified pointer expected with any output that has been loaded into the scratch buffer. The length of the expected buffer is supplied in the specified expectedLength. Return 0 if the expected buffer has the same length and contents as the scratch buffer, and non-zero otherwise. Note that the expected buffer is allowed to contain embedded nulls. The behavior is undefined unless enable has previously been called successfully.

int bsls::OutputRedirector::compare ( const char *  expected  )  const

Compare the character buffer pointed to by the specified pointer expected with any output that has been loaded into the scratch buffer. The expected buffer is assumed to be a NTBS, and its length is taken to be the string length of the NTBS. Return 0 if the expected buffer has the same length and contents as the scratch buffer, and non-zero otherwise. The behavior is undefined unless enable has previously been called successfully.

const char* bsls::OutputRedirector::getOutput (  )  const

Return the address of the scratch buffer.

bool bsls::OutputRedirector::isOutputReady (  )  const

Return true if the captured output has been loaded into the scratch buffer, and false otherwise.

bool bsls::OutputRedirector::isRedirecting (  )  const

Return true if stdout or stderr has been successfully redirected, and false otherwise.

FILE* bsls::OutputRedirector::nonRedirectedStream (  )  const

Return the value of the global stdout or stderr corresponding to the stream that is not intended to be redirected by this object.

size_t bsls::OutputRedirector::outputSize (  )  const

Return the number of bytes currently loaded into the scratch buffer.

FILE* bsls::OutputRedirector::redirectedStream (  )  const

Return the value of the global stdout or stderr corresponding to the stream that is intended to be redirected by this object.

OutputRedirector::Stream bsls::OutputRedirector::redirectedStreamId (  )  const

Return OutputRedirector::e_STDOUT_STREAM if this object is responsible for redirecting stdout, and OutputRedirector::e_STDERR_STREAM if this object is responsible for redirecting stderr.


Member Data Documentation

const size_t bsls::OutputRedirector::k_PATH_BUFFER_SIZE = PATH_MAX + 1 [static]

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