qt调试日志文件生成

系列文章目录

第一章 qt日志文件生成功能


文章目录


前言

qt有固定的调试日志接口,可以通过终端去打印,但是仅适用在本地去调试,例如想长期放到测试台去检测, 可以采用生成日志文件的方式去监测.

关键字:日志文件


一、qt日志文件生成功能

枚举类型 详情
qDebug 调试信息
qInfo 正常信息
qWarning 警告信息
qCritical 严重错误
qFatal 致命错误

二、使用步骤

qt目录结构

1.代码示例

代码如下(示例):

custommessagehandler.h

cpp 复制代码
#ifndef CUSTOMMESSAGEHANDLER_H
#define CUSTOMMESSAGEHANDLER_H

#include <QtCore>
class CustomMessageHandler
{
public:
    CustomMessageHandler();
    static void customMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);
};

#endif // CUSTOMMESSAGEHANDLER_H

custommessagehandler.cpp

cpp 复制代码
#include <QFile>
#include <QTextStream>
#include <QDateTime>

#include "custommessagehandler.h"

CustomMessageHandler::CustomMessageHandler()
{

}

void CustomMessageHandler::customMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    // 打开文件,确保文件存在并具有写入权限
    //QFile file("D:/work/lmp/qtproject/untitled/logfile.txt");
    QFile file("logfile.txt");
    if (!file.open(QIODevice::Append | QIODevice::Text)) {
        return;
    }

    // 根据消息类型添加前缀
    QString text;
    switch (type) {
    case QtDebugMsg:
        text = QString("Debug: %1").arg(msg);
        break;
    case QtInfoMsg:
        text = QString("Info: %1").arg(msg);
        break;
    case QtWarningMsg:
        text = QString("Warning: %1").arg(msg);
        break;
    case QtCriticalMsg:
        text = QString("Critical: %1").arg(msg);
        break;
    case QtFatalMsg:
        text = QString("Fatal: %1").arg(msg);
        // 注意:QtFatalMsg 会导致应用程序终止,因此这里可能无法写入完整的消息
        break;
    default:
        text = msg;
        break;
    }

    // 写入文件并关闭
    QTextStream out(&file);
    out << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz ") << text << endl;
    file.close();
}

main.cpp

cpp 复制代码
#include "mainwindow.h"
#include <QApplication>
#include "custommessagehandler.h"
#include "QDebug"

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

	// add code start
    // 安装自定义消息处理器
    qInstallMessageHandler(CustomMessageHandler::customMessageOutput);
    qInfo() << "Application started";//输出一般性信息,用于记录应用程序运行过程中的常规信息,帮助开发者了解程序执行流程。
    qDebug() << "Variable value ";//主要用于调试目的,输出详细的调试信息。
    qWarning() << "Invalid input detected, using default value";//输出警告信息,表示程序运行过程中出现了潜在的问题,但并没有阻止程序继续执行
    qCritical() << "Critical failure in network connection";//输出严重错误信息,强调程序发生了严重的、可能会导致程序异常或严重影响程序功能的情况,但程序仍然能够运行并可能有机会尝试恢复。
    // qFatal("Unrecoverable system error occurred");//输出致命错误信息,表示发生了非常严重、无法恢复的错误,通常这类错误会导致程序立即终止
    // ... 你的应用程序代码 ...
	//add code end

    MainWindow w;
    w.show();

    return a.exec();
}

2.运行截图


相关推荐
还债大湿兄1 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian4 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上4 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang4 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc4 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇5 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀5 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
liulilittle5 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程