PDA

View Full Version : Error while calling installing singleton class Qt Message Handler member function



Ahmed Abdellatif
19th January 2018, 23:39
I am trying to direct the Qt debugging messages into a text log file. while trying to install the message handler function i have written, the following error occurred:

main.cpp: error: C3867: 'Log::myMessageHandler': function call missing argument list; use '&Log::myMessageHandler' to create a pointer to member
please note that :

1. I have declared the message handler function myMessageHandler as a member function in the singleton Logger class.

2. myMessageHandler function was accessed using the [B]static method ReturnInstance().
3. The function myMessageHandler was installed in the main function of my application using qInstallMessageHanler() function.

The code is as follows:

/****************************Log.h***************** ******/
class Log : public QObject
{
Q_OBJECT
public:
//class member declaration

static Log& ReturnInstance()
{
static Log Log_obj; //The only Log object that will be used
return Log_obj;
}
void myMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg);
private:
//some member variable declartion
QFile m_file;

};
/****************************Log.cpp*************** ********/
//constructor definition
//destructor definition
void Log::myMessageHandler(QtMsgType type, QMessageLogContext &context, const QString &msg)
{

QTextStream ostream(&m_file);

switch (type) {
case QtDebugMsg:
ostream<<("Debugging Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
case QtInfoMsg:
ostream<<("Information Message: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function);
break;
case QtWarningMsg:
ostream<<("Warnning Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
case QtFatalMsg:
ostream<<("Fatal Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
case QtCriticalMsg:
ostream<<("Critical Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
break;
default:
break;
}
}
And here is the installation of the message handler function:

#include "mainwindow.h"
#include <QApplication>
#include "logger.h"

int main(int argc, char *argv[])
{
Log::ReturnInstance();
qInstallMessageHandler(Log::ReturnInstance().myMes sageHandler); //<------ The error
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

d_stranz
20th January 2018, 17:47
From the docs:


typedef QtMessageHandler

This is a typedef for a pointer to a function with the following signature:

void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);

The important words are pointer to a function, not pointer to a MEMBER function. This typically means a stand-alone function, but in your case you might be able to use a static member function of your Log class.

However, I would turn your design inside-out. Implement the message handler as a stand-alone function, but inside the handler, relay the message to the singleton's handler:




class Log : public QObject
{
Q_OBJECT
public:
//class member declaration

static Log& ReturnInstance()
{
static Log Log_obj; //The only Log object that will be used
return Log_obj;
}
void myMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg);

private:
//some member variable declartion
QFile m_file;

};

static void theMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg)
{
Log & log = Log::ReturnInstance();
log.myMessageHandler( type, context, msg );
}

#include "mainwindow.h"
#include <QApplication>
#include "logger.h"

int main(int argc, char *argv[])
{
qInstallMessageHandler( theMessageHandler ); //<------ The former error
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

Ahmed Abdellatif
20th January 2018, 23:32
Thanks a lot