Qt调试详细日志文件输出

qInstallMessageHandle安装消息,注册回调函数,对qDebug()、qWarning()、qCritial()、qInfo()、qFatal() 等函数输出信息重定向处理。

注明:以下方法仅适用于 Qt5 及以上版本

目的:生成log.txt日志文件,记录详细日志信息(包括等级、所在文件、所在行号、描述信息、产生时间等),以便于快速跟踪、定位。那么在qt createor的应用程序输出窗口就不会有调试信息显示。

QtMessageHandler qInstallMessageHandler(QtMessageHandler handler)

qInstallMessageHandler安装写在main()方法中,这样全局所有类的qDebug都可以将信息直接打印到日志文件中即安装了消息处理)。如果想恢复消息处理程序,调用qInstallMessageHandler(0)。

QtMessageHandler 回调函数处理日志信息,包括等级、所在文件、所在行号、描述信息、产生时间并保存到本地文件。

其中等级:

qDebug: 调试信息

qWarning:警告信息

qCritical: 严重错误

qFatal: 致命错误

#ifndef LOGGINGOUTPUT_H
#define LOGGINGOUTPUT_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 


const int LOG_OPERATE_OK = 100; //操作成功
const int LOG_OPENFILE_FAILED = 101; //打开文件失败


class QLogOutput : public QObject
{
    Q_OBJECT
public:
    explicit QLogOutput(QObject *parent = nullptr);

    //安装信息处理函数
    static void install(QString strLogFile);

    //卸载信息处理函数
    static void uninstall();

private:
    //日志信息处理函数
    static void outPutMsg(QtMsgType msgType, const QMessageLogContext &context, const QString &strText);

     //保存日志
    static int SaveLog(const QString &strMsg);

    //保存日志路径
    static QString m_strLogFile;

};


#endif // LOGGINGOUTPUT_H
#include "loggingoutput.h"

QString  QLogOutput::m_strLogFile = "";

QLogOutput::QLogOutput(QObject *parent) : QObject(parent)
{

}

void QLogOutput::outPutMsg(QtMsgType msgType, const QMessageLogContext &context, const QString &strText)
{
    static QMutex mutex;
    mutex.lock();

    QByteArray localMsg = strText.toLocal8Bit();

    QString strMsg("");
    QString strMessage;
    // 设置输出信息格式
    QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    switch(msgType)
    {
    case QtDebugMsg:
    {
        strMsg = QString("Debug:");
         strMessage = QString("[%1] %5 %6\t--File:%2  Line:%3  Function:%4  ")
                .arg(strDateTime).arg(context.file).arg(context.line).arg(context.function).arg(strMsg).arg(strText);
    }
        break;
    case QtInfoMsg:
    {
         strMsg = QString("Info:");
         strMessage = QString("[%1] %2 %3\t").arg(strDateTime).arg(strMsg).arg(strText);
    }
        break;
    case QtWarningMsg:
    {
        strMsg = QString("Warning:");
        strMessage = QString("[%1] %5 %6\t--File:%2  Line:%3  Function:%4  ")
               .arg(strDateTime).arg(context.file).arg(context.line).arg(context.function).arg(strMsg).arg(strText);
    }
        break;
    case QtCriticalMsg:
    {
        strMsg = QString("Critical:");
        strMessage = QString("[%1] %5 %6\t--File:%2  Line:%3  Function:%4  ")
               .arg(strDateTime).arg(context.file).arg(context.line).arg(context.function).arg(strMsg).arg(strText);
    }
        break;
    case QtFatalMsg:
    {
        strMsg = QString("Fatal:");
        strMessage = QString("[%1] %5 %6\t--File:%2  Line:%3  Function:%4  ")
               .arg(strDateTime).arg(context.file).arg(context.line).arg(context.function).arg(strMsg).arg(strText);
    }
        break;
    }

    SaveLog(strMessage);

    // 解锁
    mutex.unlock();

}

void QLogOutput::install(QString strLogFile)
{
    qInstallMessageHandler(outPutMsg);
    QString log_dir = strLogFile+"/log";
    QDir dir(log_dir);
    if(!dir.exists())
    {
        //创建在可执行程序所在目录创建log文件
        dir.mkdir(log_dir);
    }
    m_strLogFile = log_dir;
}

void QLogOutput::uninstall()
{
    qInstallMessageHandler(0);
}

int QLogOutput::SaveLog(const QString& strMsg)
{
    QString log_fileName;
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString date = current_date_time.toString("yyyy-MM-dd");
    log_fileName = m_strLogFile +"/"+"log_"+date+".txt";
    // 输出信息至文件中(读写、追加形式)
    QFile file(log_fileName);
    bool bOpen = file.open(QIODevice::ReadWrite | QIODevice::Append);
    if(!bOpen)
        return  LOG_OPENFILE_FAILED;
    QTextStream stream(&file);
    stream << strMsg << "\r\n";
    file.flush();
    file.close();
    return  LOG_OPERATE_OK;
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString strLogFile = QCoreApplication::applicationDirPath();
    QLogOutput::install(strLogFile);

    qDebug("This is a 调试 message.");
    qInfo("This is a info message.");

    Dialog w;
    w.show();
    return a.exec();
}

在vs2017 msvc + qt 的环境下,防止中文乱码,在pro文件配置

msvc{
QMAKE_CXXFLAGS += -utf-8
}

你可能感兴趣的:(qt,调试,qt,开发语言)