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< STDOUT, STDERR >

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

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.
 
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();
Capture stdout and stderr and convert into log messages.
Definition: IOIntercept.h:226

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.

236 :
237 m_name(name), m_indent(name + ": "), m_stdoutLevel(stdoutLevel), m_stderrLevel(stderrLevel),
238 m_stdoutDebugLevel(stdoutDebugLevel), m_stderrDebugLevel(stderrDebugLevel)
239 {}
LogConfig::ELogLevel m_stderrLevel
severity of the log message to be emitted for output on stderr
Definition: IOIntercept.h:268
const std::string m_name
Name of the output producing tool/library.
Definition: IOIntercept.h:262
int m_stderrDebugLevel
debug level for the log message to be emitted for output on stderr if m_stderrLevel is c_Debug
Definition: IOIntercept.h:272
LogConfig::ELogLevel m_stdoutLevel
severity of the log message to be emitted for output on stdout
Definition: IOIntercept.h:266
int m_stdoutDebugLevel
debug level for the log message to be emitted for output on stdout if m_stdoutLevel is c_Debug
Definition: IOIntercept.h:270
std::string m_indent
Identation to add to the beginning of each line of output.
Definition: IOIntercept.h:264

◆ 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(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.
Definition: IOIntercept.h:235

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

253 {}
@ c_Error
Error: for things that went wrong and have to be fixed.
Definition: LogConfig.h:30
@ c_Info
Info: for informational messages, e.g.
Definition: LogConfig.h:27

◆ ~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(); }
bool finish()
Finish the capture and emit the message if output has appeard on stdout or stderr.
Definition: IOIntercept.cc:236

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();
241 return result;
242 }
const std::string & getStdOut() const
Return the captured stdout output if any.
Definition: IOIntercept.h:160
bool finish()
Finish intercepting the output.
Definition: IOIntercept.h:158
const std::string & getStdErr() const
Return the captured stderr output if any.
Definition: IOIntercept.h:166

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

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

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

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

◆ 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()

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

Identation 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

STDERR m_stderr {std::cerr, stderr}
privateinherited

StreamInterceptor for stderr.

Definition at line 176 of file IOIntercept.h.

◆ 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

STDOUT m_stdout {std::cout, stdout}
privateinherited

StreamInterceptor for stdout.

Definition at line 174 of file IOIntercept.h.

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