Project

General

Profile

How to: Use loggers in commandline applications

For the new oflog classes, there are some rules which should be considered when writing command line applications. The following steps should be done, if possible:

  1. Define commandline options as done with OFCommandLine::addOption() and the like. If console output is needed here, COUT and CERR should be used.
  2. Standard commandline logging options should be added with OFLog::addOptions(cmd) where cmd is an OFCommandLine object. Please use the correct position!
  3. Evaluate commandline using OFConsoleApplication::parseCommandLine() and set program variables depending on command line options, OFCommandLine::findOption() and the like. If logging is needed here, app.printError() can be used, or COUT for normal messages. For conflicts and dependencies of options, use OFConsoleApplication::checkConflict() and OFConsoleApplication::checkDependence().
  4. The logging options can be evaluated using OFLog::configureFromCommandLine(cmd, app) where cmd is an OFCommandLine and app is an OFConsoleApplication object.
  5. After basic commandline evaluation (writing commandline options into variables), print out resource identifier using the DEBUG logger, e.g. in storescp this is OFLOG_DEBUG(storescpLogger, rcsid << OFendl); .
  6. From this point, the loggers can be used as needed, i.e. COUT / CERR / printf or anything should not be used any more but everything should go to the loggers instead.

Note

Please note that all DCMTK commandline tools call prepareCmdLineArgs() in the main() function which causes stderr to be mapped to stdout on Windows systems (since previous versions of the Windows operating system could not redirect stderr).