[Qt-interest] install message handler
Sajjad
dosto.walla at gmail.com
Thu Sep 2 12:49:56 CEST 2010
Hello Sean,
Thanks for the suggestion. I have tried your code snippet with very minor
changes and i am getting empty file as before.
*************************************
#include "Util.h"
#include <iostream>
QTextStream g_nonstderr;
QFile g_debugLogFile;
QMutex g_mutex;
void debugMessageHandler(QtMsgType type, const char *msg)
{
g_mutex.lock();
switch ( type )
{
case QtDebugMsg:
g_nonstderr << QString( msg ) << endl;
break;
case QtWarningMsg:
g_nonstderr << "Warning:" << msg << endl;
break;
case QtCriticalMsg:
g_nonstderr << "Critical:" << msg << endl;
break;
case QtFatalMsg:
g_nonstderr << "Fatal:" << msg << endl;
abort();
}
g_mutex.unlock();
}
void setupDebugRedirection()
{
//static QFile *g_debugLogFile = new QFile();
g_debugLogFile.setFileName("debugOut.txt");
g_nonstderr.setDevice( &g_debugLogFile );
g_debugLogFile.open( QIODevice::WriteOnly | QIODevice::Text );
qInstallMsgHandler( debugMessageHandler );
}
***********************************'***
Any other hint to look for ?
Regards
Sajjad
On Thu, Sep 2, 2010 at 11:45 AM, Sean Harmer <
sean.harmer at maps-technology.com> wrote:
> Hi,
>
> On Thursday 02 September 2010 10:28:17 Sajjad wrote:
> > I am trying all my debug output to a file and i tried using the
> > qInstallMsgHandler.the function pointer contains the following
> definition:
> >
> > ************************************************
> >
> > #include "Util.h"
> >
> > #include <iostream>
> >
> > void debugMessageHandler(QtMsgType type, const char *msg)
> > {
> > static QFile *file = new QFile(":/H3D/debugOut.txt");
> >
> > if(!file->exists())
> > std::cout << "file does not exist" << std::endl;
> > else
> > std::cout << "File is there" << std::endl;
> >
> > QTextStream out(file);
> > switch(type)
> > {
> > case QtDebugMsg:
> > out << "Debug" << QString(msg);
> > break;
> > }
> > }
> >
> > ********************************************************************
> >
> >
> > I install the handler in the main function and it is supposed to be
> > functional from anywhere in the project, right?
>
> Yes.
>
> > After every debug output with qDebug() i chcek the contents of the file
> and
> > it is still empty.
> >
> > Is there anything i am missing in the process?
>
> Two things by the looks of it.
>
> 1). You are trying to use an internal resource system file since your
> filename
> begins with a ":". This won't work since such resources are compiled into
> your
> application and so are read-only.
>
> 2). You do not open the file for writing anywhere.
>
> In the past we have used something like this:
>
> QFile g_debugLogFile;
> QTextStream g_nonstderr;
> QMutex g_mutex;
>
> void msgRedirection( QtMsgType type, const char *msg )
> {
> g_mutex.lock();
> switch ( type )
> {
> case QtDebugMsg:
> g_nonstderr << QString( msg ) << endl;
> //fprintf(&nonstderr, "Debug: %s\n", msg);
> break;
> case QtWarningMsg:
> g_nonstderr << "Warning:" << msg << endl;
> //fprintf(&nonstderr, "Warning: %s\n", msg);
> break;
> case QtCriticalMsg:
> g_nonstderr << "Critical:" << msg << endl;
> //fprintf(&nonstderr, "Critical: %s\n", msg);
> break;
> case QtFatalMsg:
> g_nonstderr << "Fatal:" << msg << endl;
> //fprintf(&nonstderr, "Fatal: %s\n", msg);
> abort();
> }
> g_mutex.unlock();
> }
>
> void setupDebugRedirection()
> {
> g_debugLogFile.setFileName( "/var/log/my-log-file.log" );
> g_nonstderr.setDevice( &g_debugLogFile );
> g_debugLogFile.open( QIODevice::WriteOnly | QIODevice::Text );
> qInstallMsgHandler( msgRedirection );
> }
>
> Then just call setupDebugRedirection() somewhere in main(). We used a mutex
> in
> the msg handler function because we were usign qDebug() and friends from
> several threads and QTextStream is not thread-safe.
>
> NB This took us a long time to realise. We thought we had a race condition
> somewhere else in our threading code and the more qDebug() statements we
> added
> the worse the problem got. We had forgotten about the innocent looking msg
> handler function. Oh well, lesson learned ;-)
>
> HTH,
>
> Sean
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100902/e5b9eb5a/attachment.html
More information about the Qt-interest-old
mailing list