Defining macros to do logging



Defining macros to do logging - prerequisites

Now that you've declared and defined your loggers/filters, it's time to log messages in code.

You'll have to define macros to log messages in code. Such a macro needs to do the following:

To see if the logger is enabled, you'll use a filter. Note that you can reuse the same filter for multiple loggers, in case you want to.

To define a macro to log messages in code, I've provided these few macros to help you:

  1. BOOST_LOG_USE_LOG_IF_FILTER(logger, filter_is_enabled)
  2. BOOST_LOG_USE_LOG_IF_LEVEL(logger, filter, level)
  3. BOOST_LOG_USE_SIMPLE_LOG_IF_FILTER(logger, filter_is_enabled)

The parameters are:

How you define your macros is up to you.



The easiest: Example of Logging, with a filter that can be turned on/off

Assume you have a filter that can only be turned on or off.

typedef logger_format_write< > logger_type;
typedef level::no_ts filter_type;

BOOST_DECLARE_LOG_FILTER(g_log_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() )


Using it in code:

L_ << "this is a cool message " << i++;
L_ << "this is another cool message " << i++;



Example of Logging, filtered by levels

Assuming you have a filter based on levels, you can:

Here's how you'd do each of the above:

typedef logger_format_write< > logger_type;
typedef level::holder filter_type;

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

// Example 1 - have one macro depending on one parameter
#define L_(lvl) BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_log_level(), lvl )


// Example 2
#define LDBG_ BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_log_level(), debug )
#define LERR_ BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_log_level(), error )
#define LAPP_ BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_log_level(), info )


Using it in code:

// Example 1
L_(debug) << "this is a debug message " << i++;
L_(info)  << "this is an info " << i++;
L_(error) << "this is an error " << i++;



// Example 2
LDBG_ << "this is a debug message " << i++;
LAPP_ << "this is an info " << i++;
LERR_ << "this is an error " << i++;



Logging with a filter based on file name

Another example. Assume you have a file_filter class, which filters based on file name (a file can be turned on or off):

struct file_filter {
    bool is_enabled(const char * file_name) const ; 
    // ...
};

In this case, the macro used to do logging would look like:

typedef logger_format_write< > logger_type;
typedef file_filter filter_type;

BOOST_DECLARE_LOG_FILTER(g_log_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(__FILE__) )


Using it in code:

L_ << "this is a message " << i++;
L_ << "hello world";



Logging with a filter based on module name

Another example. Assume you have a module_filter class, which filters based on module name (logging for a module can be turne on or off) :

struct module_filter {
    bool is_enabled(const char * module_name) const ; 
    // ...
};

In this case, the macro used to do logging would look like:

typedef logger_format_write< > logger_type;
typedef module_filter filter_type;

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

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


Using it in code:

L_("chart") << "activex loaded";
L_("connect") << "connecting to server";


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