[Qt-interest] install message handler

Sean Harmer sean.harmer at maps-technology.com
Thu Sep 2 11:45:33 CEST 2010


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



More information about the Qt-interest-old mailing list