zoukankan      html  css  js  c++  java
  • 重构qDebug()<<,使log输出到文件

    重构qDebug()<<,使log输出到文件

    #include <QProcessEnvironment>
    #include <QDateTime>
    #include <QFile>
    #include <QIODevice>
    
    class HSDbg
    {
    public:
    
        HSDbg& operator<<(const QString& str)
        {
            QTextStream txtOutput(&qtLogfile);
            if(qtLogfile.open(QIODevice::Append | QIODevice::Text))
            {
                qDebug() << str;
                txtOutput << str << "
    ";
                qtLogfile.close();
            }
            return *this;
        }
        HSDbg& operator<<(const QByteArray& str)
        {
            QTextStream txtOutput(&qtLogfile);
            if(qtLogfile.open(QIODevice::Append | QIODevice::Text))
            {
                qDebug() << str;
                txtOutput << str << "
    ";
                qtLogfile.close();
            }
            return *this;
        }
        HSDbg& operator<<(const char* str)
        {
            QTextStream txtOutput(&qtLogfile);
            if(qtLogfile.open(QIODevice::Append | QIODevice::Text))
            {
                qDebug() << QString(str);
                txtOutput << str << "
    ";
                qtLogfile.close();
            }
            return *this;
        }
        HSDbg& operator<<(const int& i)
        {
            QTextStream txtOutput(&qtLogfile);
            if(qtLogfile.open(QIODevice::Append | QIODevice::Text))
            {
                qDebug() << i;
                txtOutput << i << "
    ";
                qtLogfile.close();
            }
            return *this;
        }
        HSDbg(QString fileName)
        {
            qtLogfile.setFileName(fileName);
        }
    
    private:
        QFile qtLogfile;
    
    };
    
    #define LOG_FILE_PATH   QProcessEnvironment::systemEnvironment().value("APPDATA") + "\test\logs\"
    #define LOG_FILE_NAME   "Qt_" + QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss") + ".txt"
    
    static HSDbg sHSDbg(LOG_FILE_PATH + LOG_FILE_NAME);
    //用此QDBG输出到log文件中
    #define QDBG sHSDbg<<"==="+QString(__FILE__)+" "+QString(__FUNCTION__)+"():"+QString::number(__LINE__)
    //用此QDBG输出到consloe上
    //#define QDBG qDebug()<<__FILE__<<__FUNCTION__<<"():"<<__LINE__

    http://blog.csdn.net/liukang325/article/details/72466199

  • 相关阅读:
    wxpython 简单例子:显示文本框的窗口显示鼠标位置
    wxpython学习:创建最小的空的wxPython程序
    wxPython学习笔记
    5G PDCCH 协议
    FPGA学习
    CCS 5.5下载地址http://www.dianyuan.com/bbs/1492792.html
    朴素贝叶斯
    决策树最后的存储 检测
    决策树 绘图
    决策树 书上的例题
  • 原文地址:https://www.cnblogs.com/findumars/p/7701651.html
Copyright © 2011-2022 走看看