1.首先定义一个日志类
#ifndef GUIAPPENDER_H
#define GUIAPPENDER_H
#include "dcmtk/oflog/config.h"
#include "dcmtk/oflog/appender.h"
#include "dcmtk/oflog/helpers/property.h"
#include <sstream>
namespace log4cplus
{
class GuiAppender : public dcmtk::log4cplus::Appender
{
public:
GuiAppender();
GuiAppender(const dcmtk::log4cplus::helpers::Properties& properties, dcmtk::log4cplus::tstring& error);
virtual ~GuiAppender();
// Methods
virtual void close();
protected:
virtual void append(const dcmtk::log4cplus::spi::InternalLoggingEvent& event);
//声明一个ostringsteam对象
STD_NAMESPACE ostringstream outString ;
private:
// Disallow copying of instances of this class
GuiAppender(const GuiAppender&);
GuiAppender& operator=(const GuiAppender&);
};
} // end namespace log4cplus
#endif // GUIAPPENDER_H
#include "GuiAppender.h"
#include <string>
#include <QDebug>
#include <QString>
log4cplus::GuiAppender::GuiAppender()
{
}
log4cplus::GuiAppender::GuiAppender(const dcmtk::log4cplus::helpers::Properties& properties, dcmtk::log4cplus::tstring&)
: Appender(properties)
{
}
log4cplus::GuiAppender::~GuiAppender()
{
}
void log4cplus::GuiAppender::close()
{
}
// This method does not need to be locked since it is called by
// doAppend() which performs the locking
void log4cplus::GuiAppender::append(const dcmtk::log4cplus::spi::InternalLoggingEvent& event)
{
//格式化输入DCMTK日志
layout->formatAndAppend(outString, event);
//获取DCMTK日志字符串流
STD_NAMESPACE string m = outString.str();
//将C++标准字符串流,转换成QString
QString t = QString::fromStdString(m);
//输出调试信息
qDebug() << t;
//清空
outString.str("");
}
2.在入口文件处添加
OFLog::configure(OFLogger::DEBUG_LOG_LEVEL);
OFLOG_DEBUG(echoscuLogger, "OFLOG_DEBUG");
OFauto_ptr<dcmtk::log4cplus::Layout> layout(new dcmtk::log4cplus::PatternLayout("%D{%Y-%m-%d %H:%M:%S.%q} %5p: %m%n"));
//建立Gui日志输出类
dcmtk::log4cplus::SharedAppenderPtr guiAppender(new log4cplus::GuiAppender());
guiAppender->setLayout(layout);
//获取全局日志对象
dcmtk::log4cplus::Logger log = dcmtk::log4cplus::Logger::getRoot();
//去除所有日志输出类
log.removeAllAppenders();
//加入Gui输出类
log.addAppender(guiAppender);
//设置日志输出层
log.setLogLevel(OFLogger::DEBUG_LOG_LEVEL);
//测试输出一个error日志
OFLOG_ERROR(log, "There are six log levels and each provides a OFLOG_level() macro");