itom
Loading...
Searching...
No Matches
ito::Logger Class Reference

Writes a log file. More...

#include <itomLog.h>

Inheritance diagram for ito::Logger:

Public Slots

void writePythonLog (QString msg)
 Writes a message from python to the log.
 
RetVal copyLog (QString directory, ItomSharedSemaphore *waitCond=nullptr)
 Copies all log files to the given directory.
 

Public Member Functions

 Logger (QString logFileName, QString logFileDir="", int fileSizeBytes=0, int backupCount=0)
 Construct a new Logger object.
 

Private Member Functions

void initFiles (int fileSize, int backupCount)
 Initializes the log files.
 
QFileInfoList listBackups ()
 Lists all existing backup files whit the form <logFile>_<date>.<suffix>
 
void deleteOldBackups (int backupCount)
 Deletes the excess backup files.
 
void storeBackupFile ()
 Creates a backup of m_logFile.
 
void handleMessage (QtMsgType type, const QMessageLogContext &context, const QString &msg)
 Handles a qDebug message.
 

Static Private Member Functions

static void s_messageHandler (QtMsgType type, const QMessageLogContext &context, const QString &msg)
 a static message handler to be registered with qInstallMessageHandler
 

Private Attributes

QFile m_logFile
 
QTextStream * m_messageStream
 
QMutex m_msgOutputProtection
 

Static Private Attributes

static bool s_handlerRegistered = false
 
static QVector< Logger * > s_instances = QVector<Logger*>()
 

Detailed Description

Writes a log file.

The logger redirects qDebug messages to a given log file and offers a method to write messages from python to the same file. It offers the possibility for log rotation where a new file is opened on instantiation if the old file is bigger than a given size. The number of old log files to be kept is configurable.

Constructor & Destructor Documentation

◆ Logger()

ito::Logger::Logger ( QString logFileName,
QString logFileDir = "",
int fileSizeBytes = 0,
int backupCount = 0 )

Construct a new Logger object.

Parameters
logFileNamethe name of the log file
fileSizeBytesthe file size in bytes above which a new file will be created
backupCountthe number of old log files to be kept
logFileDirthe log will be written to this directory; a user directory is used if not given

Member Function Documentation

◆ deleteOldBackups()

void ito::Logger::deleteOldBackups ( int backupCount)
private

Deletes the excess backup files.

Parameters
backupCountthe number of backup files to be kept

◆ handleMessage()

void ito::Logger::handleMessage ( QtMsgType type,
const QMessageLogContext & context,
const QString & msg )
private

Handles a qDebug message.

The incoming message will be parsed and stored to the log file in a specific format.

◆ initFiles()

void ito::Logger::initFiles ( int fileSizeBytes,
int backupCount )
private

Initializes the log files.

Prepares m_logFile for usage by performing a backup and deleting old backups if required.

Parameters
fileSizeBytesthe file size in bytes above which a new file will be created
backupCountthe number of old log files to be kept

◆ s_messageHandler()

void ito::Logger::s_messageHandler ( QtMsgType type,
const QMessageLogContext & context,
const QString & msg )
staticprivate

a static message handler to be registered with qInstallMessageHandler

qInstallMessageHandler can only register a static method so this is used to call handleMessage on every Logger instance.

◆ storeBackupFile()

void ito::Logger::storeBackupFile ( )
private

Creates a backup of m_logFile.

m_logFile is renamed by prepending the current date and afterward m_log file is set to the old name again to create a new log file.

◆ writePythonLog

void ito::Logger::writePythonLog ( QString msg)
slot

Writes a message from python to the log.

The message will be prepended with [Python <date> ].

Parameters
msgthe message to be written, can contain newline characters

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