溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Qt高級——Qt日志信息處理

發(fā)布時間:2020-05-21 05:06:36 來源:網(wǎng)絡(luò) 閱讀:18563 作者:天山老妖S 欄目:編程語言

Qt高級——Qt日志信息處理

一、Qt日志功能簡介

Qt有Debug、Warning、Critical、Fatal四種級別的調(diào)試信息。
qDebug:調(diào)試信息
qWarning:警告信息
qCritical:嚴(yán)重錯誤
qFatal:致命錯誤
Qt4提供了qInstallMsgHandler(Qt5:qInstallMessageHandler)對qDebug、qWarning、qCritical、qFatal等函數(shù)輸出信息的重定向處理。
qInstallMsgHandler是一個回調(diào)函數(shù),由qDebug、qWarnng、qCritical、qFatal函數(shù)進行觸發(fā),qDebug、qWarnng、qCritical、qFatal函數(shù)處理的消息文本會被qInstallMsgHandler所指向的回調(diào)函數(shù)截獲,允許用戶自己來處理輸出的消息文本。

二、Qt日志輸出組件

1、Qt日志輸出組件定義

定制一個日志信息輸出組件。
LogWidget.h文件:

#ifndef LOGWIDGET_H
#define LOGWIDGET_H

#include <QWidget>
#include <QTextEdit>
#include <QHBoxLayout>
#include <QApplication>
#include <QMutex>
#include <QDateTime>

/**
 * @brief 日志組件
 */
class LogWidget : public QWidget
{
    Q_OBJECT
public:
    /**
     * @brief 獲取單例
     * @return
     */
    static LogWidget* getInstance();

    /**
     * @brief 日志信息輸出函數(shù)
     * @param type 參數(shù),日志信息的級別
     * @param msg 參數(shù),日志信息的內(nèi)容
     */
    void outputMessage(QtMsgType type, const char *msg);
protected:
    explicit LogWidget(QWidget *parent = NULL);
    /**
     * @brief 打印日志信息
     * @param msg 輸入?yún)?shù),日志信息
     */
    void printMessage(const QString& msg);
private:
    static LogWidget* m_instance;//單例
    QTextEdit* m_textEdit;//日志輸出多行文本框
};

#endif // LOGWIDGET_H

LogWidget.cpp文件:

#include "LogWidget.h"

LogWidget* LogWidget::m_instance = NULL;

LogWidget *LogWidget::getInstance()
{
    if(m_instance == NULL)
    {
        m_instance = new LogWidget();
    }
    return m_instance;
}

void LogWidget::printMessage(const QString &msg)
{
    m_textEdit->append(msg);
}

LogWidget::LogWidget(QWidget *parent) : QWidget(parent)
{
    m_textEdit = new QTextEdit(this);
    m_textEdit->setReadOnly(true);
    QHBoxLayout* layout = new QHBoxLayout;
    layout->addWidget(m_textEdit);
    setLayout(layout);
    resize(600, 200);
}

void LogWidget::outputMessage(QtMsgType type, const char *msg)
{
    static QMutex mutex;
    mutex.lock();

    QString text;
    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;
    }
    QString message = QString("[%1] %2 %3").arg(
                          QDateTime::currentDateTime().toString(
                              "yyyy-MM-dd hh:mm:ss ddd")).arg(text).arg(msg);
    printMessage(message);
    mutex.unlock();
}

2、Qt日志輸出組件的使用

#include <QApplication>
#include <QDebug>
#include "LogWidget.h"

/**
 * @brief 日志輸出回調(diào)函數(shù)
 * @param type 參數(shù),日志消息的級別
 * @param msg 參數(shù),日志消息
 */
void outputMessage(QtMsgType type, const char *msg)
{
    LogWidget::getInstance()->outputMessage(type, msg);
}

int main(int argc, char *argv[])
{
    //注冊日志消息回調(diào)函數(shù)
    qInstallMsgHandler(outputMessage);
    QApplication a(argc, argv);
    LogWidget::getInstance()->show();

    // 打印信息
    qDebug("This is a debug message.");
    qWarning("This is a warning message.");
    qCritical("This is a critical message.");
    //linux調(diào)用qFatal會導(dǎo)致coredump
    //qFatal("This is a fatal message");

    return a.exec();
}

3、Qt日志組件示例

Qt高級——Qt日志信息處理

三、Qt日志文件輸出

1、Qt日志文件輸出

#include <QApplication>
#include <QDebug>
#include <QTextStream>
#include <QDateTime>
#include <QFile>
#include <QString>
#include <QMutex>

void outputMessage(QtMsgType type, const char* msg)
{
    static QMutex mutex;
    mutex.lock();

    QString text;
    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:");
    }

    QString context_info = QString("File:(%1) Line:(%2)").arg(__FILE__).arg(__LINE__);
    QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString current_date = QString("(%1)").arg(current_date_time);
    QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);

    QFile file("log.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream out(&file);
    out << message << "\n";
    file.flush();
    file.close();
    mutex.unlock();
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    //注冊MessageHandler
    qInstallMsgHandler(outputMessage);

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

    return app.exec();
}

2、Qt日志文件

Qt高級——Qt日志信息處理

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI