Qt日志库Log4Qt的使用,支持文件名/行号/函数名的log打印输出

下载地址

https://github.com/MEONMedical/Log4Qt

 

工程源码v1.5.1版本有几处需要修改

1、用户自己的.pro文件新增以下内容

#################日志库的引入########################
##https://github.com/MEONMedical/Log4Qt
include(Log4Qt-1.5.1/src/log4qt/log4qt.pri)
include(Log4Qt-1.5.1/build.pri)
include(Log4Qt-1.5.1/g++.pri)
QT          += concurrent
DEFINES     += LOG4QT_LIBRARY
DEFINES     += LOG4QT_EXPORT
INCLUDEPATH += Log4Qt-1.5.1/src/
INCLUDEPATH += Log4Qt-1.5.1/src/log4qt
DEPENDPATH  += Log4Qt-1.5.1/src/log4qt
###################################################

2、log4qt源码dailyfileappender.cpp需要添加头文件:

#include //firecat add

 

3、Log4Qt-1.5.1\src\log4qt\ttcclayout.h:118, enum DateFormat has errors

已定义的变量ABSOLUTE和RELATIVE,会和Windows的头文件定义冲突。

C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\wingdi.h
/ * Coordinate Modes * /
#define ABSOLUTE 1
#define RELATIVE 2

请把enum DateFormat的变量ABSOLUTE和RELATIVE重命名。

 

属性相关的源文件查阅

Log4Qt-1.5.1\src\log4qt\patternlayout.h

 * \brief The class PatternLayout outputs a logging event based on a
 *        pattern string.
        \li c{section_count} : logger name with optional parameter section_count. Section count from end of logger name, sections delimiter is "::";
        \li d{format_string} : date with optional parameters in "{}"-brackets which used by QDateTime::toString();
        \li m : message
        \li p : level name
        \li r : relative date/time to start application
        \li t : thread name
        \li x : ndc name
        \li X : mdc name
        \li F : file name
        \li M : method name
        \li L : line number

Log4Qt-1.5.1\src\log4qt\helpers\patternformatter.cpp

void PatternFormatter::createConverter(QChar character,
                                       Log4Qt::FormattingInfo formattingInfo,
                                       const QString &option)
{
    Q_ASSERT_X(mConversionCharacters.indexOf(character) >= 0, "PatternFormatter::createConverter", "Unknown conversion character" );

    LogError e("Creating Converter for character '%1' min %2, max %3, left %4 and option '%5'");
    e << QString(character)
      << FormattingInfo::intToString(formattingInfo.mMinLength)
      << FormattingInfo::intToString(formattingInfo.mMaxLength)
      << formattingInfo.mLeftAligned
      << option;
    logger()->trace(e);

    switch (character.toLatin1())
    {
    case 'c':
        mPatternConverters << new LoggepatternConverter(formattingInfo,
                           parseIntegeoption(option));
        break;
    case 'd':
    {
        QString format = option;
        if (option.isEmpty())
            format = QStringLiteral("ISO8601");
        else if (option == QLatin1String("locale:long"))
            format = QLocale().dateTimeFormat(QLocale::LongFormat);
        else if (option == QLatin1String("locale:short"))
            format = QLocale().dateTimeFormat(QLocale::ShortFormat);
        else if (option == QLatin1String("locale:narrow"))
            format = QLocale().dateTimeFormat(QLocale::NarrowFormat);
        else if (option == QLatin1String("locale"))
            format = QLocale().dateTimeFormat(QLocale::ShortFormat);
        mPatternConverters << new DatePatternConverter(formattingInfo,
                                                       format);
        break;
    }
    case 'm':
        mPatternConverters << new BasicPatternConverter(formattingInfo,
                                                        BasicPatternConverter::MESSAGE_CONVERTER);
        break;
    case 'p':
        mPatternConverters << new BasicPatternConverter(formattingInfo,
                                                        BasicPatternConverter::LEVEL_CONVERTER);
        break;
    case 'r':
        mPatternConverters << new DatePatternConverter(formattingInfo,
                                                       QStringLiteral("RELATIVE"));
        break;
    case 't':
        mPatternConverters << new BasicPatternConverter(formattingInfo,
                                                        BasicPatternConverter::THREAD_CONVERTER);
        break;
    case 'x':
        mPatternConverters << new BasicPatternConverter(formattingInfo,
                                                        BasicPatternConverter::NDC_CONVERTER);
        break;
    case 'X':
        mPatternConverters << new MDCPatternConverter(formattingInfo,
                                                      option);
        break;
    case 'F':
        mPatternConverters << new BasicPatternConverter(formattingInfo,
                                                        BasicPatternConverter::FILENAME_CONVERTER);
        break;
    case 'M':
        mPatternConverters << new BasicPatternConverter(formattingInfo,
                                                        BasicPatternConverter::FUNCTIONNAME_CONVERTER);
        break;
    case 'L':
        mPatternConverters << new BasicPatternConverter(formattingInfo,
                                                        BasicPatternConverter::LINENUMBER_CONVERTER);
        break;
    case 'l':
        mPatternConverters << new BasicPatternConverter(formattingInfo,
                                                        BasicPatternConverter::LOCATION_CONVERTER);
        break;
    default:
        Q_ASSERT_X(false, "PatternFormatter::createConverter", "Unknown pattern character");
    }
}

 

我个人的实践

QThread::currentThread()->setObjectName("MainThread");
MYLOG4QT_POINTER->debug("hello"); //这种方式不会输出文件名/行号/函数名
QString s = "sds";
MYLOG4QT_FATAL("hello2=%1,%2", 1234, s); //这种方式可以

完整的demo源码下载:https://download.csdn.net/download/libaineu2004/12293343

 

 

学习教程

Qt开发笔记:log4Qt介绍、编译和使用

Log4Qt快速入门——Log4Qt日志格式化源码解析

Log4Qt快速入门——Log4Qt日志输出重定向源码解析

Log4Qt的基本使用Qt高级——QMake用户指南

 

姊妹篇

《Qt日志重定向qInstallMessageHandler,输出至文件及网络》

《Spdlog日志库的使用,支持文件名/行号/函数名的打印输出》

《Qt 自定义日志工具》

 

---

你可能感兴趣的:(Qt技术,debug&&log)