Peano
Loading...
Searching...
No Matches
tarch::logging::CommandLineLogger Class Reference

Command Line Logger. More...

#include <CommandLineLogger.h>

Collaboration diagram for tarch::logging::CommandLineLogger:

Public Member Functions

 ~CommandLineLogger ()
bool getLogMachineName () const
 Is public as some analysis frameworks check explicitly whether these features are switched on.
bool getLogThreadName () const
 Is public as some analysis frameworks check explicitly whether these features are switched on.
bool getLogTrace () const
 Is public as some analysis frameworks check explicitly whether these features are switched on.
bool getLogTimeStamp () const
 Is public as some analysis frameworks check explicitly whether these features are switched on.
void debug (long int timestampNanoseconds, int rank, int threadId, const std::string &trace, const std::string &message)
void info (long int timestampNanoseconds, int rank, int threadId, const std::string &trace, const std::string &message)
void warning (long int timestampNanoseconds, int rank, int threadId, const std::string &trace, const std::string &message)
 Write Warning.
void error (long int timestampNanoseconds, int rank, int threadId, const std::string &trace, const std::string &message)
 Write Error.
void traceIn (long int timestampNanoseconds, int rank, int threadId, const std::string &trace, const std::string &message)
void traceOut (long int timestampNanoseconds, int rank, int threadId, const std::string &trace, const std::string &message)
void indent (bool indent, const std::string &trace, const std::string &message)
 Tells the logger to increment/decrement the indent.
void setLogColumnSeparator (const std::string &separator=" ")
void setLogTimeStamp (bool value=true)
void setLogTimeStampHumanReadable (bool value=true)
void setLogMachineName (bool value=true)
void setLogThreadName (bool value=true)
void setLogMessageType (bool value=true)
void setLogTrace (bool value=true)
void setLogFormat (const std::string &columnSeparator, bool logTimeStamp, bool logTimeStampHumanReadable, bool logMachineName, bool logThreadName, bool logMessageType, bool logTrace, const std::string &outputLogFileName)
void setOutputFile (const std::string &outputLogFileName)
 Is redundant, as you could use setLogFormat() instead.
void setQuitOnError (bool value)
void closeOutputStreamAndReopenNewOne ()
 The command line logger can pipe debug data into an output file instead of piping everything to the terminal.
void close ()

Static Public Member Functions

static CommandLineLoggergetInstance ()

Private Member Functions

CommandLineLoggeroperator= (const CommandLineLogger &)
 Declared private since assignment does not make sense for an output class (output information mismatch).
 CommandLineLogger (const CommandLineLogger &)
 Declared private since copying does not make sense for an output class (output information mismatch).
std::string addSeparators (std::string::size_type spaces, std::string message) const
 Takes the message and adds spaces such that the entries are aligned like in a table.
std::string constructMessageString (std::string messageType, long int timestampNanoseconds, int rank, int threadId, const std::string &trace, const std::string &message)
 Construct message string.
void configureOutputStreams ()
 Configures the output streams.
 CommandLineLogger ()
 It's a singleton.
std::string getLogColumnSeparator () const
bool getLogTimeStampHumanReadable () const
bool getLogMessageType () const
std::ostream & out ()
void reopenOutputStream ()

Static Private Member Functions

static std::string getTimeStampHumanReadable (long int timestampNanoseconds)

Private Attributes

tarch::multicore::BooleanSemaphore _semaphore
std::string _logColumnSeparator
 Test for the column separator of a string output.
bool _logTimeStamp
bool _logTimeStampHumanReadable
bool _logMachineName
bool _logThreadName
bool _logMessageType
bool _logTrace
std::ostream * _outputStream
bool _hasWrittenToOuputStream
int _iterationCounter
std::string _outputFileName
bool _quitOnError
std::stack< std::string > _indentTraces

Static Private Attributes

static Log _log
static CommandLineLogger _singleton
static std::string::size_type _indent = 0
 Indent is supported only in debug mode.
static const std::string::size_type NumberOfIndentSpaces = 2
static const std::string::size_type NumberOfStandardColumnSpaces = 12
static const std::string::size_type NumberOfTraceColumnSpaces = 55
 The trace column is not formatted using only tabulators, but it uses spaces to create a unique column size: First, TRACE_SPACE_NUMBER-n spaces are added if n is the length of the original string, then a \t is appended.
