DCMTK-Configure the logger from a program

Howto: Configure the logger from a program

The following example shows how to configure the logger from a program (without the need for a log config file):

#include "dcmtk/config/osconfig.h"
#include "dcmtk/oflog/oflog.h"
 
int main(int argc, char *argv[])
{
    /* enable verbose mode (info log level) */
    OFLog::configure(OFLogger::INFO_LOG_LEVEL);
 
   	//OFList<DcmFileFormat *> *sendFaths = new OFList<DcmFileFormat *>();
	//OFList<DcmFileFormat *> *sendFaths = (OFList<DcmFileFormat *> *)hde;
    /* ... */
}

Log Levels

The following log levels are available. Every log level in this list includes the details of the above level (e.g. the WARN_LOG_LEVEL also outputs all errors and thus also fatal errors).

  • FATAL_LOG_LEVEL: Only fatal error messages are printed.
  • ERROR_LOG_LEVEL: All errors are printed.
  • WARN_LOG_LEVEL: Warnings are printed.
  • INFO_LOG_LEVEL: The most important processing information is printed.
  • DEBUG_LOG_LEVEL: This is a pretty verbose logging level that is a good starting point for getting more information what actually happens in the code and to identify corresponding problems.
  • TRACE_LOG_LEVEL: This is the most verbose logging level and outputs many processing details.

Redirecting log output to a file

If the log output should be directed to a file or another log pattern should be used, we need some more lines of code:

#include "dcmtk/config/osconfig.h"
#include "dcmtk/oflog/fileap.h"
 
int main(int argc, char *argv[])
{
    /* specify log pattern */
    OFauto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout("%D{%Y-%m-%d %H:%M:%S.%q} %5p: %m%n"));
    /* Denote that a log file should be used that is appended to. The file is re-created every
       time the code gets to this point.
     */
    log4cplus::SharedAppenderPtr logfile(new log4cplus::FileAppender("example.log"));
    logfile->setLayout(layout);
 
    /* make sure that only the file logger is used */
    log4cplus::Logger log = log4cplus::Logger::getRoot();
    log.removeAllAppenders();
    log.addAppender(logfile);
    log.setLogLevel(OFLogger::INFO_LOG_LEVEL);
 
    /* ...*/
}

The above code re-creates the example.log file every time the code is encountered, i.e. if the complete program is restarted. If you want to append a file without deleting it, use the following line instead

  log4cplus::SharedAppenderPtr logfile(new log4cplus::FileAppender("example.log", STD_NAMESPACE ios::app));

No output at all (quiet mode)

If all log output should be discarded:

#include "dcmtk/config/osconfig.h"
#include "dcmtk/oflog/nullap.h"
 
int main(int argc, char *argv[])
{
    log4cplus::SharedAppenderPtr nullapp(new log4cplus::NullAppender());
 
    /* make sure that only the null logger is used */
    log4cplus::Logger log = log4cplus::Logger::getRoot();
    log.removeAllAppenders();
    log.addAppender(nullapp);
 
    /* ...*/
}

Note

Please note that the above sample code requires DCMTK 3.5.5 (20091222) or newer.

Please also note that the namespace has changed after the DCMTK 3.6.0 release from log4cplus to dcmtk::log4cplus with this commit.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值