Computer Assited Medical Intervention Tool Kit  version 4.1
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
camitk::CamiTKLogger Class Reference

This is the default logger for CamiTK. More...

#include <CamiTKLogger.h>

Inherits camitk::InterfaceLogger.

Public Member Functions

 CamiTKLogger ()
 Default constructor. More...
 
bool getDebugInformation () override
 
QFileInfo getLogFileInfo () override
 
LogLevel getLogLevel () override
 Get Current verbosity level of the log: More...
 
bool getLogToFile () override
 check if the logger is currently writing on a file More...
 
bool getLogToStandardOutput () override
 check if the logger is currently writing on standard output (std::cout) More...
 
LogLevel getMessageBoxLevel () override
 
bool getTimeStampInformation () override
 
QString log (const QString msg, const LogLevel level, char const *fileName, char const *methodName, int lineNumber, const QObject *sender=nullptr) override
 Log a message: If the logger is allowed to write on standard output, it will display the message (and timestamp) on the standard output If the logger is allowed to write on a file, it will also display the message in the log file. More...
 
void setDebugInformation (bool) override
 Allows the logger to add debug information to the log message. More...
 
bool setLogFileDirectory (QDir directoryName, bool moveExistingLogFile=true) override
 Set the specific directory to write to (default is the subdirectory "CamiTK" in the system temporary directory). More...
 
void setLogLevel (LogLevel level) override
 Sets Current verbosity level of the log: More...
 
bool setLogToFile (bool writeToFile) override
 Allows the logger to write to the log file. More...
 
void setLogToStandardOutput (bool writeToStdOut) override
 Allows the logger to write on std::cout. More...
 
void setMessageBoxLevel (LogLevel level) override
 Set the lowest log level that will open modal message box for messages instead of (silently/undisruptedly) write on std output. More...
 
void setTimeStampInformation (bool showTimeStamp) override
 By default a logger should always show the time-stamp in the form of "yyyy-MM-dd HH:mm:ss.zzz" In test environment, reproducible log message might be prefered. More...
 
 ~CamiTKLogger () override
 Destructor. More...
 
- Public Member Functions inherited from camitk::InterfaceLogger
virtual ~InterfaceLogger ()=default
 empty virtual destructor, to avoid memory leak More...
 

Protected Member Functions

virtual QString buildLogMessage (QString message, LogLevel level, char const *fileName, char const *methodName, int lineNumber, const QObject *sender=nullptr)
 Builds a log message of correct format. More...
 

Private Member Functions

void closeLogFile ()
 close the current log file and reset state More...
 
QString getCamiTKAPIInformation (const QObject *sender)
 Returns CamiTK API description string if and only if sender derived from CamiTK Action. More...
 
bool openLogFile (bool moveFile=false, QFileInfo fileToMove=QFileInfo())
 open the log file, More...
 

Private Attributes

bool displayDebugInformation
 display debug information (file/class name, method name and line number) More...
 
bool displayTimeStampInformation
 display time stamp information (in the form "yyyy-MM-dd HH:mm:ss.zzz") More...
 
InterfaceLogger::LogLevel level
 current log level More...
 
QFile * logFile
 Current log file (the stream is flushed log message by log message) More...
 
QDir logFileDirectory
 Current directory for the log file. More...
 
QDateTime logStartTime
 Instanciation time. More...
 
QTextStream * logStream
 Current stream to output to the log file. More...
 
bool logToFile
 is the logger currently writing everything to a file More...
 
bool logToStdOut
 is the logger currently writing everything to the standard output More...
 
InterfaceLogger::LogLevel messageBoxLevel
 current level for message boxes More...
 

Additional Inherited Members

- Public Types inherited from camitk::InterfaceLogger
enum  LogLevel {
  NONE = 0, ERROR = 1, WARNING = 2, INFO = 3,
  TRACE = 4
}
 : how chatty should the log output be... More...
 

Detailed Description

This is the default logger for CamiTK.

This class implements InterfaceLogger with the following default parameters:

Note
About the log file name The log file name is unique for a given instance of CamiTKLogger as it is based directly on the date/time of instanciantion unless the log file directory is modified during the life of the instance using setLogFileDirectory(). In this case a new date/time is generated inside setLogFileDirectory().

This way if the application or user decides to temporarily stop writing to the log file, all messages will be in the same file, even the one logged after the writing to the log file is resumed.

