Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

Logging with cxxtools

Overview

Cxxtools contains a meta-logging-library which enables applications to switch the used logging-library with compile-switches. Cxxtools supports 4 kinds of logging:

Logging-API

To enable logging with cxxtools you need to initialize logging with log_init. This is a macro to a function-call, which depends on the used logging-library. This is defined in cxxtools/loginit.h.

Define in every file of your application, which wants to log something, a logging-category with log_define. This is a macro with one argument. This is defined in cxxtools/log.h.

To log something you use simple macros log_fatal, log_error, log_warn, log_info and log_debug, which gives you a std::ostream, where you can write your messages to. These are defined in cxxtools/log.h.

Link the cxxtools library to your application.

Minimum example

myapp.cpp

#include <cxxtools/loginit.h>
#include <cxxtools/log.h>

log_define("myapp")

int main()
{
  int ret = 0;
  log_init();
  log_debug("this is a debug-message");
  log_info("I will return " << ret);
  return ret;
}

Makefile for it

CC=g++
LDFLAGS=-lcxxtools
myapp: myapp.o

If you run make, a application named "myapp" is built. When run, it does nothing. To see the log messages, you need to enable logging with a property-file. The name of the property-file is either found in the environmentvariable LOGPROPERTIES or by default "log.properties" is used (at least with the default logging library).

Create now a file "log.properties":

rootlogger=INFO
logger.myapp=DEBUG

If you run the application now, the log messages are printed to stdout. You can create a more detailed configuration file including comments using "cxxtools-config --properties myapp >log.properties

There is a more sophisticated exampleprogram at demo/log.cpp in the source-distribution. There you can find also some example-propertyfiles for the different libraies.

Copyright © 2008 The Tntnet Development Team
Tntnet 1.6