...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
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; }
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:
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.