Qt有Debug、Warning、Critical、Fatal四種級別的調試信息。
qDebug:調試信息
qWarning:警告信息
qCritical:嚴重錯誤
qFatal:致命錯誤
Qt4提供了qInstallMsgHandler(Qt5:qInstallMessageHandler)對qDebug、qWarning、qCritical、qFatal等函數輸出信息的重定向處理。
qInstallMsgHandler是一個回調函數,由qDebug、qWarnng、qCritical、qFatal函數進行觸發,qDebug、qWarnng、qCritical、qFatal函數處理的消息文本會被qInstallMsgHandler所指向的回調函數截獲,容許用戶本身來處理輸出的消息文本。linux
定製一個日誌信息輸出組件。
LogWidget.h文件:app
#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 日誌信息輸出函數 * @param type 參數,日誌信息的級別 * @param msg 參數,日誌信息的內容 */ void outputMessage(QtMsgType type, const char *msg); protected: explicit LogWidget(QWidget *parent = NULL); /** * @brief 打印日誌信息 * @param msg 輸入參數,日誌信息 */ void printMessage(const QString& msg); private: static LogWidget* m_instance;//單例 QTextEdit* m_textEdit;//日誌輸出多行文本框 }; #endif // LOGWIDGET_H
LogWidget.cpp文件:ide
#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(); }
#include <QApplication> #include <QDebug> #include "LogWidget.h" /** * @brief 日誌輸出回調函數 * @param type 參數,日誌消息的級別 * @param msg 參數,日誌消息 */ void outputMessage(QtMsgType type, const char *msg) { LogWidget::getInstance()->outputMessage(type, msg); } int main(int argc, char *argv[]) { //註冊日誌消息回調函數 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調用qFatal會致使coredump //qFatal("This is a fatal message"); return a.exec(); }
#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(); }