zoukankan      html  css  js  c++  java
  • Qt::日志处理

     // 自定义消息处理
     qInstallMessageHandler(comfunc::myMessageOutput);
    void comfunc::myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
        //修改日志消息内容
        QString logMsg = msg;
        if(logMsg.simplified().length() == 0) //过滤掉空消息
        {
            return;
        }
        if(logMsg[0] =='\"' && logMsg[logMsg.size()-1] == '\"')
        {
            logMsg = logMsg.mid(1,logMsg.length()-2);//去除消息中的冒号
        }
    
        QString logType;
        switch(type)
        {
            case QtDebugMsg:
                 logType = QString("DEBUG");
                 break;
    
            case QtInfoMsg:
                 logType = QString("INFO");
                 break;
    
            case QtWarningMsg:
                 logType = QString("WARN");
                 break;
    
            case QtCriticalMsg:
                 logType = QString("ERROR");
                 break;
    
            case QtFatalMsg:
                 logType = QString("Fatal");
                 break;
    
            default:
                 logType = QString("UNKNOWN");
                 break;
        }
    
        QString current_Date_Time = QDateTime::currentDateTime().toString("MM/dd hh:mm:ss.zzz");
    
        QString message = QString("%1|%2|%3|%4|%5\n").arg(current_Date_Time).arg(logType).arg(context.function).arg(context.line).arg(logMsg);
    
        //生成日志文件
        static QMutex mutex;    //添加互斥锁,便于在多线程情况的使用
        mutex.lock();             //加锁
        QDir::setCurrent(QCoreApplication::applicationDirPath());//设置当前目录
        static QString LogName = "./mylog.log";
        QFile file(LogName);
        if(file.size() >= 100*1024*1024)//如果日志文件超过100M,则另写一个
        {
            file.rename(LogName, LogName+QDateTime::currentDateTime().toString("_yyyyMMddhhmmss.log"));
            file.setFileName(LogName);
        }
    
        if(file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
        {
            QTextStream text_stream(&file);
            text_stream << message;
            file.flush();//将任何缓存数据刷新到文件中
            file.close();
        }
    
        mutex.unlock();    //解锁
    }
  • 相关阅读:
    通过在vs中”程序包管理器控制台“的输入命令来实现下载和安装所需版本的Mysql.data和Mysql.Data.Entity.EF6
    ireport使用问题
    mysql数据库迁移到达梦数据库
    Idea配置热部署
    阿里云双12年终钜惠
    移动端弹性布局方案lib-flexible实践
    阿里云服务器2折起
    javascript百度地图使用(根据地名定位、根据经纬度定位)
    js拖拽上传图片
    javascript xml转json
  • 原文地址:https://www.cnblogs.com/osbreak/p/14370312.html
Copyright © 2011-2022 走看看