 |
Belle II Software
release-05-02-19
|
15 #include <framework/logging/LogConfig.h>
23 namespace IOIntercept {
25 class StreamInterceptor {
61 static void readFD(
int fd, std::string& out);
84 bool start()
const {
return true; }
86 bool finish()
const {
return true; }
152 template<
class STDOUT,
class STDERR>
153 class InterceptOutput {
164 static_assert(std::is_same<STDOUT, CaptureStream>::value,
"Only valid if stdout is captured using CaptureStream");
170 static_assert(std::is_same<STDERR, CaptureStream>::value,
"Only valid if stderr is captured using CaptureStream");
238 int stdoutDebugLevel,
int stderrDebugLevel):
316 b.m_interceptor =
nullptr;
T * m_interceptor
pointer to the interceptor we guard
int m_savedFD
Saved file descriptor: a duplicate of the file descriptor of m_fileObject.
bool start()
start intercepting the stream.
~StreamInterceptor()
close file descriptors
DiscardStream(std::ostream &stream, FILE *fileObject)
Create StreamInterceptor which will redirect to /dev/null.
void setIndent(const std::string &indent)
Set the indent for each line of the output, default is the supplied name + ": "
bool start() const
doing nothing always succeds
~CaptureStream()
Close file descriptors.
int m_stdoutDebugLevel
debug level for the log message to be emitted for output on stdout if m_stdoutLevel is c_Debug
InterceptorScopeGuard(T &interceptor)
Construct a new instance for a given interceptor object and start intercepting io.
FILE * m_fileObject
File object of the file we want to replace, needed to obtain file descriptor and to flush.
int m_stderrDebugLevel
debug level for the log message to be emitted for output on stderr if m_stderrLevel is c_Debug
STDERR m_stderr
StreamInterceptor for stderr.
void setReplacementFD(int fd)
set the replacement file descriptor, should be called in the constructor of derived classes
Simple class to just discard anything written to stream by redirecting it to /dev/null.
KeepStream(std::ostream &, FILE *)
This class is basically empty so the constructor does nothing.
LogConfig::ELogLevel m_stdoutLevel
severity of the log message to be emitted for output on stdout
bool finish()
Restore the stream and get the output from the pipe.
bool finish()
Finish the capture and emit the message if output has appeard on stdout or stderr.
@ c_Info
Info: for informational messages, e.g.
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.
std::string m_outputStr
string with the output, only filled after finish()
std::string m_indent
Identation to add to the beginning of each line of output.
ELogLevel
Definition of the supported log levels.
CaptureStream(std::ostream &stream, FILE *fileObject)
Create a StreamInterceptor which writes into a pipe.
bool finish()
Finish intercepting the output.
int m_replacementFD
Replacement file descriptor to be used while capturing.
const std::string & getStdOut() const
Return the captured stdout output if any.
STDOUT m_stdout
StreamInterceptor for stdout.
Class to capture anything written to stream into a string.
Abstract base class for different kinds of events.
bool finish()
stop intercepting the stream.
InterceptorScopeGuard< T > start_intercept(T &interceptor)
Convenience wrapper to simplify use of InterceptorScopeGuard<T>.
@ c_Error
Error: for things that went wrong and have to be fixed.
bool start()
Start intercepting the output.
Dummy class which keeps the stream unmodified.
LogConfig::ELogLevel m_stderrLevel
severity of the log message to be emitted for output on stderr
Simple RAII guard for output interceptor.
InterceptorScopeGuard & operator=(const InterceptorScopeGuard &)=delete
Also no assignment.
~OutputToLogMessages()
Destructor to make sure that output is converted to messages on destruction.
const std::string & getStdErr() const
Return the captured stderr output if any.
Base class with all necessary features to intercept output to a file descriptor.
const std::string m_name
Name of the output producing tool/library.
const std::string & getOutput() const
Get the output, only set after finish()
Capture stdout and stderr and convert into log messages.
bool m_capturing
Check whether we are already capturing.
bool finish() const
doing nothing always succeds
int m_pipeReadFD
file descriptor of the read end of the pipe
std::ostream & m_stream
C++ stream object, only needed to flush before replacement.
bool start()
Start intercepting the output.
Class to intercept stdout and stderr and either capture, discard or keep them unmodified depending on...
friend class CaptureStreamAbortHandler
allow handling of SIGABRT
bool replaceFD(int fileDescriptor)
Replace the file descriptor of m_fileObject with the one passed.
InterceptOutput< CaptureStream, CaptureStream > CaptureStdOutStdErr
Capture both stdout and stderr as strings.
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.
StreamInterceptor(std::ostream &stream, FILE *fileObject)
Construct keeping a reference to the std::ostream and the file descriptor which are associated with t...
~InterceptorScopeGuard()
Finish interception on cleanup.