Getting started - the Fast & Furious way...

So, you don't have time to spend (at least not right now), to read the tutorial, but still want to use the Boost Logging Lib.



The basics

Here are the quick facts:

There are several writers. The most common uses the concept of formatters and destinations:

Once you have a writer, you can add several formatters and/or destinations. The writer object knows how to call them. The common (and default) one calls them like this:

The easiest writer is the Named Writer. It's an easy interface to using formatters and destinations. In other words, you set a format string and a destination string.



Structuring your code

You'll use macros to:

You should structure your code like this:



Example 1 : Have one Named Writer, No levels

Assume you want one logger and one filter - the filter is a simple filter that can only be turned on/off (the filter is not thread-safe).
This is something you can copy-paste into your program.

// my_app_log.h - DECLARE your loggers & filters here
#ifndef my_app_LOG_H_header
#define my_app_LOG_H_header

#include <boost/logging/format/named_write_fwd.hpp>
// #include <boost/logging/writer/on_dedicated_thread.hpp> // uncomment if you want to do logging on a dedicated thread

namespace bl = boost::logging;
typedef bl::named_logger<>::type logger_type;
typedef bl::filter::no_ts filter_type;

BOOST_DECLARE_LOG_FILTER(g_l_filter, filter_type)
BOOST_DECLARE_LOG(g_l, logger_type)

#define L_ BOOST_LOG_USE_LOG_IF_FILTER( g_l(), g_log_filter()->is_enabled() ) 

// initialize thy logs..
void init_logs();

#endif

// my_app_log.cpp - DEFINE your loggers & filters here
#include "my_app_log.h"
#include <boost/logging/format/named_write.hpp>

BOOST_DEFINE_LOG_FILTER(g_log_filter, filter_type ) 
BOOST_DEFINE_LOG(g_l, logger_type) 


void init_logs() {
    // formatting    : time [idx] message \n
    // destinations  : console, file "out.txt" and debug window
    g_l()->writer().write("%time%($hh:$mm.$ss.$mili) [%idx%] |\n", "cout file(out.txt) debug");
    g_l()->mark_as_initialized();
}

void use_logger() {
    int i = 1;
    L_ << "this is a simple message " << i;
    std::string hello = "hello";
    L_ << hello << " world";
}



Example 2 : Have one Named Writer , Use Levels

Assume you want one logger and one filter - the filter is a based on levels (the filter is not thread-safe).
This is something you can copy-paste into your program.

// my_app_log.h - DECLARE your loggers & filters here
#ifndef my_app_LOG_H_header
#define my_app_LOG_H_header

#include <boost/logging/format/named_write_fwd.hpp>
// #include <boost/logging/writer/on_dedicated_thread.hpp> // uncomment if you want to do logging on a dedicated thread

namespace bl = boost::logging;
typedef bl::named_logger<>::type logger_type;
typedef bl::level::holder filter_type;

BOOST_DECLARE_LOG_FILTER(g_l_level, filter_type)
BOOST_DECLARE_LOG(g_l, logger_type)

#define L_(lvl) BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_l_level(), lvl )

// initialize thy logs..
void init_logs();

#endif

// my_app_log.cpp - DEFINE your loggers & filters here
#include "my_app_log.h"
#include <boost/logging/format/named_write.hpp>

BOOST_DEFINE_LOG_FILTER(g_l_level, filter_type ) 
BOOST_DEFINE_LOG(g_l, logger_type) 


void init_logs() {
    // formatting    : time [idx] message \n
    // destinations  : console, file "out.txt" and debug window
    g_l()->writer().write("%time%($hh:$mm.$ss.$mili) [%idx%] |\n", "cout file(out.txt) debug");
    g_l()->mark_as_initialized();
}

void use_logger() {
    int i = 1;
    L_(debug) << "this is a simple message " << i;
    std::string hello = "hello";
    L_(info) << hello << " world";
}

Other examples...

Yup, we have other examples as well. We also have a starter project.

Copyright John Torjo © 2007
Have a question/ suggestion/ comment? Send me feedback