zoukankan      html  css  js  c++  java
  • qInstallMsgHandler实现日志输出

    #include <QtDebug>
    #include <QFile>
    #include <QTextStream>
    
    #define _TIME_ qPrintable (QTime::currentTime ().toString ("hh:mm:ss:zzz"))
    
    void Log(QtMsgType type, const char* msg)
    {
        QString qstrText;
        switch (type)
        {
        case QtDebugMsg:
            qstrText = QString("%1: %2").arg(_TIME_, msg);
            break;
        case QtWarningMsg:
            qstrText = QString("%1: %2").arg(_TIME_, msg);
            break;
        case QtCriticalMsg:
            qstrText = QString("%1: %2").arg(_TIME_, msg);
            break;
        case QtFatalMsg:
            qstrText = QString("%1: %2").arg(_TIME_, msg);
            exit(0);
        }
        QFile out("log.txt");
        out.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream ts(&out);
        ts<<qstrText<<endl;
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        qInstallMsgHandler(Log);
    
        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 a.exec();
    }
  • 相关阅读:
    flutter 和 NTFS
    APIO2020 游记
    CF1336F Journey
    ZJOI2020 游记
    CF568E Longest Increasing Subsequence
    CSP2020 游记
    洛谷 P6217 简单数论题
    CF587F Duff is Mad
    CF526G Spiders Evil Plan
    WC2021 游记
  • 原文地址:https://www.cnblogs.com/venow/p/2716756.html
Copyright © 2011-2022 走看看