static const int DigitsInFilenamesIterationNumer = 5

Detailed Description

Command Line Logger.

Standard log output device. Implements the LogHandle. Usually there's only one instance of this class for only the Log type aggregates it in a static way. For a detailed description of this compiler flags please browse through the static consts of the class or the howto page.

An interesting issue is the behaviour of the command line logger in the parallel mode if debug is switched on. In this case the number of messages soon becomes unreadable for any human. Thus, the command line logger internally creates one output file rank.log for each rank and writes all the information into this file. info, warnings and errors are written (in short form) to std::cout / std::cerr, too.

If the debug mode is not set, all information is written to std::cout.

Note, that if multithreading is switched on (TBB-flag), the access to std::cout or the logfile respectively is synchronized. The other methods are not thread-safe and should be called only in safe context for defined behaviour.

Version
Revision
1.19
Author
Tobias Weinzierl, Wolfgang Eckhardt

Definition at line 48 of file CommandLineLogger.h.

Constructor & Destructor Documentation

◆ CommandLineLogger() [1/2]

tarch::logging::CommandLineLogger::CommandLineLogger ( const CommandLineLogger & )
private

Declared private since copying does not make sense for an output class (output information mismatch).

Definition at line 79 of file CommandLineLogger.cpp.

References CommandLineLogger().

