Logging Events With BoostLog

Logging Events With BoostLog

By ICS Development Team

In this post, we'll look at Boost::Log, a facility of the Boost C++ libraries that makes it easy to add logging to applications.

QMessageLogger 1 was introduced in Qt 5.0 as a means of providing a framework for logging application messages. While QMessageLogger is useful as a lightweight logging tool when in debug mode, it fails to provide the flexibility and features necessary to suit many application requirements.

Boost::Log V2 2 was first released with Boost 1.54 and provides a flexible, robust framework that is capable of supporting a variety of application logging requirements.

Multi-threaded applications are supported. Although all of the default functionality is thread-safe, care does need to be taken to ensure correct record ordering when multiple threads are accessing the same log.

In the Boost::Log architecture, the application feeds log records through the logging core. Using application-defined filters, the core routes the log records to front-end and back-end sinks which filter and format the records before delivering them to the final destination. The final destination, as defined by the back-end sink, could be the console, a file, or even fed back to the application to present some form of alarm.

Filters can be passed to the front-end sink as methods or lambda expressions. For example, filters can be used to determine whether to display a message of a pre-defined severity.

There are many types of back-end sinks for text streams or files. For example, if you wanted your log to be in the form of an XML file, automatic headers and footers can be defined to write out the opening and closing tags. Additionally, character decorators can be defined to pass legal tokens through. Similar functionality is available to create CSV files.

Specialized back-ends exist to write to the Windows Event log, Windows Debugger or to the network-based syslog.

Incorporating Boost::Log within a Qt application is a straightforward process. The example 3 included here is a simple simulation of a heater device. The device must log its temperature at five Hertz when it is not idle. This application generates two logs. One log is the temperature log, which records the time, set point and actual temperature data. The other log is an event log, which records device state changes and errors. A screen shot of the application running is shown below.

Below is some sample output from the event log:

*****  Heater Simulation Started *****
[2015.04.22 15:11:06.520158] |notification| [Heater on]
[2015.04.22 15:11:06.567052] |notification| [Hold temperature achieved]
[2015.04.22 15:11:06.695881] |notification| [Heater off]
[2015.04.22 15:11:06.879938] |notification| [Heater on]
[2015.04.22 15:11:06.966462] |notification| [Hold temperature achieved]
[2015.04.22 15:11:08.151715] |notification| [Heater off]
[2015.04.22 15:11:08.495861] |notification| [Heater on]
[2015.04.22 15:11:08.566418] |notification| [Hold temperature achieved]
[2015.04.22 15:11:08.687900] |notification| [Heater off]
***** Heater Simulation Concluded *****

The example was built and tested on an Ubuntu Linux 14.04 system with Qt 5.4.1 and Boost versions 1.54 and 1.57. It also compiles with Qt version 4. On Ubuntu Linux, you can install all of the Boost development packages by installing the package libboost-all-dev. You may need to adjust the qmake project file for the location where Boost is installed on your system.

References

  1. Documentation for the QMessageLogger class, accessed 28 May 2015, http://doc.qt.io/qt-5/qmessagelogger.html
  2. Boost Documentation for boost.Log v2, accessed 28 May, 2015, http://www.boost.org/doc/libs/1_57_0/libs/log/doc/html/index.html
  3. Source code for example application, ftp://ftp.ics.com/pub/pickup/Logger.zip