The log file name is "log-yyyy-MM-ddTHH-mm-ss.camitklog". The date/time format is based on ISO 8601 (as given by Qt::ISODate) with the difference that all ":" are replaced by "-", as ":" in file name does not always agree with Windows. The date/time It is based on the instanciation time (or the time the directory name was changed if is called after the instanciation)

Note
About writing log to file The default directory for the log file is the subdirectory "CamiTK" in the system temporary directory. The filename is automatically determined by the logger. The log file is opened in append mode. This ways, temporarily stop writing to the log file is possible without losing any data.

The log file is permanently kept open for performance reason, and is flushed everytime a log message is appended for safety. This should cover all normal case (even application crash). However, this does not prevent an external process (or user) to truncate or remove the current file. The former will result in missing log message, the latter will result in a complete loss of all previous message and of all newer message.

Note
About message box level When a log message is above the message box level, it will display a modal QMessageBox that depends on the log message level:
  • TRACE and INFO messages are displayed in QMessageBox::Information dialogs
  • WARNING messages are displayed in QMessageBox::Warning dialogs
  • ERROR messages are displayed in QMessageBox::Critical dialogs The main text of the message box contains only the user message but a detailed text is available when the "Show details..." button is pressed. This way the main message to the user is not "polluted" by log timestamp and debug details, but they are both still available to the user (and can be copied to the clipboard for bug report or debugging session).
About logger self message CamiTKLogger also log some messages:
  • during instanciation, when everything is ready, a TRACE message is logged
  • when the configuration change (e.g., the log has changed) or the log file is opened/closed, TRACE messages are logged
  • WARNING messages are logged when something strange occurs on the QTextStream used for logging in file
  • ERROR messages are logged when the log file directory cannot be created, when the log file cannot be opened or when the QTextStream changed to WriteFailed status.
About debugging information Allows the logger to add debug information to the log message. Debug information contains the filename, line number and method names where the log was called. Note that the exact content of the method name debug information depends on the compiler.
About the log message The log macro message is log using the following format: yyyy-MM-dd HH:mm:ss.zzz [LEVEL] [DEBUG_INFO] message ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^ ^if time stamp shown ^ if debug info shown

Moreover, if the log macro is used inside a CamiTK inherited class (e.g., Action, Component,...) then message is prepend with a specific text (see below)

Note
About log macro used inside a CamiTK inherited class If the log macro is used in a CamiTK inherited class, then it will automatically be prepend with a specific text.

For example:

Action, Component, Viewer, Application, MainWindow, ComponentExtension and ActionExtension are currently supported.

Constructor & Destructor Documentation

◆ CamiTKLogger()

camitk::CamiTKLogger::CamiTKLogger ( )

◆ ~CamiTKLogger()

camitk::CamiTKLogger::~CamiTKLogger ( )
override

Destructor.

References closeLogFile(), log(), and camitk::InterfaceLogger::TRACE.

Member Function Documentation

◆ buildLogMessage()

QString camitk::CamiTKLogger::buildLogMessage ( QString  message,
LogLevel  level,
char const *  fileName,
char const *  methodName,
int  lineNumber,
const QObject *  sender = nullptr 
)
protectedvirtual

Builds a log message of correct format.

References displayDebugInformation, displayTimeStampInformation, getCamiTKAPIInformation(), and camitk::Log::getLevelAsString().

Referenced by log().

◆ closeLogFile()

void camitk::CamiTKLogger::closeLogFile ( )
private

◆ getCamiTKAPIInformation()

QString camitk::CamiTKLogger::getCamiTKAPIInformation ( const QObject *  sender)
private

◆ getDebugInformation()

bool camitk::CamiTKLogger::getDebugInformation ( )
overridevirtual
Returns
true if the logger is currently printing the debug information in the log message.

Implements camitk::InterfaceLogger.

References displayDebugInformation.

◆ getLogFileInfo()

QFileInfo camitk::CamiTKLogger::getLogFileInfo ( )
overridevirtual
Returns
the current file information about the current log file

Implements camitk::InterfaceLogger.

References logFile, and logToFile.

◆ getLogLevel()

CamiTKLogger::LogLevel camitk::CamiTKLogger::getLogLevel ( )
overridevirtual

Get Current verbosity level of the log:

Implements camitk::InterfaceLogger.

References level.

◆ getLogToFile()

bool camitk::CamiTKLogger::getLogToFile ( )
overridevirtual

