1
|
QsLog - the simple Qt logger
|
2
|
-------------------------------------------------------------------------------
|
3
|
QsLog is an easy to use logger that is based on Qt's QDebug class.
|
4
|
|
5
|
Features
|
6
|
-------------------------------------------------------------------------------
|
7
|
* Six logging levels (from trace to fatal)
|
8
|
* Logging level threshold configurable at runtime.
|
9
|
* Minimum overhead when logging is turned off.
|
10
|
* Multiple destinations, comes with file and debug destinations.
|
11
|
* Thread-safe
|
12
|
* Logging of common Qt types out of the box.
|
13
|
* Immediate logging or queueing messages in a separate thread.
|
14
|
* Small dependency: just drop it in your project directly.
|
15
|
|
16
|
Usage
|
17
|
-------------------------------------------------------------------------------
|
18
|
By directly including QsLog in your project:
|
19
|
1. Include QsLog.pri in your pro file
|
20
|
2. Include QsLog.h in your C++ files. Include QsLogDest.h only where you create/add destinations.
|
21
|
3. Get the instance of the logger by calling QsLogging::Logger::instance();
|
22
|
4. Optionally set the logging level. Info is default.
|
23
|
5. Create as many destinations as you want by using the QsLogging::DestinationFactory.
|
24
|
6. Add the destinations to the logger instance by calling addDestination.
|
25
|
7. Start logging!
|
26
|
Note: when you want to use QsLog both from an executable and a shared library you have to
|
27
|
link dynamically with QsLog due to a limitation with static variables.
|
28
|
|
29
|
By linking to QsLog dynamically:
|
30
|
1. Build QsLog using the QsLogSharedLibrary.pro.
|
31
|
2. Add the QsLog shared library to your LIBS project dependencies.
|
32
|
3. Follow the steps in "directly including QsLog in your project" starting with step 2.
|
33
|
|
34
|
Configuration
|
35
|
-------------------------------------------------------------------------------
|
36
|
QsLog has several configurable parameters:
|
37
|
* defining QS_LOG_LINE_NUMBERS in the .pri file enables writing the file and line number
|
38
|
automatically for each logging call
|
39
|
* defining QS_LOG_SEPARATE_THREAD will route all log messages to a separate thread.
|
40
|
|
41
|
Sometimes it's necessary to turn off logging. This can be done in several ways:
|
42
|
* globally, at compile time, by enabling the QS_LOG_DISABLE macro in the .pri file.
|
43
|
* globally, at run time, by setting the log level to "OffLevel".
|
44
|
* per file, at compile time, by including QsLogDisableForThisFile.h in the target file.
|
45
|
|
46
|
Thread safety
|
47
|
-------------------------------------------------------------------------------
|
48
|
The Qt docs say: A thread-safe function can be called simultaneously from multiple threads,
|
49
|
even when the invocations use shared data, because all references to the shared data are serialized.
|
50
|
A reentrant function can also be called simultaneously from multiple threads, but only if each
|
51
|
invocation uses its own data.
|
52
|
|
53
|
Since sending the log message to the destinations is protected by a mutex, the logging macros are
|
54
|
thread-safe provided that the log has been initialized - i.e: instance() has been called.
|
55
|
The instance function and the setup functions (e.g: setLoggingLevel, addDestination) are NOT
|
56
|
thread-safe and are NOT reentrant.
|
57
|
|
58
|
IMPORTANT: when using a separate thread for logging, your program might crash at exit time on some
|
59
|
operating systems if you won't call Logger::destroyInstance() before your program exits.
|
60
|
This function can be called either before returning from main in a console app or
|
61
|
inside QCoreApplication::aboutToQuit in a Qt GUI app.
|
62
|
The reason is that the logging thread is still running as some objects are destroyed by
|
63
|
the OS. Calling destroyInstance will wait for the thread to finish.
|
64
|
Nothing will happen if you forget to call the function when not using a separate thread
|
65
|
for logging.
|