Boost C++ LibrariesSourceForge.net Logo

PrevUpHomeNext

Tutorial

Trivial logging
Trivial logging with filters
Setting up sinks
Log record formatting
Creating loggers and writing logs
Adding more information to log: Attributes

In this section we shall walk through the essential steps to get started with the library. After reading it you should be able to initialize the library and add logging to your application. The code of this tutorial is also available in examples residing in the libs/log/examples directory. Feel free to play around with them, compile and see the result.

For those who don't want to read tons of clever manuals and just need a simple tool for logging, here you go:

#include <boost/log/trivial.hpp>

int main(int, char*[])
{
    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
    BOOST_LOG_TRIVIAL(info) << "An informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
    BOOST_LOG_TRIVIAL(error) << "An error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
}

The BOOST_LOG_TRIVIAL macro accepts a severity level and results in a stream-like object that supports insertion operator. As you can see, this library usage pattern is quite similar to what you would do with std::cout. However, the library offers a few advantages:

  1. The logging result is written to a text file, rather than the console.
  2. Besides the record message, each log record in the file contains a line number, a timestamp, thread identifier and severity level.
  3. It is safe to write logs from different threads concurrently.

It must be said that the macro, along with other similar macros provided by the library, is not the only interface that the library offers. It is possible to issue log records without using any macros at all.


PrevUpHomeNext