A more complex example - a Line counter application

What the program does

This represents a small program to count lines of code from a directory and its subdirs. It's a simple application, just to show you how to use the Boost Logging Lib v2 in your own projects.

It counts code lines, empty lines, and comment lines. Again, it's a very simple program, thus:

Command line arguments:

About logging:

The highlights of setting up the logging are :

You can check out the whole example: libs/logging/samples/basic_usage.

The log.h file

#ifndef LOG_H_header
#define LOG_H_header

#include <boost/logging/format_fwd.hpp>

// Step 1: Optimize : use a cache string, to make formatting the message faster
BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> ) 

#ifndef BOOST_LOG_COMPILE_FAST
#include <boost/logging/format.hpp>
#include <boost/logging/writer/ts_write.hpp>
#endif

// Step 3 : Specify your logging class(es)
typedef boost::logging::logger_format_write< > log_type;

// Step 4: declare which filters and loggers you'll use
BOOST_DECLARE_LOG_FILTER(g_l_filter, boost::logging::level::holder)
BOOST_DECLARE_LOG(g_l, log_type)

// Step 5: define the macros through which you'll log
#define LDBG_ BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_l_filter(), debug ) << "[dbg] "
#define LERR_ BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_l_filter(), error ) << "[ERR] "
#define LAPP_ BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_l_filter(), info )

void init_logs();

#endif

The log.cpp file

#include "log.h"
#include <boost/logging/format.hpp>
#include <boost/logging/writer/ts_write.hpp>

using namespace boost::logging;

// Step 6: Define the filters and loggers you'll use
BOOST_DEFINE_LOG(g_l, log_type)
BOOST_DEFINE_LOG_FILTER(g_l_filter, level::holder)


void init_logs() {
    // Add formatters and destinations
    // That is, how the message is to be formatted...
    g_l()->writer().add_formatter( formatter::idx() );
    g_l()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
    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") );
    g_l()->turn_cache_off();
}

The main.cpp file

// main.cpp : Where the main() action is

// Wherever you use logs, include this ;)
#include "log.h"

#include <string>
#include <sstream>
#include <iostream>

#include "dir_spec.h"
#include "extensions.h"
#include "util.h"
#include <boost/filesystem/path.hpp>

using namespace boost::logging;
namespace fs = boost::filesystem;

int main(int argc, char * argv[])  
{
    fs::path::default_name_check( fs::no_check);

    init_logs();

    std::string dir = ".";
    if ( argc > 1)
        dir = argv[1];

    extensions ext;
    {
    std::string ext_str = "cpp;c;h;hpp";
    if ( argc > 2)
        ext_str = argv[2];
    str_replace(ext_str, ";", " ");
    str_replace(ext_str, ",", " ");
    str_replace(ext_str, ".", "");
    std::istringstream in( ext_str);
    std::string word;
    while ( in >> word)
        ext.add(word);
    }
    
    level::type lev = level::info;
    std::string lev_str = "info";
    if ( argc > 3) {
        lev_str = argv[3];
        if ( lev_str == "d") {
            lev = level::debug;
            lev_str = "debug";
        }
        else if ( lev_str == "i") {
            lev = level::info;
            lev_str = "info";
        }
        else if ( lev_str == "e") {
            lev = level::error;
            lev_str = "error";
        }
        else {
            LERR_ << "invalid verbosity " << lev_str << "; available options: d, i, e";
            lev_str = "info";
        }
    }
    g_l_filter()->set_enabled(lev);

    LAPP_ << "Verbosity: " << lev_str;

    dir_spec spec(dir, ext);
    spec.iterate();
        return 0;
}



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