Reputation: 99
I have tried to create a log file for an application by installing a message handler using qinstallMessageHandler()
function. My program is as follows:
#include <QCoreApplication>
#include <QtDebug>
#include <QDir>
#include <fstream>
#include <iostream>
#include <QtCore>
#include <stdio.h>
#include <stdlib.h>
FILE *fd;
void myMessageOutput(QtMsgType type, const char *msg)
{
QString timeStamp = QTime::currentTime().toString("hh:mm:ss:zzz");
switch (type) {
case QtDebugMsg:
fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
fprintf(fd, "[Debug] %s\n", msg);
break;
case QtWarningMsg:
fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
fprintf(fd, "[Warning] %s\n", msg);
break;
case QtCriticalMsg:
fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
fprintf(fd, "[Critical] %s\n", msg);
break;
case QtFatalMsg:
fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
fprintf(fd, "[Fatal] %s\n", msg);
abort();
}
}
int main(int argc, char *argv[])
{
fd = fopen("log.txt", "a");
qInstallMessageHandler(myMessageOutput);
QCoreApplication a(argc, argv);
qDebug()<<"\t Hello World!!! \n";
return a.exec();
}
But after compiling I get the following error:
error: invalid conversion from 'void ()(QtMsgType, const char)' to 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&, const QString&)}' [-fpermissive]
Anyone here who have faced the same problem earlier?
Upvotes: 0
Views: 3179
Reputation: 21230
It looks like you switch to Qt5 and used the message handler function signature defined in Qt4.x. You need to declare your message handler as follows:
void myMessageOutput(QtMsgType type,
const QMessageLogContext &context,
const QString &msg)
{
[..]
}
instead.
Upvotes: 3