Referenced by CommandLineLogger(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CommandLineLogger() [2/2]

tarch::logging::CommandLineLogger::CommandLineLogger ( )
private

◆ ~CommandLineLogger()

tarch::logging::CommandLineLogger::~CommandLineLogger ( )

Definition at line 82 of file CommandLineLogger.cpp.

References close().

Here is the call graph for this function:

Member Function Documentation

◆ addSeparators()

std::string tarch::logging::CommandLineLogger::addSeparators ( std::string::size_type spaces,
std::string message ) const
private

Takes the message and adds spaces such that the entries are aligned like in a table.

Should either be passed NumberOfIndentSpaces or NumberOfTraceSpaces.

Definition at line 87 of file CommandLineLogger.cpp.

References getLogColumnSeparator().

Referenced by constructMessageString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ close()

void tarch::logging::CommandLineLogger::close ( )

Definition at line 473 of file CommandLineLogger.cpp.

References _outputStream.

Referenced by ~CommandLineLogger().

Here is the caller graph for this function:

◆ closeOutputStreamAndReopenNewOne()

void tarch::logging::CommandLineLogger::closeOutputStreamAndReopenNewOne ( )

The command line logger can pipe debug data into an output file instead of piping everything to the terminal.

This happens if you call setLogFormat() with a non-empty log file name. This operation closes the output file, and opens a new one. Many uses, e.g., prefer one debug output file per traversal.

Debugging

If you PeanoDebug symbol is bigger than 0, the routine gives you some hint which trace objects are still on the stack, i.e. which traceIn has not yet been matched by a traceOut.

Definition at line 99 of file CommandLineLogger.cpp.

References _indent, _indentTraces, _iterationCounter, logWarning, and reopenOutputStream().

Referenced by runParallel(), and runSerial().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ configureOutputStreams()

void tarch::logging::CommandLineLogger::configureOutputStreams ( )
private

Configures the output streams.

Definition at line 66 of file CommandLineLogger.cpp.

References out().

Referenced by CommandLineLogger().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ constructMessageString()

std::string tarch::logging::CommandLineLogger::constructMessageString ( std::string messageType,
long int timestampNanoseconds,
int rank,
int threadId,
const std::string & trace,
const std::string & message )
private

Construct message string.

!!! Thread Safety

The message string relies on the global field _indent. This one might change throughout the execution of this method. However, I accept such a behavior: Changing _indent throughout the message execution makes the method add the wrong number of whitespaces in front of the message. That is a 'bug' we can accept.

To satisfy Intel Inspector et al at least slightly, I copy over _indent before I actually construct the message string. So the indent can't change while we add the spaces/tabs to the output.

Definition at line 150 of file CommandLineLogger.cpp.

References _indent, _indentTraces, addSeparators(), assertion, getLogMachineName(), getLogMessageType(), getLogThreadName(), getLogTimeStamp(), getLogTimeStampHumanReadable(), getLogTrace(), getTimeStampHumanReadable(), indent(), NumberOfStandardColumnSpaces, and NumberOfTraceColumnSpaces.

Referenced by debug(), error(), info(), traceIn(), traceOut(), and warning().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ debug()

void tarch::logging::CommandLineLogger::debug ( long int timestampNanoseconds,
int rank,
int threadId,
const std::string & trace,
const std::string & message )

Definition at line 196 of file CommandLineLogger.cpp.

References _semaphore, assertion, constructMessageString(), out(), and tarch::logging::LogFilter::FilterListEntry::TargetDebug.

Here is the call graph for this function:

◆ error()

void tarch::logging::CommandLineLogger::error ( long int timestampNanoseconds,
int rank,
int threadId,
const std::string & trace,
const std::string & message )

Write Error.

In the implementation, I call a flush on cout before I write to cerr. Otherwise, the cerr messages might overtake cout. Before the operation returns, it does a flush on cerr, too. Otherwise, the message might not occur, i.e. the application might shut down before the message is flushed to the terminal.

Definition at line 245 of file CommandLineLogger.cpp.

References _outputStream, _quitOnError, _semaphore, tarch::mpi::Rank::abort(), constructMessageString(), and out().

Here is the call graph for this function:

◆ getInstance()

tarch::logging::CommandLineLogger & tarch::logging::CommandLineLogger::getInstance ( )
static

◆ getLogColumnSeparator()

std::string tarch::logging::CommandLineLogger::getLogColumnSeparator ( ) const
private

Definition at line 398 of file CommandLineLogger.cpp.

References _logColumnSeparator.

Referenced by addSeparators().

Here is the caller graph for this function:

◆ getLogMachineName()

bool tarch::logging::CommandLineLogger::getLogMachineName ( ) const

Is public as some analysis frameworks check explicitly whether these features are switched on.

Definition at line 413 of file CommandLineLogger.cpp.

References _logMachineName.

Referenced by constructMessageString().

Here is the caller graph for this function:

◆ getLogMessageType()

bool tarch::logging::CommandLineLogger::getLogMessageType ( ) const
private

Definition at line 423 of file CommandLineLogger.cpp.

References _logMessageType.

Referenced by constructMessageString().

Here is the caller graph for this function:

◆ getLogThreadName()

bool tarch::logging::CommandLineLogger::getLogThreadName ( ) const

Is public as some analysis frameworks check explicitly whether these features are switched on.

Definition at line 418 of file CommandLineLogger.cpp.

References _logThreadName.

Referenced by constructMessageString().

Here is the caller graph for this function:

◆ getLogTimeStamp()

bool tarch::logging::CommandLineLogger::getLogTimeStamp ( ) const

Is public as some analysis frameworks check explicitly whether these features are switched on.

Definition at line 403 of file CommandLineLogger.cpp.

References _logTimeStamp.

Referenced by constructMessageString().

Here is the caller graph for this function:

◆ getLogTimeStampHumanReadable()

bool tarch::logging::CommandLineLogger::getLogTimeStampHumanReadable ( ) const
private

Definition at line 408 of file CommandLineLogger.cpp.

References _logTimeStampHumanReadable.

Referenced by constructMessageString().

Here is the caller graph for this function:

◆ getLogTrace()

bool tarch::logging::CommandLineLogger::getLogTrace ( ) const

Is public as some analysis frameworks check explicitly whether these features are switched on.

Definition at line 428 of file CommandLineLogger.cpp.

References _logTrace.

Referenced by constructMessageString().

Here is the caller graph for this function:

◆ getTimeStampHumanReadable()

std::string tarch::logging::CommandLineLogger::getTimeStampHumanReadable ( long int timestampNanoseconds)
staticprivate
Parameters
timestampNanosecondsTime stamp given in milliseconds

Definition at line 120 of file CommandLineLogger.cpp.

Referenced by constructMessageString().

Here is the caller graph for this function:

◆ indent()

void tarch::logging::CommandLineLogger::indent ( bool indent,
const std::string & trace,
const std::string & message )

Tells the logger to increment/decrement the indent.

!!! Thread Safety

_indent is a global static field shared by all threads. If we increment or decrement it, this is first of all a read followed by a write. Consequently data races could occur and the counter could become smaller than zero. This ain't possible in the sequential code as each increment is accompanied by a decrement. The following table illustrates the race:

|| value of _indent || Thread 1 || Thread 2 | 2 | initial condition | initial condition | | enter indent(false) | enter indent(true) | | fetch indent into register | fetch indent into register | | register value -= 2 | register value += 2 | 4 | is a little bit slower | write back new value of indent | 0 | write back new value of indent |

To avoid this data race, I introduced a semaphore. This one could also be implemented with TBB's atomic construct, e.g., but I prefer the semaphor / critical section technique.

Parameters
traceNeeded in debug mode to be able to find out who called indent(false) without an indent(true)
messageNeeded in debug mode to be able to find out who called indent(false) without an indent(true)

Definition at line 292 of file CommandLineLogger.cpp.

References _indent, _indentTraces, _semaphore, assertion3, assertion5, assertionEquals2, tarch::multicore::Core::getInstance(), indent(), and NumberOfIndentSpaces.

Referenced by constructMessageString(), and indent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ info()

void tarch::logging::CommandLineLogger::info ( long int timestampNanoseconds,
int rank,
int threadId,
const std::string & trace,
const std::string & message )

Definition at line 213 of file CommandLineLogger.cpp.

References _outputStream, _semaphore, constructMessageString(), out(), and tarch::logging::LogFilter::FilterListEntry::TargetInfo.

Here is the call graph for this function:

◆ operator=()

tarch::logging::CommandLineLogger & tarch::logging::CommandLineLogger::operator= ( const CommandLineLogger & )
private

Declared private since assignment does not make sense for an output class (output information mismatch).

Definition at line 74 of file CommandLineLogger.cpp.

References CommandLineLogger().

Here is the call graph for this function:

◆ out()

std::ostream & tarch::logging::CommandLineLogger::out ( )
private

Definition at line 56 of file CommandLineLogger.cpp.

References _hasWrittenToOuputStream, and _outputStream.

Referenced by configureOutputStreams(), debug(), error(), info(), traceIn(), traceOut(), and warning().

Here is the caller graph for this function:

◆ reopenOutputStream()

void tarch::logging::CommandLineLogger::reopenOutputStream ( )
private

Definition at line 328 of file CommandLineLogger.cpp.

References _hasWrittenToOuputStream, _iterationCounter, _outputFileName, _outputStream, _semaphore, and DigitsInFilenamesIterationNumer.

Referenced by closeOutputStreamAndReopenNewOne(), and setOutputFile().

Here is the caller graph for this function:

◆ setLogColumnSeparator()

void tarch::logging::CommandLineLogger::setLogColumnSeparator ( const std::string & separator = " ")

Definition at line 433 of file CommandLineLogger.cpp.

References _logColumnSeparator.

Referenced by CommandLineLogger().

Here is the caller graph for this function:

◆ setLogFormat()

void tarch::logging::CommandLineLogger::setLogFormat ( const std::string & columnSeparator,
bool logTimeStamp,
bool logTimeStampHumanReadable,
bool logMachineName,
bool logThreadName,
bool logMessageType,
bool logTrace,
const std::string & outputLogFileName )
Parameters
outputLogFileNameIf this argument is not the empty string, the logger pipes all debug data into a file and not to the terminal anymore.
See also
closeAllOutputFilesAndReopenNewOnes

Definition at line 376 of file CommandLineLogger.cpp.

References _logColumnSeparator, _logMachineName, _logMessageType, _logThreadName, _logTimeStamp, _logTimeStampHumanReadable, _logTrace, and setOutputFile().

Here is the call graph for this function:

◆ setLogMachineName()

void tarch::logging::CommandLineLogger::setLogMachineName ( bool value = true)

Definition at line 448 of file CommandLineLogger.cpp.

References _logMachineName.

Referenced by CommandLineLogger().

Here is the caller graph for this function:

◆ setLogMessageType()

void tarch::logging::CommandLineLogger::setLogMessageType ( bool value = true)

Definition at line 458 of file CommandLineLogger.cpp.

References _logMessageType.

Referenced by CommandLineLogger().

Here is the caller graph for this function:

◆ setLogThreadName()

void tarch::logging::CommandLineLogger::setLogThreadName ( bool value = true)

Definition at line 453 of file CommandLineLogger.cpp.

References _logThreadName.

Referenced by CommandLineLogger().

Here is the caller graph for this function:

◆ setLogTimeStamp()

void tarch::logging::CommandLineLogger::setLogTimeStamp ( bool value = true)

Definition at line 438 of file CommandLineLogger.cpp.

References _logTimeStamp.

Referenced by CommandLineLogger().

Here is the caller graph for this function:

◆ setLogTimeStampHumanReadable()

void tarch::logging::CommandLineLogger::setLogTimeStampHumanReadable ( bool value = true)

Definition at line 443 of file CommandLineLogger.cpp.

References _logTimeStampHumanReadable.

Referenced by CommandLineLogger().

Here is the caller graph for this function:

◆ setLogTrace()

void tarch::logging::CommandLineLogger::setLogTrace ( bool value = true)

Definition at line 463 of file CommandLineLogger.cpp.

References _logTrace.

Referenced by CommandLineLogger().

Here is the caller graph for this function:

◆ setOutputFile()

void tarch::logging::CommandLineLogger::setOutputFile ( const std::string & outputLogFileName)

Is redundant, as you could use setLogFormat() instead.

However, it is tedious to specify all of these parameters if the only thing you want is to establish an output file.

Definition at line 358 of file CommandLineLogger.cpp.

References _outputFileName, tarch::mpi::Rank::getInstance(), tarch::mpi::Rank::getRank(), and reopenOutputStream().

Referenced by main(), and setLogFormat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setQuitOnError()

void tarch::logging::CommandLineLogger::setQuitOnError ( bool value)

Definition at line 468 of file CommandLineLogger.cpp.

References _quitOnError.

◆ traceIn()

void tarch::logging::CommandLineLogger::traceIn ( long int timestampNanoseconds,
int rank,
int threadId,
const std::string & trace,
const std::string & message )

Definition at line 266 of file CommandLineLogger.cpp.

References _semaphore, constructMessageString(), and out().

Here is the call graph for this function:

◆ traceOut()

void tarch::logging::CommandLineLogger::traceOut ( long int timestampNanoseconds,
int rank,
int threadId,
const std::string & trace,
const std::string & message )

Definition at line 279 of file CommandLineLogger.cpp.

References _semaphore, constructMessageString(), and out().

Here is the call graph for this function:

◆ warning()

void tarch::logging::CommandLineLogger::warning ( long int timestampNanoseconds,
int rank,
int threadId,
const std::string & trace,
const std::string & message )

Write Warning.

In the implementation, I call a flush on cout before I write to cerr. Otherwise, the cerr messages might overtake cout. Before the operation returns, it does a flush on cerr, too. Otherwise, the message might not occur, i.e. the application might shut down before the message is flushed to the terminal.

Definition at line 228 of file CommandLineLogger.cpp.

References _outputStream, _semaphore, constructMessageString(), out(), and tarch::logging::LogFilter::FilterListEntry::TargetWarning.

Here is the call graph for this function:

Field Documentation

◆ _hasWrittenToOuputStream

bool tarch::logging::CommandLineLogger::_hasWrittenToOuputStream
private

Definition at line 67 of file CommandLineLogger.h.

Referenced by CommandLineLogger(), out(), and reopenOutputStream().

◆ _indent

std::string::size_type tarch::logging::CommandLineLogger::_indent = 0
staticprivate

Indent is supported only in debug mode.

Definition at line 87 of file CommandLineLogger.h.

Referenced by closeOutputStreamAndReopenNewOne(), constructMessageString(), and indent().

◆ _indentTraces

std::stack< std::string > tarch::logging::CommandLineLogger::_indentTraces
private

◆ _iterationCounter

int tarch::logging::CommandLineLogger::_iterationCounter
private

◆ _log

tarch::logging::Log tarch::logging::CommandLineLogger::_log
staticprivate

Definition at line 50 of file CommandLineLogger.h.

◆ _logColumnSeparator

std::string tarch::logging::CommandLineLogger::_logColumnSeparator
private

Test for the column separator of a string output.

Definition at line 59 of file CommandLineLogger.h.

Referenced by getLogColumnSeparator(), setLogColumnSeparator(), and setLogFormat().

◆ _logMachineName

bool tarch::logging::CommandLineLogger::_logMachineName
private

Definition at line 62 of file CommandLineLogger.h.

Referenced by getLogMachineName(), setLogFormat(), and setLogMachineName().

◆ _logMessageType

bool tarch::logging::CommandLineLogger::_logMessageType
private

Definition at line 64 of file CommandLineLogger.h.

Referenced by getLogMessageType(), setLogFormat(), and setLogMessageType().

◆ _logThreadName

bool tarch::logging::CommandLineLogger::_logThreadName
private

Definition at line 63 of file CommandLineLogger.h.

Referenced by getLogThreadName(), setLogFormat(), and setLogThreadName().

◆ _logTimeStamp

bool tarch::logging::CommandLineLogger::_logTimeStamp
private

Definition at line 60 of file CommandLineLogger.h.

Referenced by getLogTimeStamp(), setLogFormat(), and setLogTimeStamp().

◆ _logTimeStampHumanReadable

bool tarch::logging::CommandLineLogger::_logTimeStampHumanReadable
private

◆ _logTrace

bool tarch::logging::CommandLineLogger::_logTrace
private

Definition at line 65 of file CommandLineLogger.h.

Referenced by getLogTrace(), setLogFormat(), and setLogTrace().

◆ _outputFileName

std::string tarch::logging::CommandLineLogger::_outputFileName
private

Definition at line 69 of file CommandLineLogger.h.

Referenced by reopenOutputStream(), and setOutputFile().

◆ _outputStream

std::ostream* tarch::logging::CommandLineLogger::_outputStream
private

Definition at line 66 of file CommandLineLogger.h.

Referenced by close(), CommandLineLogger(), error(), info(), out(), reopenOutputStream(), and warning().

◆ _quitOnError

bool tarch::logging::CommandLineLogger::_quitOnError
private

Definition at line 70 of file CommandLineLogger.h.

Referenced by CommandLineLogger(), error(), and setQuitOnError().

◆ _semaphore

tarch::multicore::BooleanSemaphore tarch::logging::CommandLineLogger::_semaphore
private

Definition at line 54 of file CommandLineLogger.h.

Referenced by debug(), error(), indent(), info(), reopenOutputStream(), traceIn(), traceOut(), and warning().

◆ _singleton

tarch::logging::CommandLineLogger tarch::logging::CommandLineLogger::_singleton
staticprivate

Definition at line 52 of file CommandLineLogger.h.

Referenced by getInstance().

◆ DigitsInFilenamesIterationNumer

const int tarch::logging::CommandLineLogger::DigitsInFilenamesIterationNumer = 5
staticprivate

Definition at line 108 of file CommandLineLogger.h.

Referenced by reopenOutputStream().

◆ NumberOfIndentSpaces

const std::string::size_type tarch::logging::CommandLineLogger::NumberOfIndentSpaces = 2
staticprivate
Todo

Definition at line 92 of file CommandLineLogger.h.

Referenced by indent().

◆ NumberOfStandardColumnSpaces

const std::string::size_type tarch::logging::CommandLineLogger::NumberOfStandardColumnSpaces = 12
staticprivate
Todo

Definition at line 97 of file CommandLineLogger.h.

Referenced by constructMessageString().

◆ NumberOfTraceColumnSpaces

const std::string::size_type tarch::logging::CommandLineLogger::NumberOfTraceColumnSpaces = 55
staticprivate

The trace column is not formatted using only tabulators, but it uses spaces to create a unique column size: First, TRACE_SPACE_NUMBER-n spaces are added if n is the length of the original string, then a \t is appended.

This message augmentation is done only, if the Debug-mode is set.

Definition at line 106 of file CommandLineLogger.h.

Referenced by constructMessageString().


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