• 转 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();
    }
  • 相关阅读:
    数据结构和算法大纲
    内存碎片产生原因及处理技术
    相关资源
    busybox hexdump 命令使用
    http协议中content-length 以及chunked编码分析
    libtool工具的使用
    音视频学习相关资源
    winpcap
    ipkg包管理
    system返回值校验
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/3363018.html
Copyright © 2020-2023  润新知