Skip to content

peerhuang/QLogger

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QLogger is made to be compatible with Qt framework logs management, this library provide an easy (and thread-safe) way to use log file rotation behaviour.

Table of contents:

1. Requirements

2. How to build

This library can be use as an embedded library in a subdirectory of your project (like a git submodule for example):

  1. In the root CMakeLists, add instructions:
add_subdirectory(qlogger) # Or if library is put in a folder "dependencies" : add_subdirectory(dependencies/qlogger)
  1. In the application CMakeLists, add instructions :
# Link QLogger library
target_link_libraries(${PROJECT_NAME} PRIVATE qlogger)

3. How to use

3.1. Initialization

This library only have to be initialized in the main method of the application, then all call to log Qt logs methods (qDebug(), qInfo(), qWarning(), etc...) of the application and used libraries will be redirected to QLogger. Below, a simple example:

#include "qlogger/qloghandler.h"

int main(int argc, char **argv)
{
    /* Set logs */
    QLogger::QLogHandler::instance().init("log.txt", 3, 1024 * 1024 * 5, true);

    /* Manage application properties */
    QApplication app(argc, argv);
    ...

    return app.exec();
}

This will configure QLogger for:

  • Number of logs files limited to 3
  • Log file max size is 5 megabytes (5Mb) (1 Kb = 1024 bytes, 1Mb = 1024 * 1024 bytes)
  • Use log.txt as main file (once rotated, generated files will be: log1.txt and log2.txt)
  • All debug messages will also be redirected to console (useful during development phase)

3.2. Desinitialization

QLogger library will properly destruct (and close) all used ressources automatically when application is closed. But if user need to quit QLogger manually, just use:

QLogger::QLogHandler::instance().desinit();

In this case, default Qt logger will be restablished. User can still use his own log behaviour with qInstallMessageHandler() method (QLogger must have been desinitialize !)

3.3. Log format

Log message will be formatted as below, depending of type of build:

  • Release: [utc-date][type-log] log-msg
  • Debug: [utc-date][type-log] log-msg (cpp-file:cpp-line, cpp-function)

If source file informations must appears on logs even on release mode, please use the associated macro in your main CMakefile: QT_MESSAGELOGCONTEXT.

So for example, when using:

const QString strValue = "nine";
const int value = 9;

qDebug() << "My value is:" << myValue;
qDebug("String [%s] converted as number is [%d]", qUtf8Printable(strValue), value);

This will log:

[2023-07-09T13:36:09Z][debug] My value is: 9 (mainwindow.cpp:14, MainWindow::MainWindow(QWidget*))
[2023-07-09T13:36:09Z][debug] String [nine] converted as number is [9] (mainwindow.cpp:15, MainWindow::MainWindow(QWidget*))

4. Documentation

All methods has been documented with Doxygen utility, documentation can be generated by using:

# Run documentation generation
doxygen ./Doxyfile

# Under Windows OS, maybe doxygen is not added to the $PATH
"C:\Program Files\doxygen\bin\doxygen.exe" ./Doxyfile

Note: You can also load the Doxyfile into Doxywizard (Doxygen GUI) and run generation.

5. Library details

5.1. Why another log library ?

We already have many good C++ logs libraries outside, so why to create a new one ? Since many of my custom application are built with Qt framework, I thought that having Qt framework as a dependency was already enough and didnt' want to use another just for logging. Besides, alternatives C++ logs libraries doesn't behave properly when used inside a library (.dll under Windows, .so under Linux) but Qt log framework allow such usage.
Only missing feature: file rotation with thread-safety.

Qt log module allow to easily add our custom log behaviour throught qInstallMessageHandler(). This library is just build around that extension feature.

5.2. Implementation

This library use the singleton pattern to manage QLogger instance. This singleton was implemented using Meyer's Singleton pattern, this implementation use static variable initialization to ensure thread-safety at initialization. This doesn't ensure your singleton to be thread-safe... only his initialization and desininitialization are !
Note that (at least !) a C++11 compiler is required (C++11 added requirement for static variable initialization to be thread-safe).

For more informations about Meyer's Singleton, see below:

Then, library thread-safety is ensured by using a mutex whenever a log message is received (to prevent for thread-race issues when trying to write to log file for example).

6. License

This library is licensed under MIT license.

7. Ressources

About

Custom Qt logger adding thread-safe file rotation behaviour

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C++ 75.8%
  • CMake 14.7%
  • C 9.5%