QT 日志打印

效果

头文件

cpp 复制代码
//Log.h
#ifndef LOG_H
#define LOG_H

#include <QCoreApplication>
#include <QString>

#define LOG_FILE    ("log.txt")//日志文件名

void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);

#endif // LOG_H

源文件

cpp 复制代码
//Log.cpp
#include "Log.h"
#include <QMutex>
#include <QString>
#include <QFile>
#include <QDateTime>
#include <QTextStream>

void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QString text;
    QString time;
    QFile file(LOG_FILE);
    static QMutex mutex;

    Q_UNUSED(context)

    mutex.lock();//互斥锁

    file.open(QIODevice::WriteOnly | QIODevice::Append);//只写 追加
    QTextStream textStream(&file);

//    <QtGlobal>::QtDebugMsg       0  A message generated by the qDebug() function.
//    <QtGlobal>::QtInfoMsg        4  A message generated by the qInfo() function.
//    <QtGlobal>::QtWarningMsg     1  A message generated by the qWarning() function.
//    <QtGlobal>::QtCriticalMsg    2  A message generated by the qCritical() function.
//    <QtGlobal>::QtFatalMsg       3  A message generated by the qFatal() function.
//    <QtGlobal>::QtSystemMsg   `QtCriticalMsg

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

	//拼接日志格式
    time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    text = QString("%1%2 %3").arg(text).arg(time).arg(msg);
    //写入文件
    textStream << text << "\r\n";
    
    file.flush();
    file.close();

    mutex.unlock();
}

主函数

cpp 复制代码
#include "Login.h"
#include "Log.h"

#include <QApplication>
#include <QTime>

//#define APP_DEBUG

int main(int argc, char *argv[])
{
	//日志输出到 控制台/文件
	#ifdef APP_DEBUG
	    qInstallMessageHandler(nullptr);
	#else
	    qInstallMessageHandler(messageHandler);
	#endif
	
	//日志打印
	qInfo()<<"系统开始执行时间"<<QDateTime::currentDateTime()<<__FILE__<<__LINE__;
	    
    QApplication a(argc, argv);
    Login w;
    w.show();
    
    return a.exec();
}

其他补充

QTextStream 用法

QTextStream是一个用于读写文本数据的类。它提供了方便的方法来读取和写入不同数据类型的文本。
QTextStream可以与不同的设备(例如文件、套接字、字符串等)一起使用,以便从设备中读取或向设备中写入文本数据。

cpp 复制代码
//--------------------案列-------------------
#include <QFile>
#include <QTextStream>

int main() {
    // 打开一个文件供读取
    QFile file("myfile.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        // 文件打开失败
        return 1;
    }

    // 创建QTextStream对象,将其与文件关联
    QTextStream in(&file);

    // 从文件中读取文本数据
    QString line = in.readLine();
    while (!line.isNull()) {
        // 处理读取到的每行文本数据
        // ...

        // 继续读取下一行
        line = in.readLine();
    }

    // 关闭文件
    file.close();

    // 打开一个文件供写入
    QFile outFile("output.txt");
    if (!outFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
        // 文件打开失败
        return 1;
    }

    // 创建QTextStream对象,将其与文件关联
    QTextStream out(&outFile);

    // 向文件中写入文本数据
    out << "Hello, world!" << endl;

    // 关闭文件
    outFile.close();

    return 0;
}
Q_UNUSED(context)

Q_UNUSED(context) 是一个宏,用于标识未使用的变量。它的作用是告诉编译器,该变量在代码中未被使用,并防止产生未使用变量的编译警告。在这里,context 是一个占位符变量,通常用于函数签名中但在函数体内未被使用的情况。通过使用Q_UNUSED宏,我们可以确保在编译过程中不会产生针对未使用变量的警告信息。

详细教程可转

Qt应用程序输出日志的方法

相关推荐
mahuifa7 小时前
(7)python开发经验
python·qt·pyside6·开发经验
galaxy_strive8 小时前
qtc++ qdebug日志生成
开发语言·c++·qt
TNTLWT8 小时前
Qt功能区:简介与安装
开发语言·qt
凯雀安全16 小时前
printspoofer的RPC调用接口的简单代码
qt·网络协议·rpc
六bring个六17 小时前
文件系统交互实现
开发语言·c++·qt·交互
__BMGT()18 小时前
C++ QT图片查看器
前端·c++·qt
chilavert3181 天前
从RPA项目说说RPC和MQ的使用。
开发语言·qt·rpc·rabbitmq
Smile丶凉轩1 天前
Qt 界面优化(绘图)
开发语言·数据库·c++·qt
charlie1145141911 天前
基于Qt6 + MuPDF在 Arm IMX6ULL运行的PDF浏览器——MuPDF Adapter文档
arm开发·qt·学习·pdf·教程·设计·qt6