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

Capture stdout and stderr and convert into log messages. More...

#include <IOIntercept.h>

Inheritance diagram for OutputToLogMessages:
Collaboration diagram for OutputToLogMessages:

Public Member Functions

 OutputToLogMessages (const std::string &name, LogConfig::ELogLevel stdoutLevel, LogConfig::ELogLevel stderrLevel, int stdoutDebugLevel, int stderrDebugLevel)
 Full constructor to choose the log levels and debug levels for both stdout and stderr. More...
 
 OutputToLogMessages (const std::string &name, LogConfig::ELogLevel stdoutLevel, LogConfig::ELogLevel stderrLevel)
 Constructor to choose the log levels both stdout and stderr. More...
 
 OutputToLogMessages (const std::string &name)
 Simple constructor which uses c_Info for output on stdout and c_Error for output on stderr. More...
 
 ~OutputToLogMessages ()
 Destructor to make sure that output is converted to messages on destruction.
 
void setIndent (const std::string &indent)
 Set the indent for each line of the output, default is the supplied name + ": "
 
bool finish ()
 Finish the capture and emit the message if output has appeard on stdout or stderr.
 
bool start ()
 Start 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

const std::string m_name
 Name of the output producing tool/library.
 
std::string m_indent
 Identation to add to the beginning of each line of output. More...
 
LogConfig::ELogLevel m_stdoutLevel
 severity of the log message to be emitted for output on stdout
 
LogConfig::ELogLevel m_stderrLevel
 severity of the log message to be emitted for output on stderr
 
int m_stdoutDebugLevel
 debug level for the log message to be emitted for output on stdout if m_stdoutLevel is c_Debug
 
int m_stderrDebugLevel
 debug level for the log message to be emitted for output on stderr if m_stderrLevel is c_Debug
 
STDOUT m_stdout {std::cout, stdout}
 StreamInterceptor for stdout.
 
STDERR m_stderr {std::cerr, stderr}
 StreamInterceptor for stderr.
 

Detailed Description

Capture stdout and stderr and convert into log messages.

This class can be used to convert output by a third party library into basf2 log messages.

For example

IOIntercept::OutputToLogMessages capture("external_library");
capture.start();
// here all output will be intercepted. the call to finish will convert
// any output to stdout to a B2INFO message and any output to stderr to
// a B2ERROR message
std::cerr << "this is my error"
capture.finish();

this will emit an ERROR message of the form

[ERROR] Output from external_library: external_library: this is my error

The leading indentation defaults to the name supplied on construction plus a colon and can be changed using the setIndent() member.

Warning
to NOT use this class for big periods of time. It is intended to be used around short calls to external software which produces output that cannot be converted to log messages otherwise. Don't try to just enable it the whole time. Not only will this cut off ouput that is longer then 64kB, it will also generate long and meaningless messages and will intercept normal log messages emitted while capture is active as well.

Definition at line 226 of file IOIntercept.h.

Constructor & Destructor Documentation

◆ OutputToLogMessages() [1/3]

OutputToLogMessages ( const std::string &  name,
LogConfig::ELogLevel  stdoutLevel,
LogConfig::ELogLevel  stderrLevel,
int  stdoutDebugLevel,
int  stderrDebugLevel 
)
inline

Full constructor to choose the log levels and debug levels for both stdout and stderr.

Parameters
namename of the code causing the output, for example "ROOT", "Rave", ...
stdoutLevelseverity of the log message to be emitted for output on stdout
stderrLevelseverity of the log message to be emitted for output on stderr
stdoutDebugLeveldebug level for the log message to be emitted for output on stdout if stdoutLevel is c_Debug
stderrDebugLeveldebug level for the log message to be emitted for output on stderr if stderrLevel is c_Debug

Definition at line 235 of file IOIntercept.h.

◆ OutputToLogMessages() [2/3]

OutputToLogMessages ( const std::string &  name,
LogConfig::ELogLevel  stdoutLevel,
LogConfig::ELogLevel  stderrLevel 
)
inline

Constructor to choose the log levels both stdout and stderr.

If the level is set to c_Debug a debug level of 100 is used.

Parameters
namename of the code causing the output, for example "ROOT", "Rave", ...
stdoutLevelseverity of the log message to be emitted for output on stdout
stderrLevelseverity of the log message to be emitted for output on stderr

Definition at line 246 of file IOIntercept.h.

◆ OutputToLogMessages() [3/3]

OutputToLogMessages ( const std::string &  name)
inlineexplicit

Simple constructor which uses c_Info for output on stdout and c_Error for output on stderr.

Parameters
namename of the code causing the output, for example "ROOT", "Rave", ...

Definition at line 252 of file IOIntercept.h.

Member Function Documentation

◆ getStdErr()

const std::string& getStdErr ( ) const
inlineinherited

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
inlineinherited

Return the captured stdout output if any.

Only filled after finish() is called

Definition at line 160 of file IOIntercept.h.

◆ start()

bool start ( )
inlineinherited

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.

Member Data Documentation

◆ m_indent

std::string m_indent
private

Identation to add to the beginning of each line of output.

defaults to "${m_name}: "

Definition at line 264 of file IOIntercept.h.


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