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
- 3. How to use
- 4. Documentation
- 5. Library details
- 6. License
- 7. Ressources
- C++11 compiler (see implementation section for more details)
This library can be use as an embedded library in a subdirectory of your project (like a git submodule for example):
- In the root CMakeLists, add instructions:
add_subdirectory(qlogger) # Or if library is put in a folder "dependencies" : add_subdirectory(dependencies/qlogger)- In the application CMakeLists, add instructions :
# Link QLogger library
target_link_libraries(${PROJECT_NAME} PRIVATE qlogger)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.txtas main file (once rotated, generated files will be:log1.txtandlog2.txt)- All debug messages will also be redirected to console (useful during development phase)
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 !)
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*))
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" ./DoxyfileNote: You can also load the Doxyfile into Doxywizard (Doxygen GUI) and run generation.
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.
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).
This library is licensed under MIT license.
- Qt ressources
- Singleton pattern
- Documentation utility
- C++ logs libraries: