how to get the oflog in a Gui application?

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
qimo601
Posts: 32
Joined: Wed, 2012-06-06, 07:38

how to get the oflog in a Gui application?

#1 Post by qimo601 »

hi,all.

I have a problem about the oflog.
As it is said in the oflog moudle, we can only use oflog in the console application .
But how can i use it in a Qt Gui Application?
Because i want to catch the logs ,and show them in the software interface or VS2008 ouput window.

Also I find a answer in this forum which suggests we rewrite a class inherit log4cplus::Appender like the ConsoleAppender and FileAppender.

I need your help to rewrite this class to catch these logs.


Thanks
qimo

J. Riesmeier wrote:What about using the DCMTK_GUI macro as described in the documentation?

Alternatively, you could write your own appender based on the "oflog" framework ...


--------------------------
I find The ConsoleAppender's codes here, as shown in the following:

#include "dcmtk/oflog/layout.h"
#include "dcmtk/oflog/consap.h"
#include "dcmtk/oflog/streams.h"
#include "dcmtk/oflog/helpers/loglog.h"
#include "dcmtk/oflog/helpers/strhelp.h"
#include "dcmtk/oflog/spi/logevent.h"
#include "dcmtk/ofstd/ofconsol.h"

using namespace std;
using namespace log4cplus::helpers;


//////////////////////////////////////////////////////////////////////////////
// log4cplus::ConsoleAppender ctors and dtor
//////////////////////////////////////////////////////////////////////////////

log4cplus::ConsoleAppender::ConsoleAppender(bool logToStdErr_, bool immediateFlush_)
: logToStdErr(logToStdErr_),
immediateFlush(immediateFlush_)
{
}



log4cplus::ConsoleAppender::ConsoleAppender(const log4cplus::helpers::Properties properties, log4cplus::tstring&)
: Appender(properties),
logToStdErr(false),
immediateFlush(false)
{
tstring val = toLower(properties.getProperty(LOG4CPLUS_TEXT("logToStdErr")));
if(val == LOG4CPLUS_TEXT("true")) {
logToStdErr = true;
}
if(properties.exists( LOG4CPLUS_TEXT("ImmediateFlush") )) {
tstring tmp = properties.getProperty( LOG4CPLUS_TEXT("ImmediateFlush") );
immediateFlush = (toLower(tmp) == LOG4CPLUS_TEXT("true"));
}
}



log4cplus::ConsoleAppender::~ConsoleAppender()
{
destructorImpl();
}



//////////////////////////////////////////////////////////////////////////////
// log4cplus::ConsoleAppender public methods
//////////////////////////////////////////////////////////////////////////////

void
log4cplus::ConsoleAppender::close()
{
getLogLog().debug(LOG4CPLUS_TEXT("Entering ConsoleAppender::close().."));
closed = true;
}



//////////////////////////////////////////////////////////////////////////////
// log4cplus::ConsoleAppender protected methods
//////////////////////////////////////////////////////////////////////////////

// Normally, append() methods do not need to be locked since they are
// called by doAppend() which performs the locking. However, this locks
// on the LogLog instance, so we don't have multiple threads writing to
// tcout and tcerr
void
log4cplus::ConsoleAppender::append(const spi::InternalLoggingEvent& event)
{
LOG4CPLUS_BEGIN_SYNCHRONIZE_ON_MUTEX( getLogLog().mutex )
log4cplus::tostream& output = (logToStdErr ? ofConsole.lockCerr() : ofConsole.lockCout());
layout->formatAndAppend(output, event);
if(immediateFlush) {
output.flush();
}
if (logToStdErr) {
ofConsole.unlockCerr();
} else {
ofConsole.unlockCout();
}
LOG4CPLUS_END_SYNCHRONIZE_ON_MUTEX;
}

omarelgazzar
Posts: 101
Joined: Wed, 2009-07-08, 16:06
Location: Oldenburg, Germany

Re: how to get the oflog in a Gui application?

#2 Post by omarelgazzar »

Did you read this HowTo?

qimo601
Posts: 32
Joined: Wed, 2012-06-06, 07:38

Re: how to get the oflog in a Gui application?

#3 Post by qimo601 »

omarelgazzar wrote:Did you read this HowTo?

Thank you,I got it. :lol:

qimo601
Posts: 32
Joined: Wed, 2012-06-06, 07:38

Re: how to get the oflog in a Gui application?

#4 Post by qimo601 »

omarelgazzar wrote:Did you read this HowTo?

At last , I worked it out by inheriting Appender class .

I rewite a GuiAppender Class to catch DCMTK logs ,then I can ouput them on VS2008 Debug window.

Code: Select all

#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 Appender
	{

	public:
		GuiAppender();
		GuiAppender(const log4cplus::helpers::Properties& properties, log4cplus::tstring& error);
		virtual ~GuiAppender();

		virtual void close();

	protected:
		virtual void append(const log4cplus::spi::InternalLoggingEvent& event);
		//important 
		STD_NAMESPACE ostringstream outString ;
	private:
		GuiAppender(const GuiAppender&);
		GuiAppender& operator=(const GuiAppender&);

	};
} // end namespace log4cplus
#endif // GUIAPPENDER_H

Code: Select all

#include "GuiAppender.h"
#include <string>
#include <QDebug>
#include <QString>
log4cplus::GuiAppender::GuiAppender()
{

}
log4cplus::GuiAppender::GuiAppender(const log4cplus::helpers::Properties& properties, 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 spi::InternalLoggingEvent& event)
{
	
	layout->formatAndAppend(outString, event);
	STD_NAMESPACE string m = outString.str();
	QString t = QString::fromStdString(m);
	qDebug() << t;
}

Code: Select all

#include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */ 
#include "dcmtk/dcmnet/diutil.h" 
#include "DcmTestSCU.h"
#include <QtGui/QApplication>
#include <QDebug>
#include "dcmtk/oflog/oflog.h"
#include "GuiAppender.h"

 
int main(int argc, char *argv[]) 
{ 
	QApplication a(argc, argv);	

	/* specify log pattern */
	OFauto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout("%D{%Y-%m-%d %H:%M:%S.%q} %5p: %m%n"));

	//建立Gui日志输出类
	log4cplus::SharedAppenderPtr guiAppender(new log4cplus::GuiAppender());
	guiAppender->setLayout(layout);
	//获取全局日志对象
	log4cplus::Logger log = log4cplus::Logger::getRoot();
	//去除所有日志输出类
	log.removeAllAppenders();
	//加入Gui输出类
	log.addAppender(guiAppender);
	//设置日志输出层
	log.setLogLevel(OFLogger::INFO_LOG_LEVEL);
	//测试输出一个error日志
	OFLOG_ERROR(log, "There are six log levels and each provides a OFLOG_level() macro");
        return a.exec();
}

My Blog http://qimo601.iteye.com/blog/1684139

Image

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest