Qt 调试信息重定向到本地文件

1、 在Qt软件开发过程中,我们经常使用qDebug()输出一些调试信息在QtCreator终端上。

但若将软件编译、生成、打包为一个完整的可运行的程序并安装在系统中后,系统中没有QtCreator和编译环境,那应用程序出现问题,如何输出信息排查呢?

**2、**一个好方法就是Qt调试信息重定向本地文件,即仍然使用qDebug()等函数,但设置后调试信息不输出在终端上,而是输出到指定路径的日志文件中,这样我们就可以通过日志进行调试。

3、 Qt提供了5个全局信息输出函数,对应不同级别:

(1)、qDebug(): 调试信息。

(2)、qInfo(): 普通信息。

(3)、qWarning(): 警告信息。

(4)、qCritical(): 关键错误和系统错误信息。

(5)、qFatal(): 致命错误信息,如果运行qFatal(),应用程序会立即终止。

**4、**Qt调试信息重定向本地文件,只要实现消息处理函数,然后通过qInstallMessageHandler重定义,就可以将调试信息输出到指定路径的文件中了。

**5、**示例: (main.cpp, 其它文件略)

cpp 复制代码
//main.cpp

#include "widget.h"
#include <QApplication>
#include <QMutex>
#include <QDateTime>
#include <QFile>
#include <QDebug>


static QMutex mutex;
void MyLog(QtMsgType type, const QMessageLogContext & context, const QString & message)
{
    mutex.lock();

    QString strType;
    switch (type) {
    case QtDebugMsg:
        strType = "Debug";
        break;
    case QtInfoMsg:
        strType = "Info";
        break;
    case QtWarningMsg:
        strType = "Warning";
        break;
    case QtCriticalMsg:
        strType = "Critical";
        break;
    default:
        break;
    }

    QString strFile = context.file;
    QString strLine = QString::number(context.line);
    QString strFunc = context.function;
    QString strTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    QString strLog = QString("[%1][%2][%3][%4][%5]%6.").arg(strType).arg(strFile).arg(strLine).arg(strFunc).arg(strTime).arg(message);

    QString strFileName = QString("App_%1.log").arg(QDateTime::currentDateTime().toString("yyyyMMdd"));
    QFile file(strFileName);
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream stream(&file);
    stream << strLog << "\r\n";
    file.flush();
    file.close();

    mutex.unlock();
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    qInstallMessageHandler(MyLog);

    qDebug() << "This is Debug";
    qInfo() << "This is Info";
    qWarning() << "This is Warning";
    qCritical() << "This is Critical";

    Widget w;
    w.show();

    return a.exec();
}

//运行结果,在exe同级目录下有一个如App_20250425.log文件,里面包含各种输出信息。

Debug\]\[..\\..\\main.cpp\]\[57\]\[int __cdecl main(int,char \*\[\])\]\[2025-04-25 16:55:49\]This is Debug. \[Info\]\[..\\..\\main.cpp\]\[58\]\[int __cdecl main(int,char \*\[\])\]\[2025-04-25 16:55:49\]This is Info. \[Warning\]\[..\\..\\main.cpp\]\[59\]\[int __cdecl main(int,char \*\[\])\]\[2025-04-25 16:55:49\]This is Warning. \[Critical\]\[..\\..\\main.cpp\]\[60\]\[int __cdecl main(int,char \*\[\])\]\[2025-04-25 16:55:49\]This is Critical. \[Debug\]\[..\\..\\main.cpp\]\[57\]\[int __cdecl main(int,char \*\[\])\]\[2025-04-25 16:56:51\]This is Debug. \[Info\]\[..\\..\\main.cpp\]\[58\]\[int __cdecl main(int,char \*\[\])\]\[2025-04-25 16:56:51\]This is Info. \[Warning\]\[..\\..\\main.cpp\]\[59\]\[int __cdecl main(int,char \*\[\])\]\[2025-04-25 16:56:51\]This is Warning. \[Critical\]\[..\\..\\main.cpp\]\[60\]\[int __cdecl main(int,char \*\[\])\]\[2025-04-25 16:56:51\]This is Critical.

相关推荐
渣渣盟15 分钟前
JavaScript核心概念全解析
开发语言·javascript·es6
java叶新东老师1 小时前
goland编写go语言导入自定义包出现: package xxx is not in GOROOT (/xxx/xxx) 的解决方案
开发语言·后端·golang
檀越剑指大厂1 小时前
【Python系列】Flask 应用中的主动垃圾回收
开发语言·python·flask
檀越剑指大厂2 小时前
【Python系列】使用 memory_profiler 诊断 Flask 应用内存问题
开发语言·python·flask
笠码2 小时前
JVM Java虚拟机
java·开发语言·jvm·垃圾回收
橙小花2 小时前
C语言:指针、变量指针与指针变量、数组指针与指针数组
c语言·开发语言
Cyanto2 小时前
MyBatis-Plus高效开发实战
java·开发语言·数据库
艾莉丝努力练剑2 小时前
【LeetCode&数据结构】二叉树的应用(二)——二叉树的前序遍历问题、二叉树的中序遍历问题、二叉树的后序遍历问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
wjs20243 小时前
XML 语法详解
开发语言
双叶8363 小时前
(Python)文件储存的认识,文件路径(文件储存基础教程)(Windows系统文件路径)(基础教程)
开发语言·windows·python