Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::ApplicationLogger Class Reference

The ApplicationLogger class acts as a convenient means of producing an execution log for an application. More...

#include <qx/io/qx-applicationlogger.h>

Public Member Functions

 ApplicationLogger ()
 
 ApplicationLogger (const QString &filePath)
 
 ApplicationLogger (const QString &filePath, const QCoreApplication *app)
 
QString applicationArguments () const
 
QString applicationName () const
 
QString applicationVersion () const
 
QString filePath () const
 
IoOpReport finish (int returnCode)
 
bool hasError () const
 
bool isOpen () const
 
int maximumEntries () const
 
IoOpReport openLog ()
 
IoOpReport recordErrorEvent (const QString &src, const Error &error)
 
IoOpReport recordGeneralEvent (const QString &src, const QString &event)
 
IoOpReport recordVerbatim (const QString &text)
 
void resetStatus ()
 
void setApplicationArguments (const QString &args)
 
void setApplicationArguments (const QStringList &args)
 
void setApplicationName (const QString &name)
 
void setApplicationVersion (const QString &version)
 
void setFilePath (const QString &path)
 
void setMaximumEntries (int max)
 
IoOpReport status () const
 

Detailed Description

Often it is useful for an applications to produce a log file that provides additional information about its inner-workings in order to assist with debugging, optimization, or error resolution.

ApplicationLogger simplifies this process by providing a simple interface through which to record basic information about an application and record events/errors with automatic timestamps.

[ My Application Execution Log ] (1.0.8) : Sun Oct 9 00:53:51 2022
Arguments: -i input_file.bin
Events:
- <00:53:51> [Main] App started
- <00:53:51> [Main] CRITICAL) Bad thing happened!
- <00:53:51> [Main] App closing...
---------- Execution finished prematurely (Code -1) ----------

Details of your application should be provided via the classes various setters.

Constructor & Destructor Documentation

◆ ApplicationLogger() [1/3]

Qx::ApplicationLogger::ApplicationLogger ( )

Creates an application logger with no file path or details set.

◆ ApplicationLogger() [2/3]

Qx::ApplicationLogger::ApplicationLogger ( const QString & filePath)

Creates an application logger set to record to the file at filePath.

See also
setFilePath().

◆ ApplicationLogger() [3/3]

Qx::ApplicationLogger::ApplicationLogger ( const QString & filePath,
const QCoreApplication * app )

Creates an application logger set to record to the file at filePath.

The details of the application (name, version, and arguments) are automatically set using the application instance app.

See also
QCoreApplication::instance().

Member Function Documentation

◆ applicationArguments()

QString Qx::ApplicationLogger::applicationArguments ( ) const

Returns the arguments string of the application that will be recorded in the log.

See also
setApplicationArguments().

◆ applicationName()

QString Qx::ApplicationLogger::applicationName ( ) const

Returns the name of the application that will be recorded in the log.

See also
setApplicationName().

◆ applicationVersion()

QString Qx::ApplicationLogger::applicationVersion ( ) const

Returns the version of the application that will be recorded in the log.

See also
setApplicationVersion().

◆ filePath()

QString Qx::ApplicationLogger::filePath ( ) const

Returns the path of the log file that the logger is set to record to.

See also
setFilePath().

◆ finish()

IoOpReport Qx::ApplicationLogger::finish ( int returnCode)

Writes a footer to the current log entry that notes the application's return code and whether or not execution finished successfully, then closes the log. The return code is reinterpreted as an unsigned integer before it's printed.

If returnCode equals 0, execution is considered to have been successful; otherwise, it will be considered to have ended prematurely.

See also
openLog().

◆ hasError()

bool Qx::ApplicationLogger::hasError ( ) const

Returns true if the logger's current status indicates that an error has occurred; otherwise, returns false.

Equivalent to status().isFailure().

See also
status().

◆ isOpen()

bool Qx::ApplicationLogger::isOpen ( ) const

Returns true if the log is open for writing; otherwise, returns false.

See also
openLog() and finish().

◆ maximumEntries()

int Qx::ApplicationLogger::maximumEntries ( ) const

Returns the maximum number of entries allowed in the log before the oldest entry is purged.

The default is 100.

See also
setMaximumEntries().

◆ openLog()

IoOpReport Qx::ApplicationLogger::openLog ( )

Opens the log for recording, writes the log entry heading and basic application information, and returns a report noting if the operation succeeded or failed.

See also
finish().

◆ recordErrorEvent()

IoOpReport Qx::ApplicationLogger::recordErrorEvent ( const QString & src,
const Error & error )

Records error to the log with a timestamp and label that denotes the error's severity.

The parameter src acts as a means to identify which section of the application the error originated from, whether it be a source file name, class name, etc.

See also
recordGeneralEvent().

◆ recordGeneralEvent()

IoOpReport Qx::ApplicationLogger::recordGeneralEvent ( const QString & src,
const QString & event )

Records event to the log with a timestamp.

The parameter src acts as a means to identify which section of the application the error originated from, whether it be a source file name, class name, etc.

See also
recordErrorEvent().

◆ recordVerbatim()

IoOpReport Qx::ApplicationLogger::recordVerbatim ( const QString & text)

Records text to the log directly as provided with no timestamp or list item separator and terminates it with a newline.

See also
recordGeneralEvent(), and recordErrorEvent().

◆ resetStatus()

void Qx::ApplicationLogger::resetStatus ( )

Resets the status of the logger.

Note
If an error occurs while writing the logger will ignore all further write attempts and hold its current status until this function is called.
See also
status().

◆ setApplicationArguments() [1/2]

void Qx::ApplicationLogger::setApplicationArguments ( const QString & args)

Sets the arguments string of the application that will be recorded in the log to args.

If no arguments are set or the provided argument string is empty, the log will record the application's arguments as *None*.

Note
This must be called before the log is opened or else it will have no effect.
See also
applicationArguments().

◆ setApplicationArguments() [2/2]

void Qx::ApplicationLogger::setApplicationArguments ( const QStringList & args)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

args are joined together into a single string using spaces as a separator.

See also
applicationArguments().

◆ setApplicationName()

void Qx::ApplicationLogger::setApplicationName ( const QString & name)

Sets the name of the application that will be recorded in the log to name.

Note
This must be called before the log is opened or else it will have no effect.
See also
applicationName().

◆ setApplicationVersion()

void Qx::ApplicationLogger::setApplicationVersion ( const QString & version)

Sets the version of the application that will be recorded in the log version.

Note
This must be called before the log is opened or else it will have no effect.
See also
applicationVersion().

◆ setFilePath()

void Qx::ApplicationLogger::setFilePath ( const QString & path)

Sets the path of the log file that the logger will use for recording to path.

The file is appended to with a newline separating the new log entry from the previous entry.

Note
This must be called before the log is opened.
See also
filePath(), and setMaximumEntries().

◆ setMaximumEntries()

void Qx::ApplicationLogger::setMaximumEntries ( int max)

Sets the maximum number of entries to be kept in the log before the oldest one is purged.

Note
This must be called before the log is opened or else it will have no effect.
See also
maximumEntries().

◆ status()

IoOpReport Qx::ApplicationLogger::status ( ) const

Returns the error status of the logger.

The status is a report of the last operation performed by ApplicationLogger. If no operation has been performed since the logger was constructed or resetStatus() was last called the report will be null.

See also
resetStatus().

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