正在安装消息处理程序

Installing message handler

本文关键字:程序 消息处理 安装      更新时间:2023-10-16

我尝试通过使用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] %sn", msg);
        break;
    case QtWarningMsg:
        fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
        fprintf(fd, "[Warning] %sn", msg);
        break;
    case QtCriticalMsg:
        fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
        fprintf(fd, "[Critical] %sn", msg);
        break;
    case QtFatalMsg:
        fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
        fprintf(fd, "[Fatal] %sn", 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,常量QMessageLogContext&,常量QString&)}"的转换无效[-fpermission]

这里有谁以前也遇到过同样的问题?

看起来您切换到Qt5并使用了Qt4.x中定义的消息处理程序函数签名

void myMessageOutput(QtMsgType type,
                     const QMessageLogContext &context,
                     const QString &msg)
{
    [..]
}

相反。