Error while calling installing singleton class Qt Message Handler member function
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:
Code:
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:
Code:
/****************************Log.h***********************/
{
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
};
/****************************Log.cpp***********************/
//constructor definition
//destructor definition
void Log::myMessageHandler(QtMsgType type, QMessageLogContext
&context,
const QString &msg
) {
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:
Code:
#include "mainwindow.h"
#include <QApplication>
#include "logger.h"
int main(int argc, char *argv[])
{
Log::ReturnInstance();
qInstallMessageHandler(Log::ReturnInstance().myMessageHandler); //<------ The error
MainWindow w;
w.show();
return a.exec();
}
Re: Error while calling installing singleton class Qt Message Handler member function
From the docs:
Quote:
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:
Code:
{
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
};
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
MainWindow w;
w.show();
return a.exec();
}
Re: Error while calling installing singleton class Qt Message Handler member function