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应用程序输出日志的方法

相关推荐
cpp_learners3 小时前
QT 引入Quazip和Zlib源码工程到项目中,无需编译成库,跨平台,压缩进度
qt·zlib·加密压缩·quazip
数巨小码人7 小时前
QT SQL框架及QSqlDatabase类
jvm·sql·qt
程序员老舅9 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
enyp8010 小时前
Qt QStackedWidget 总结
开发语言·qt
luoyayun36110 小时前
Trae+Qt+MSVC环境配置
vscode·qt·环境配置·trae qt
水瓶丫头站住18 小时前
Qt中QDockWidget的使用方式
开发语言·qt
laimaxgg19 小时前
Qt常用控件之数字显示控件QLCDNumber
开发语言·c++·qt·qt5·qt6.3
牵牛老人20 小时前
Qt开发中出现中文乱码问题深度解析与解决方案
开发语言·qt
Zfox_20 小时前
【QT】信号与槽 & 窗口坐标
开发语言·c++·qt·qt5
进击ing小白1 天前
Qt程序退出相关资源释放问题
开发语言·qt