DCMTK: oflog: a logging library based on log4cplus

DCMTK Version 3.6.9 OFFIS DICOM Toolkit

Loading...
Searching...
No Matches
oflog: a logging library based on log4cplus

This module contains classes which are used for logging purposes. This library is based on log4cplus.

The main classes are (in alphabetical order):

- OFLog
- OFLogger
- dcmtk::log4cplus::ConsoleAppender
- dcmtk::log4cplus::FileAppender
- dcmtk::log4cplus::PatternLayout

# Files

The following files provide sample configurations and further documentation:

- logger.cfg file
- filelog.cfg file
- consolog.cfg file

# Examples

The following example shows how to use oflog in a console application.

## The actual logging

First we need the necessary headers and definitions:

// Naturally, we need the header for oflog
#include "dcmtk/oflog/oflog.h"
// Then we create our logger object. The argument is the name of the logger
// which can be used to configure it from the config file
OFLogger my_log = OFLog::getLogger("dcmtk.apps.sample");
OFLog::getLogger
static OFLogger getLogger(const char *name)
create a new logger object.
OFLogger
simple wrapper around the "low-level" Logger object to make it easier to switch to a different system
Definition oflog.h:45

That is all that is necessary to create log statements. You can now use any of the following macros to generate log entries:

OFLOG_FATAL(my_log, "This is a sample message of log level 'fatal'");
OFLOG_ERROR(my_log, "There are six log levels and each provides a OFLOG_level() macro");
OFLOG_WARN(my_log, "These macros are quite flexible");
OFLOG_INFO(my_log, "To output numbers like " << 5 << " you can use any iostream operations");
OFLOG_DEBUG(my_log, "Since iostreams are quite flexible themselves, a lot of stuff is possible");
OFLOG_TRACE(my_log, "hex numbers? 0x" << STD_NAMESPACE hex << 0x1234 << " and decimal numbers "
<< STD_NAMESPACE dec << 0x1234 << " are no problem at all");

## Configuring the logging

While the above code works by itself, the result is not nice and flexible. To configure oflog with the help of OFCommandLine, you can do something like this:

// This is just an example
cmd.addGroup("general options:", LONGCOL, SHORTCOL);
// You own options here, e.g. --version
cmd.addOption("--version", "print version information and exit", OFCommandLine::AF_Exclusive);
// This call adds all of oflog's options, e.g. --debug and --quiet
OFLog::addOptions(cmd);
OFCommandLine::AF_Exclusive
static const int AF_Exclusive
exclusive option that overrides any other option (e.g. "--help")
Definition ofcmdln.h:972
OFLog::addOptions
static void addOptions(OFCommandLine &cmd)
add the command line options which configureFromCommandLine() checks for

After you called OFConsoleApplication::parseCommandLine(), oflog can be set up with one single call:

OFLog::configureFromCommandLine(cmd, app);
OFLog::configureFromCommandLine
static void configureFromCommandLine(OFCommandLine &cmd, OFConsoleApplication &app, OFLogger::LogLevel defaultLevel=OFLogger::WARN_LOG_LEVEL)
handle the command line options used for logging

This is all that is necessary to configure the logger and have options like `–verbose` and `–log-config` available.

Alternatively you can use OFLog::configure(), but this approach doesn't offer the flexibility of `–log-level` and `–log-config`.

The default pattern for the log messages is "%P: %m%n", i.e. first character of the log level (e.g. "D" for debug or "E" for error), a colon, the message and a line break.

Generated on Sat Mar 1 2025 for DCMTK Version 3.6.9 by Doxygen 1.13.2