check if the logger is currently writing on a file

Implements camitk::InterfaceLogger.

References logToFile.

◆ getLogToStandardOutput()

bool camitk::CamiTKLogger::getLogToStandardOutput ( )
overridevirtual

check if the logger is currently writing on standard output (std::cout)

Returns
true if the logger is currently writing on std output, false otherwise.

Implements camitk::InterfaceLogger.

References logToStdOut.

◆ getMessageBoxLevel()

InterfaceLogger::LogLevel camitk::CamiTKLogger::getMessageBoxLevel ( )
overridevirtual
Returns
get the current lowest log level that will generate modal message box

Implements camitk::InterfaceLogger.

References messageBoxLevel.

◆ getTimeStampInformation()

bool camitk::CamiTKLogger::getTimeStampInformation ( )
overridevirtual
Returns
true if the logger is currently printing the time stamp information in the log messages.

Implements camitk::InterfaceLogger.

References displayTimeStampInformation.

◆ log()

QString camitk::CamiTKLogger::log ( const QString  msg,
const LogLevel  level,
char const *  fileName,
char const *  methodName,
int  lineNumber,
const QObject *  sender = nullptr 
)
overridevirtual

Log a message: If the logger is allowed to write on standard output, it will display the message (and timestamp) on the standard output If the logger is allowed to write on a file, it will also display the message in the log file.

The level parameter gives the current message level that has to be compared with the logger level: if this level is greater or equals to the current log level, the current message will be logged.

Example: if the logger level is at WARNING, only ERROR and WARNING level messages will be displayed.

If in a static method or a non QObject class, then call this method with an empty last parameter.

Implements camitk::InterfaceLogger.

References buildLogMessage(), camitk::InterfaceLogger::ERROR, getCamiTKAPIInformation(), camitk::Log::getLevelAsString(), camitk::InterfaceLogger::INFO, logStream, logToFile, logToStdOut, messageBoxLevel, camitk::InterfaceLogger::TRACE, and camitk::InterfaceLogger::WARNING.

Referenced by closeLogFile(), openLogFile(), setDebugInformation(), setLogLevel(), setLogToFile(), setLogToStandardOutput(), setMessageBoxLevel(), setTimeStampInformation(), and ~CamiTKLogger().

◆ openLogFile()

bool camitk::CamiTKLogger::openLogFile ( bool  moveFile = false,
QFileInfo  fileToMove = QFileInfo() 
)
private

open the log file,

Returns
true if and only if the log file can be open in write mode

References camitk::InterfaceLogger::ERROR, log(), logFile, logFileDirectory, logStartTime, logStream, logToFile, and camitk::InterfaceLogger::TRACE.

Referenced by setLogFileDirectory(), and setLogToFile().

◆ setDebugInformation()

void camitk::CamiTKLogger::setDebugInformation ( bool  showDebugInformation)
overridevirtual

Allows the logger to add debug information to the log message.

Debug information contains the filename, line number and method names where the log was called. Note that the exact content of the method name debug information depends on the compiler.

Implements camitk::InterfaceLogger.

References displayDebugInformation, log(), and camitk::InterfaceLogger::TRACE.

◆ setLogFileDirectory()

bool camitk::CamiTKLogger::setLogFileDirectory ( QDir  directoryName,
bool  moveExistingLogFile = true 
)
overridevirtual

Set the specific directory to write to (default is the subdirectory "CamiTK" in the system temporary directory).

If the directory does not exists it is going to be created.

If the log directory is different than the existing one:

  • if moveExistingLogFile is true (default), the current log file is moved to the new directory, new messages will be appened
  • if moveExistingLogFile is false, a new log file is generated, previous log file is kept untouched in the previous temporary directory
Parameters
directoryNamethe new directory to use for the log file
moveExistingLogFileif true, the existing log file is move to the new directory and use to append the log messages from now.
Returns
if the logger is already logging to a file, returns true if the log file could have been created in the given directory, otherwise returns the log to file status.

Implements camitk::InterfaceLogger.

References closeLogFile(), logFile, logFileDirectory, logStartTime, logToFile, and openLogFile().

◆ setLogLevel()

void camitk::CamiTKLogger::setLogLevel ( LogLevel  level)
overridevirtual

Sets Current verbosity level of the log:

Implements camitk::InterfaceLogger.

