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

相关推荐
雪的季节1 小时前
qt信号槽跨线程使用时候的坑
java·开发语言·qt
yy_xzz1 小时前
【Qt 开发笔记】能扛住断电、多线程的通用配置类(移植直接用)
笔记·qt
丁劲犇3 小时前
改造传统Qt6Widgets程序为多会话MCPServer生产力工具-技巧与实现
qt·ai·agent·并发·mcp·mcpserver·widgets
sycmancia4 小时前
Qt——对话框及其类型
开发语言·qt
sycmancia5 小时前
Qt——登录对话框
开发语言·qt
妙为15 小时前
银河麒麟V4下编译Qt5.12.12源码
c++·qt·国产化·osg3.6.5·osgearth3.2·银河麒麟v4
史迪仔011218 小时前
[QML] QML IMage图像处理
开发语言·前端·javascript·c++·qt
小樱花的樱花1 天前
打造高效记事本:UI设计到功能实现
开发语言·c++·qt·ui
丁劲犇1 天前
QMetaObject的invokeMethod异步阻塞调用在MCPServer开发中的巧妙应用
qt·ai·agent·异步·阻塞·mcp·mcp server
m0_497214151 天前
Qt事件系统
开发语言·qt