Belle II Software development
OutputToLogMessages Class Reference

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

#include <IOIntercept.h>

Inheritance diagram for OutputToLogMessages:
InterceptOutput< CaptureStream, CaptureStream >

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.
 
 OutputToLogMessages (const std::string &name, LogConfig::ELogLevel stdoutLevel, LogConfig::ELogLevel stderrLevel)
 Constructor to choose the log levels both stdout and stderr.
 
 OutputToLogMessages (const std::string &name)
 Simple constructor which uses c_Info for output on stdout and c_Error for output on stderr.
 
 ~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.
 
bool start ()
 Start intercepting the output.
 
const std::string & getStdOut () const
 Return the captured stdout output if any.
 
const std::string & getStdOut () const
 Return the captured stdout output if any.
 
const std::string & getStdErr () const
 Return the captured stderr output if any.
 
const std::string & getStdErr () const
 Return the captured stderr output if any.
 

Private Attributes

const std::string m_name
 Name of the output producing tool/library.
 
std::string m_indent
 Indentation to add to the beginning of each line of output.
 
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
 
CaptureStream m_stdout
 StreamInterceptor for stdout.
 
CaptureStream m_stdout
 StreamInterceptor for stdout.
 
CaptureStream m_stderr
 StreamInterceptor for stderr.
 
CaptureStream m_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();
Capture stdout and stderr and convert into log messages.

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

236 :
237 m_name(name), m_indent(name + ": "), m_stdoutLevel(stdoutLevel), m_stderrLevel(stderrLevel),
238 m_stdoutDebugLevel(stdoutDebugLevel), m_stderrDebugLevel(stderrDebugLevel)
239 {}

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

246 :
247 OutputToLogMessages(name, stdoutLevel, stderrLevel, 100, 100)
248 {}

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

252 : OutputToLogMessages(name, LogConfig::c_Info, LogConfig::c_Error)
253 {}

◆ ~OutputToLogMessages()

~OutputToLogMessages ( )
inline

Destructor to make sure that output is converted to messages on destruction.

Definition at line 255 of file IOIntercept.h.

255{ finish(); }

Member Function Documentation

◆ finish()

bool finish ( )

Finish the capture and emit the message if output has appeard on stdout or stderr.

Definition at line 236 of file IOIntercept.cc.

237 {
238 bool result = CaptureStdOutStdErr::finish();
239 sendLogMessage(m_stdoutLevel, m_stdoutDebugLevel, m_name, m_indent, getStdOut());
240 sendLogMessage(m_stderrLevel, m_stderrDebugLevel, m_name, m_indent, getStdErr());
241 return result;
242 }

◆ getStdErr() [1/2]

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.

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

◆ getStdErr() [2/2]

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.

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() [1/2]

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.

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

◆ getStdOut() [2/2]

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.

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

◆ setIndent()

void setIndent ( const std::string & indent)
inline

Set the indent for each line of the output, default is the supplied name + ": "

Definition at line 257 of file IOIntercept.h.

257{ m_indent = indent; }

◆ start() [1/2]

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.

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

◆ start() [2/2]

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.

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

Member Data Documentation

◆ m_indent

std::string m_indent
private

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

defaults to "${m_name}: "

Definition at line 264 of file IOIntercept.h.

◆ m_name

const std::string m_name
private

Name of the output producing tool/library.

Definition at line 262 of file IOIntercept.h.

◆ m_stderr [1/2]

CaptureStream m_stderr
privateinherited

StreamInterceptor for stderr.

Definition at line 176 of file IOIntercept.h.

176{std::cerr, stderr};

◆ m_stderr [2/2]

CaptureStream m_stderr
privateinherited

StreamInterceptor for stderr.

Definition at line 176 of file IOIntercept.h.

176{std::cerr, stderr};

◆ m_stderrDebugLevel

int m_stderrDebugLevel
private

debug level for the log message to be emitted for output on stderr if m_stderrLevel is c_Debug

Definition at line 272 of file IOIntercept.h.

◆ m_stderrLevel

LogConfig::ELogLevel m_stderrLevel
private

severity of the log message to be emitted for output on stderr

Definition at line 268 of file IOIntercept.h.

◆ m_stdout [1/2]

CaptureStream m_stdout
privateinherited

StreamInterceptor for stdout.

Definition at line 174 of file IOIntercept.h.

174{std::cout, stdout};

◆ m_stdout [2/2]

CaptureStream m_stdout
privateinherited

StreamInterceptor for stdout.

Definition at line 174 of file IOIntercept.h.

174{std::cout, stdout};

◆ m_stdoutDebugLevel

int m_stdoutDebugLevel
private

debug level for the log message to be emitted for output on stdout if m_stdoutLevel is c_Debug

Definition at line 270 of file IOIntercept.h.

◆ m_stdoutLevel

LogConfig::ELogLevel m_stdoutLevel
private

severity of the log message to be emitted for output on stdout

Definition at line 266 of file IOIntercept.h.


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