Qt--日志文件(log)

一、Qt 日志文件介绍

1、平时code时,一般时debug模式,打印对应的信息就便于调试,但是工程最终生成release版本,需要将对应的log信息打印到文件中。

2、Qt中模块core中有对应的类QMessageLogger,该类能方便的将调试信息打印到文件中。

 

二、函数类介绍

1、QMessageLogger,介绍如下:

The QMessageLogger class generates log messages.
QMessageLogger is used to generate messages for the Qt logging framework. Usually one uses it through qDebug(), qInfo(), qWarning(), qCritical, or qFatal() functions, which are actually macros: For example qDebug() expands to QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug() for debug builds, and QMessageLogger(0, 0, 0).debug() for release builds.

 

三、代码实现

1、log.h

//
#ifndef LOG_H
#define LOG_H

#include <QFile>
#include <QMessageLogger>
#include <qlogging.h>
#include <QFile>
#include <QTextStream>
#include <QDateTime>

extern QMessageLogger *gMLog;
void logSysInit(QString filePath);
void logSysInit();

//打印日志到文件中
//qDebug("This is a debug message");
//qWarning("This is a warning message");
//qCritical("This is a critical message");
//qFatal("This is a fatal message");

#endif // LOG_H
//

2、log.cpp

//
#include "log.h"

QFile *gFileLog = NULL;
QMessageLogger *gMLog = NULL;

char *msgHead[] = {
    "Debug   ",
    "Warning ",
    "Critical",
    "Fatal   ",
    "Info    "
};

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz ddd");

    if(gFileLog){
        QTextStream tWrite(gFileLog);

        QString msgText="%1 | %6 | %2:%3, %4 | %5\n";
        msgText = msgText.arg(msgHead[type]).arg(context.file).arg(context.line).arg(context.function).arg(localMsg.constData()).arg(current_date_time);
        //gFileLog->write(msgText.toLocal8Bit(), msgText.length());
        tWrite << msgText;
    }else{
        fprintf(stderr, "%s | %s | %s:%u, %s | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), context.file, context.line, context.function, localMsg.constData());
    }

}

void logSysInit(QString filePath)
{
    gFileLog = new QFile(filePath);
    if (!gFileLog->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
        return;
    }
//初始化自定义日志处理函数myMessageOutput
    qInstallMessageHandler(myMessageOutput);
//gMLog 这个类不要也可以,执行的时候只能看一下效果。
    gMLog = new QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO);
}

void logSysInit()
{
    qInstallMessageHandler(myMessageOutput);

    gMLog = new QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO);
}
//

3、main.cpp

//
#include "log.h"
#include <QTime>

int main()
{
    QTime t;
    t.start();
    logSysInit("log.txt");
    //打印日志到文件中
    qDebug("This is a debug message");
    qWarning("This is a warning message");
    qDebug("Test");
    qWarning("Test");
    //qCritical("This is a critical message");
    //qFatal("This is a fatal message");

    if(gMLog){
        gMLog->debug("aaaaa");
    }

    qDebug("Time elapsed: %d ms", t.elapsed());

    return 0;
}
//

 

 

 

 

 

展开阅读全文
©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读