Chair for Computer Aided Medical Procedures & Augmented Reality
Lehrstuhl für Informatikanwendungen in der Medizin & Augmented Reality

Back to main page.

How to implement logging with log4cpp in Ubitrack

NOTE: This information applies to the log4cpp version in the Ubitrack library, which is slightly modified compared to the original version. If you somehow googled this page and want to find out how to use log4cpp in general, you are wrong here.

Step 1: Getting a logger

Before you can write a message to the logger, you have to get a reference to a logger first:

    #include <log4cpp/Category.hh>
    static log4cpp::Category& logger( log4cpp::Category::getInstance( "<category name>" ) );

Try to create a logger once and then keep the reference. Creating a new logger for every log message will cause performance problems!

By convention, the name of the catogory contains the namespace/class name, with components separated by ".", e.g.

If you want to log operations that happen in a running dataflow, please use the "Ubitrack.Events" prefix which has a lower default log level of NOTICE, e.g.

Step 2: Writing log messages

log4cpp knows the log levels FATAL, ALERT, CRIT, ERROR, WARN, NOTICE, INFO, DEBUG and TRACE. To write a message, please use the macro LOG4CPP_XXX (replace XXX by log level):

    LOG4CPP_INFO( logger, "I have something to say" );

If you want to concatenate multiple variables or output exotic types, you can use the standard streaming operator:

    int errorBadness = 8523;
    LOG4CPP_ERROR( logger, "There was an error of badness " << errorBadness );

Configuring log output

Inside a program, you can use the normal log4cpp methods to add layouts and appenders to different loggers. If you are calling Util::initLogging (which is called by initDataflow, do the configuration after that.

If you are using an application that uses Ubitrack, such as trackman or the UT console, you can configure logging at runtime by creating a file called log4cpp.conf in the applications's working directory. There you can specify different appenders (logging destination), log formats and log levels for different categories. An examplary configuration file for most purposes is given below:

log4j.rootCategory=ERROR, rolling, console ###win32debug

# Configuration for other categories
log4j.category.Ubitrack.Events.Dataflow=INFO, win32debug

# Configure appender "Console"
log4j.appender.console.layout.ConversionPattern=%d{%H:%M:%S.%l} %6p %20f:%-3l %m %c %n

# Configure appender "Win32 Debug"
log4j.appender.win32debug.layout.ConversionPattern=%d{%H:%M:%S.%l} %6p %20f:%-3l %m %c %n

# Configure appender "Rolling log file"
log4j.appender.rolling.maxFileSize=10000000       # size in byte
log4j.appender.rolling.maxBackupIndex=1           # keep one backup file
log4j.appender.rolling.layout.ConversionPattern=%d{%H:%M:%S.%l} %6p %20f:%-3l %m %c %n

A very useful appender when developping on Windows is the Win32DebugAppender. It allows to obtain the log inside the Visual Studio console, instead of the ugly Windows shell. To enable it, replace console by win32debug in the first line of code in above's configration file.

For a list of available appenders and their parameters, have a look at 3rd\log4cpp\src\PropertyConfiguratorImpl.cpp.

Edit | Attach | Refresh | Diffs | More | Revision r1.10 - 07 Jul 2011 - 20:46 - PeterKeitler

Lehrstuhl für Computer Aided Medical Procedures & Augmented Reality    rss.gif