zoukankan      html  css  js  c++  java
  • QT5生成log日志

    函数说明:生成log日志。

    调用方法:qInstallMessageHandler(outputMessage);

    使用qDebug()<<"q"<<endl;

    就可以在log.txt文件中查看所打印的信息。

    #ifndef _LOGRECORD_H
    #define _LOGRECORD_H
    #pragma execution_character_set("utf-8")
    #include <QObject>
    #include <QMutex>
    #include <QDateTime>
    #include <QFile>
    #include <QTextStream>
    void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg);

    #endif // !_LOGRECORD_H

    #include "LogRecord.h"
    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 ");
    }

    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 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 text_stream(&file);
    text_stream << message << " ";
    file.flush();
    file.close();
    mutex.unlock();
    }

    后知后觉、越学越菜
  • 相关阅读:
    【Spring Framework】10、代理模式
    【Spring Framework】8、使用注解开发
    Codeforces 516E
    AtCoder Grand Contest 055 题解
    Codeforces 1606F
    贪心/构造/DP 杂题选做
    整数拆分最大乘积
    CSP-S2021 被碾压记
    洛谷 P2791
    LCT 小记
  • 原文地址:https://www.cnblogs.com/chenhuanting/p/10824998.html
Copyright © 2011-2022 走看看