References camitk::Log::getLevelAsString(), level, log(), and camitk::InterfaceLogger::TRACE.

◆ setLogToFile()

bool camitk::CamiTKLogger::setLogToFile ( bool  writeToFile)
overridevirtual

Allows the logger to write to the log file.

By default, the directory to write to is the subdirectory "CamiTK" in the system temporary directory. The file name itself will be determined by the logger.

Returns
true if the logger is going to log into the file (if the parameter is true, it also means that there was no error in opening the logfile for writing in the log file directory)

Implements camitk::InterfaceLogger.

References closeLogFile(), log(), logToFile, openLogFile(), and camitk::InterfaceLogger::TRACE.

◆ setLogToStandardOutput()

void camitk::CamiTKLogger::setLogToStandardOutput ( bool  writeToStdOut)
overridevirtual

Allows the logger to write on std::cout.

Implements camitk::InterfaceLogger.

References log(), logToStdOut, and camitk::InterfaceLogger::TRACE.

◆ setMessageBoxLevel()

void camitk::CamiTKLogger::setMessageBoxLevel ( InterfaceLogger::LogLevel  level)
overridevirtual

Set the lowest log level that will open modal message box for messages instead of (silently/undisruptedly) write on std output.

Default value is NONE (too annoying).

Implements camitk::InterfaceLogger.

References camitk::Log::getLevelAsString(), level, log(), messageBoxLevel, and camitk::InterfaceLogger::TRACE.

◆ setTimeStampInformation()

void camitk::CamiTKLogger::setTimeStampInformation ( bool  showTimeStamp)
overridevirtual

By default a logger should always show the time-stamp in the form of "yyyy-MM-dd HH:mm:ss.zzz" In test environment, reproducible log message might be prefered.

In this case the time stamp can be disabled.

Parameters
showTimeStampif false the time stamp is not printed in log message.

Implements camitk::InterfaceLogger.

References displayTimeStampInformation, log(), and camitk::InterfaceLogger::TRACE.

Member Data Documentation

◆ displayDebugInformation

bool camitk::CamiTKLogger::displayDebugInformation
private

display debug information (file/class name, method name and line number)

Referenced by buildLogMessage(), CamiTKLogger(), getDebugInformation(), and setDebugInformation().

◆ displayTimeStampInformation

bool camitk::CamiTKLogger::displayTimeStampInformation
private

display time stamp information (in the form "yyyy-MM-dd HH:mm:ss.zzz")

Referenced by buildLogMessage(), CamiTKLogger(), getTimeStampInformation(), and setTimeStampInformation().

◆ level

InterfaceLogger::LogLevel camitk::CamiTKLogger::level
private

current log level

Referenced by CamiTKLogger(), getLogLevel(), setLogLevel(), and setMessageBoxLevel().

◆ logFile

QFile* camitk::CamiTKLogger::logFile
private

Current log file (the stream is flushed log message by log message)

Referenced by CamiTKLogger(), closeLogFile(), getLogFileInfo(), openLogFile(), and setLogFileDirectory().

◆ logFileDirectory

QDir camitk::CamiTKLogger::logFileDirectory
private

Current directory for the log file.

Referenced by CamiTKLogger(), openLogFile(), and setLogFileDirectory().

◆ logStartTime

QDateTime camitk::CamiTKLogger::logStartTime
private

Instanciation time.

Referenced by CamiTKLogger(), openLogFile(), and setLogFileDirectory().

◆ logStream

QTextStream* camitk::CamiTKLogger::logStream
private

Current stream to output to the log file.

Referenced by CamiTKLogger(), closeLogFile(), log(), and openLogFile().

◆ logToFile

bool camitk::CamiTKLogger::logToFile
private

is the logger currently writing everything to a file

Referenced by CamiTKLogger(), closeLogFile(), getLogFileInfo(), getLogToFile(), log(), openLogFile(), setLogFileDirectory(), and setLogToFile().

◆ logToStdOut

bool camitk::CamiTKLogger::logToStdOut
private

is the logger currently writing everything to the standard output

Referenced by CamiTKLogger(), getLogToStandardOutput(), log(), and setLogToStandardOutput().

◆ messageBoxLevel

InterfaceLogger::LogLevel camitk::CamiTKLogger::messageBoxLevel
private

current level for message boxes

Referenced by CamiTKLogger(), getMessageBoxLevel(), log(), and setMessageBoxLevel().


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