QLogger
|
QLogger is made to be compatible with Qt framework logs management, this library provide an easy (and thread-safe) way to use multiple sinks behaviour.
main
branch. 1.0.x
: branch dev/1.0
1.1.x
: branch dev/1.1
Table of contents:
This library requires at least C++ 11 standard (see implementation section for more details)
Below, list of required dependencies:
Dependencies | Comments |
---|---|
Qt | Library built with Qt framework and compatible with series 5.15.x and 6.x |
This library can be use as an embedded library in a subdirectory of your project (like a git submodule for example):
This library only have to be initialized in the main
method of the application, then all calls to Qt logs methods (qDebug()
, qInfo()
, qWarning()
, etc...) of the application and used libraries will be redirected to QLogger. Multiple sinks behaviour are available:
QLogger::QLoggerFactory::initLoggerRotating()
QLogger::QLoggerFactory::initLoggerDaily()
Example:
This will configure QLogger to:
logs/log.txt
as main file (once rotated, generated files will be: logs/log1.txt
and logs/log2.txt
)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:
In this case, default Qt logger will be restablished. User can still use his own log behaviour with qInstallMessageHandler()
method (QLogger must have been desinitialized !)
desinit()
method, each sink init method will properly take care of this.By default, log message will be formatted as below, depending of type of build:
[utc-date][type-log] log-msg
[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:
This will log:
Format log message can also be customized by using a custom QLogger::LogFormatter
method, here an example:
User can also configure whether or not all messages should be logged. By default, all messages are logged but that can be changed by choosing minimum level to use (see Qt log level enum doc):
QtWarningMsg
, QtCriticalMsg
and QtFatalMsg
In order to easily check at compilation time library version (to manage compatibility between multiple versions for example), macro QLOGGER_VERSION_ENCODE
(defined inside qloggerglobal.h file) can be used:
At runtime, it is recommended to use the static method:
All classes/methods has been documented with Doxygen utility and automatically generated at online website documentation.
docs/fragments/Doxyfile-public-api.in
docs/fragments/Doxyfile-internal.in
To generate documentation locally, we can use:
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.
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 QLoggerFactory 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 from thread-race issues when trying to write to log to file for example).
This library is licensed under [MIT license][repo-license].