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:
- Define commandline options as done with
OFCommandLine::addOption()
and the like. If console output is needed here,COUT
andCERR
should be used. - Standard commandline logging options should be added with
OFLog::addOptions(cmd)
wherecmd
is an OFCommandLine object. Please use the correct position! - 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, orCOUT
for normal messages. For conflicts and dependencies of options, useOFConsoleApplication::checkConflict()
andOFConsoleApplication::checkDependence()
. - The logging options can be evaluated using
OFLog::configureFromCommandLine(cmd, app)
wherecmd
is anOFCommandLine
andapp
is anOFConsoleApplication
object. - After basic commandline evaluation (writing commandline options into variables), print out resource identifier using the DEBUG logger, e.g. in
storescp
this isOFLOG_DEBUG(storescpLogger, rcsid << OFendl);
. - 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
).