qInstallMessageHandler() 不 return 文件行、文件名、函数名

qInstallMessageHandler() does not return fileline, filename, functioname

我有 qInstallMessageHandler() 将所有日志发送到外部文件,主要是 rownumber 错误,所以我可以看到错误的来源:

#include <QQmlApplicationEngine>
#include <QGuiApplication>
#include <QtQml>
#include <QCoreApplication>
#include <QtCore>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QtSql>
#include <QtDebug>
#include <QTextStream>
#include "mssql.h"
#include "cryption.h"
#include "led.h"


void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QDateTime datatime = QDateTime::currentDateTime();
    QString formattedDateTime = datatime.toString("dd.MM.yyyy hh:mm:ss");
    QByteArray localMsg = msg.toLocal8Bit();
    const char *file = context.file ? context.file : "";
    const char *function = context.function ? context.function : "";

    QString txt;
    switch (type) {
        case QtDebugMsg:
            txt = QString("%1: Debug: %2, file: %3, line: %4, function: %5").arg(formattedDateTime).arg(localMsg.constData()).arg(file).arg(context.line).arg(function);
        break;
        case QtInfoMsg:
            txt = QString("%1: Info: %2, file: %3, line: %4, function: %5").arg(formattedDateTime).arg(localMsg.constData()).arg(file).arg(context.line).arg(function);
        break;
        case QtWarningMsg:
            txt = QString("%1: Warning: %2, file: %3, line: %4, function: %5").arg(formattedDateTime).arg(localMsg.constData()).arg(file).arg(context.line).arg(function);
        break;
        case QtCriticalMsg:
            txt = QString("%1: Critical: %2, file: %3, line: %4, function: %5").arg(formattedDateTime).arg(localMsg.constData()).arg(file).arg(context.line).arg(function);
        break;
        case QtFatalMsg:
            txt = QString("%1: Fatal: %2, file: %3, line: %4, function: %5").arg(formattedDateTime).arg(localMsg.constData()).arg(file).arg(context.line).arg(function);
        break;
    }
    QFile outFile("debug.log");
    outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
    QTextStream ts(&outFile);
    ts.setCodec("UTF-8");
    ts << txt << endl;
}

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    qInstallMessageHandler(myMessageHandler);

    qmlRegisterType<MSSQL>("MSSQL", 1, 0, "MSSQL");
    qmlRegisterType<LED>("LED", 1, 0, "LED");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    // Sets path for SQLite
    engine.setOfflineStoragePath(QDir::currentPath());

    return app.exec();
}

但是 debug.log 文件看起来像:

30.06.2020 10:11:03: Debug: Terminal DB Error: No INI File Found!, file: , line: 0, function: 
30.06.2020 10:11:16: Warning: QSqlDatabasePrivate::removeDatabase: connection 'Terminal_DB_External' is still in use, all queries will cease to work., file: , line: 0, function: 
30.06.2020 10:11:16: Warning: QSqlDatabasePrivate::removeDatabase: connection 'ANeT_DB' is still in use, all queries will cease to work., file: , line: 0, function: 
30.06.2020 10:11:16: Warning: QSqlDatabasePrivate::removeDatabase: connection 'Terminal_DB_External' is still in use, all queries will cease to work., file: , line: 0, function: 
30.06.2020 10:11:16: Warning: QSqlDatabasePrivate::removeDatabase: connection 'ANeT_DB' is still in use, all queries will cease to work., file: , line: 0, function: 
30.06.2020 10:11:16: Debug: Terminal DB Error_1: "Driver not loaded Driver not loaded", file: , line: 0, function: 

有没有想过如何设置主要错误行以便我可以看到错误来自何处?文件名也是需要的,但如果不可能,我可以没有函数名。

如果您在调试模式下构建,则不会记录该信息。
如果您使用 QtCreator 作为 IDE,您可以使用左下角任务栏中项目按钮显示的菜单更改构建类型(从底部开始的第四个按钮,带有显示图标的按钮)。
如果您从命令行使用 qmake,请执行 'qmake CONFIG+="debug"'.
需要注意的一件事是您正在谈论的调试消息来自 qt 库本身,因此如果您以某种标准方式做事,您就可以了,但是如果您手动链接或使用自定义脚本请注意,信息取决于您链接的是 'debug' 还是 'release' 版本的 qtlibs。 QtLibs 系列的调试版本以 'd'.

为后缀