A starter project...

You've probably looked at the scenarios, at basic_usage example, and just in case, here's a something to get you started in your own project(s).

In your project, you will need to create 2 files:

Again, this is just a starter project, you can customize it as you wish. Enjoy!

The my_app_log.h file

// my_app_log.h
#ifndef my_app_LOG_H_header
#define my_app_LOG_H_header

#include <boost/logging/format_fwd.hpp>

// uncomment if you're using Named Formatters and Destinations
//#include <boost/logging/format/named_write_fwd.hpp>

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

namespace bl = boost::logging;
typedef bl::tag::holder<
    // string class
    // tags
    bl::tag::thread_id, bl::tag::time> log_string_type;
// note: if you don't use tags, you can simply use a string class:
// typedef bl::optimize::cache_string_one_str<> log_string_type;
BOOST_LOG_FORMAT_MSG( log_string_type )

using namespace boost::logging::scenario::usage;
typedef use<
        //  how often do you manipulate (change) the filter?
        //  does the filter use levels?
        // how often do you manipulate (change) the logger?
        // for the logger: do you favor speed or correctness?
        logger_::favor::correctness> finder;

BOOST_DECLARE_LOG_FILTER(g_l_filter, finder::filter)
BOOST_DECLARE_LOG(g_l, finder::logger)

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

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


The my_app_log.cpp file

// my_app_log.cpp
#include "my_app_log.h"
#include <boost/logging/format.hpp>
#include <boost/logging/format/formatter/tags.hpp>

// uncomment if you're using Named Formatters and Destinations
// #include <boost/logging/format/named_write.hpp>

using namespace boost::logging;

BOOST_DEFINE_LOG_FILTER(g_log_filter, finder::filter ) 
BOOST_DEFINE_LOG(g_l, finder::logger) 

void init_logs() {
    // Add formatters and destinations
    // That is, how the message is to be formatted...
    g_l()->writer().add_formatter( formatter::tag::thread_id() );
    g_l()->writer().add_formatter( formatter::tag::time("$hh:$mm.$ss ") );
    g_l()->writer().add_formatter( formatter::idx() );
    g_l()->writer().add_formatter( formatter::append_newline() );

    //        ... and where should it be written to
    g_l()->writer().add_destination( destination::cout() );
    g_l()->writer().add_destination( destination::dbg_window() );
    g_l()->writer().add_destination( destination::file("out.txt") );

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