Boost C++ LibrariesSourceForge.net Logo

PrevUpHomeNext

Tutorial

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

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 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";

    return 0;
}

See the complete code.

The BOOST_LOG_TRIVIAL macro accepts a severity level and results in a stream-like object that supports insertion operator. As a result of this code, the log messages will be printed on the console. 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. Besides the record message, each log record in the output contains a timestamp, the current thread identifier and severity level.
  2. It is safe to write logs from different threads concurrently, log messages will not be corrupted.
  3. As will be shown later, filtering can be applied.

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


PrevUpHomeNext