zoukankan      html  css  js  c++  java
  • Qt之分模块log

    说明

    对于一般的log,使用 qInstallMessageHandler 重定向到文件即可,甚至可以根据日志等级,分类存储。但是并不是适用所有情况,比如,程序运行时动态创建模块,而每个模块需要创建不用的日志记录。参考Qt的 QDebug 代码,重新编写日志模块。

    代码

    /********************************************************************************
    *
    *	Description	log模块,根据传入参数,创建log文件
    *	Author		sherlock
    *	Creation Time	2019/8/28
    *	Modify
    *
    *******************************************************************************/
    
    #ifndef LOGGER_H
    #define LOGGER_H
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <QFile>
    #include <QTextStream>
    #include <QDir>
    #include <QDateTime>
    #include <QMutex>
    
    class Logger
    {
    public:
        Logger(){}
        static void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
        {
            static QMutex mutex;
            mutex.lock();
            QString log_message;
            QByteArray localMsg = msg.toLocal8Bit();
            switch (type) {
            case QtDebugMsg:
                log_message = QString("Debug	:");
                fprintf(stderr, "Debug: %s (%s:%u, %s)
    ", localMsg.constData(), context.file, context.line, context.function);
                break;
            case QtInfoMsg:
                log_message = QString("Info	:");
                fprintf(stderr, "Info: %s (%s:%u, %s)
    ", localMsg.constData(), context.file, context.line, context.function);
                break;
            case QtWarningMsg:
                log_message = QString("Warning	:");
                fprintf(stderr, "Warning: %s (%s:%u, %s)
    ", localMsg.constData(), context.file, context.line, context.function);
                break;
            case QtCriticalMsg:
                log_message = QString("Critical	:");
                fprintf(stderr, "Critical: %s (%s:%u, %s)
    ", localMsg.constData(), context.file, context.line, context.function);
                break;
            case QtFatalMsg:
                fprintf(stderr, "Fatal: %s (%s:%u, %s)
    ", localMsg.constData(), context.file, context.line, context.function);
                abort();
            }
            fflush(stderr);//立即输出,msvc没有这句也会立即输出,但是mingw不会
    
    
    #ifndef QT_DEBUG
    //        if(QtDebugMsg == type)
    //        {
    //            mutex.unlock();
    //            return;
    //        }
            log_message = QString("%1 %2").arg(log_message).arg(msg);
    #else	//release下不会有文件以及函数信息
            log_message = QString("%1 %2	(%3:%4, %5)").arg(log_message).arg(msg).arg(context.file).arg(context.line).arg(context.function);
    #endif // else
    
            QDir dir;
            if(!dir.exists(logPath))
            {
                dir.mkpath(logPath);
            }
    
            QDateTime current_time = QDateTime::currentDateTime();
            QString log_file_name = QString("%1/%2_%3.log").arg(logPath)
                    .arg(context.category)
                    .arg(QDate::currentDate().toString("yyyyMMdd"));
    
            QFile outFile(log_file_name);
            if (outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
            {
                QTextStream write_log(&outFile);
                write_log << current_time.toString("yyyy-MM-dd hh:mm:ss") << "-";
                write_log << log_message << endl;
                outFile.close();
            }
    
            mutex.unlock();
        }
    
    private:
        static QString logPath;
    };
    
    //默认当前路径下的log文件夹下
    QString Logger::logPath = QString("%1/104_file_log").arg(QDir::currentPath());
    
    #endif
    

    为了保证旧代码不受影响,可以在项目根目录下新增QDebug文件,填入以下内容

    QDebug

    #include "qdebug.h"
    
    #define qModuleDebug(module) QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, module.toStdString().c_str()).debug()
    #define qModuleInfo(module) QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, module.toStdString().c_str()).info()
    #define qModuleWarning(module) QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, module.toStdString().c_str()).warning()
    #define qModuleCritical(module) QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, module.toStdString().c_str()).critical()
    

    使用时,只需要使用 qModuleDebug 宏替代原先的 qDebug宏即可,且原来的 qDebug 仍然可以使用

    QString moduleName = "test";
    qModuleDebug(moduleName)<<"This is test log";
    
  • 相关阅读:
    Navicat连接mysql提示1251解决方案
    js获取select下拉框选中的值
    Windows下安装Mysql数据库
    ASP.NET MVC API以及.Core API进行安全拦截和API请求频率控制
    myeclipse 10.7中文破解版 下载安装看着一篇就够了
    Runtime exception at 0x004000bc: invalid integer input (syscall 5)
    MARS(MIPS汇编程序和运行时模拟器)
    如何将本地的代码上传到github
    JavaWeb基础
    大学什么时候开学?这款小程序告诉你!
  • 原文地址:https://www.cnblogs.com/sherlock-lin/p/12862200.html
Copyright © 2011-2022 走看看