OverviewLogging is an essential part of any application and can make it easier to figure out what is going on during application development and debugging. Qtilities provides a ready to use, thread safe and powerful logger which can easily be embedded into any Qt application. Logging is done through short and easy to use macros, or for more advanced logging requirements more sophisticated logging functions are available. Logger engines provide back-ends for the logger and messages are formatted on a per engine basis. New logger engines or formatting engines can easily be created and added to the logger. The logger is able to store its configuration between sessions and a ready to use logging configuration widget is available in GUI applications. The logger is only dependent on QtCore, thus it can be used in console or GUI applications. The Qtilities CoreGui library provides a widget logger engine which can be used with the logger in GUI applications. This article will show how to use the logger in a Qt application and provide an overview of the available features. Table of contents: |
|
The Qtilities::Logging::Logger provides macros to initialize it on application startup and to finalize in when the application exists. During initialization the logger will create standard logger engines and formatting engines and restore its settings from the previous session. The finalization does the opposite: it saves the current settings and deletes all the logger engines currently in the application.
Basic logging can be done through a set of macros which makes it very easy to log different types of messages. Lets look at a simple console application:
The above example showed basic usage of the logger in a console application, however in many applications the QtGui module is used and an user interface is provided to the user. In such applications it is desirable to have one or more ready to use logging widgets to display log messages to the user.
The Qtilities CoreGui library provides ready to use logging widgets which can be accessed through the Qtilities::CoreGui::LoggerGui singleton's static methods. For example we can get log dock widgets as follows:
The obtained dock widget will look similar to the image shown below:
The logging configuration widget can also be accessed using the Qtilities::CoreGui::LoggerGui class. For example:
The obtained configuration widget will look like the image shown below:
In the QtilitiesExamples
project there is a session log plugin which can be used in any application which makes use of the Qtilities extension system. For more information see the Plugins Overview section of the Examples and Plugins page.
The logger provides the ability to log priority messages. A priority message is handled in the same way as a normal message logged with logMessage() except that it also emits the newPriorityMessage() signal. This allows selective messages to be emitted and displayed in a convenient place in an application where all messages should not necessarily be displayed. An example is the case where selective messages should be displayed in a status bar. The Qtilities::CoreGui::QtilitiesMainWindow class provides ready to use support for priority messages.
Logging of priority messages is very similar to the macros already shown in this article. Below is an example where we log different types of priority messages.
The Qtilities logger allows logging to a specific engine where the name of the engine is used to determine the destination of the log messages. The following lines can be added to the example above as an example where we log specific messages to all three of the engines we created.
For more advanced logging operations the Qtilities::Logging::Logger::logMessage() function should be used directly. The logger singleton is directly accessible through the Log
macro which returns a pointer to the logger. The log message function allows additional parameters to be passed to the logger. See the function documentation for more information.
The logger groups messages using the concept of different message contexts. These contexts are described by the Qtilities::Logging::Logger::MessageContext enumeration. By default all logger engines accept messages from all message context. It is however possible to use specify combinations of message contexts explicitly for each individiual engine. For example:
The logger can easily be extended by creating new logger or formatting engines by creating classes which inherits from Qtilities::Logging::AbstractLoggerEngine and Qtilities::Logging::AbstractFormattingEngine respectively. See the class documentation for more information.
Qtilities : Reference Documentation | Back to top |
Copyright © 2009-2013, Jaco Naudé
|