【发布时间】:2013-11-21 07:34:59
【问题描述】:
我尝试通过使用qinstallMessageHandler() 函数安装消息处理程序来为应用程序创建日志文件。我的程序如下:
#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();
}
但编译后出现以下错误:
错误:从 'void ()(QtMsgType, const char)' 到 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&, const QString&)}'的无效转换 [-fpermissive]
这里有人遇到过同样的问题吗?
【问题讨论】:
-
检查消息处理程序所需的参数列表。它与您的功能不匹配(如果您不想阅读参考资料,错误消息会准确地告诉您想要什么)。