Qt generates log files

Summary:

This article implements the log function in the Qt program, readers can further create and expand on this basis

Introduction:

System log generally refers to the log.txt file that stores important operating information of the system. It has two main functions: 1. Recording important operating information of the system 2. When the system suddenly crashes, you can track and locate program errors based on the log.
Qt provides qInstallMessageHandler (Qt5) or qInstallMsgHandler (Qt4), which can redirect QDebug, QWarning, QError, etc.

qDebug Debug information
qWarning Warning message
qCritical Serious error
qFatal Fatal error

Implementation process

#include <QApplication>
#include <QtDebug>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QDateTime>
#include <QMutex>
#include <QString>
#include <qapplication.h>
#include <QMessageBox>

void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &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;
    default:break;
     }

     // 设置输出信息格式
     QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
     QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ");
     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);
     QString message = QString("%1 %2").arg(current_date).arg(msg);
     // 输出信息至文件中(读写、追加形式)
     QFile file("log.txt");
     file.open(QIODevice::WriteOnly | QIODevice::Append);
     QTextStream text_stream(&file);
     text_stream << message << "\r\n";
     file.flush();
     file.close();
     // 解锁
     mutex.unlock();
 }

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //注册日志函数
    qInstallMessageHandler(outputMessage); 
    //记录日志信息
    qDebug("This is a debug message");
    qWarning("This is a warming message");
    qCritical("This is a critical message");
    //qFatal("This is a fatal message");
    return a.exec();
}

operation result:

The log.txt file can be found under the build-** _Qt_5_11_2_MSVC201 _ ** bit-**** file
Insert picture description here

expand:

In the above introduction, we defined an outputMessage before the main function. This method is very unsightly in a complex program. A better way is to package it into the log library. In this case, only #include " log.h "will do. In addition, many blog posts encapsulate it in the log category, which is also worth learning.
https://blog.csdn.net/bootleader/article/details/70213281?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

Published 22 original articles · Likes2 · Visits 1157

Guess you like

Origin blog.csdn.net/qinqinxiansheng/article/details/105037044