Log4Qt使用方法

1、将解压好的Log4Qt放在.pro文件所在目录,在.pro文件里面引入该库的.pri文件

include($$PWD/log4qt/log4qt.pri)
INCLUDEPATH += $$PWD/log4qt

2、在项目中加入以下LogHelper.h   LogHelper.cpp文件

#ifndef __LOG_HELPER__
#define __LOG_HELPER__
#include <QString>
#include <QByteArray>
#include "log4qt/basicconfigurator.h"
#include "log4qt/consoleappender.h"
#include "log4qt/dailyrollingfileappender.h"
#include "log4qt/fileappender.h"
#include "log4qt/helpers/configuratorhelper.h"
#include "log4qt/helpers/datetime.h"
#include "log4qt/helpers/factory.h"
#include "log4qt/helpers/initialisationhelper.h"
#include "log4qt/helpers/optionconverter.h"
#include "log4qt/helpers/patternformatter.h"
#include "log4qt/helpers/properties.h"
#include "log4qt/logmanager.h"
#include "log4qt/loggerrepository.h"
#include "log4qt/patternlayout.h"
#include "log4qt/propertyconfigurator.h"
#include "log4qt/rollingfileappender.h"
#include "log4qt/simplelayout.h"
#include "log4qt/ttcclayout.h"
#include "log4qt/varia/denyallfilter.h"
#include "log4qt/varia/levelmatchfilter.h"
#include "log4qt/varia/levelrangefilter.h"
#include "log4qt/varia/stringmatchfilter.h"

using namespace Log4Qt;

#if ( defined(DEBUG) || defined(_DEBUG) || defined(RELEASE_DEBUG_MODE) )

#define LOG_QT_TRACE(logPtr, s)  do{logPtr->trace( QString::fromLocal8Bit("[File:%1, Line:%2] %3").arg(__FILE__).arg(__LINE__).arg(s));}while(0);
#define LOG_QT_DEBUG(logPtr, s)  do{logPtr->debug( QString::fromLocal8Bit("[File:%1, Line:%2] %3").arg(__FILE__).arg(__LINE__).arg(s));}while(0);
#define LOG_QT_INFO(logPtr, s)   do{logPtr->info( QString::fromLocal8Bit("[File:%1, Line:%2] %3").arg(__FILE__).arg(__LINE__).arg(s));}while(0);
#define LOG_QT_WARN(logPtr, s)   do{logPtr->warn( QString::fromLocal8Bit("[File:%1, Line:%2] %3").arg(__FILE__).arg(__LINE__).arg(s));}while(0);
#define LOG_QT_ERROR(logPtr, s)  do{logPtr->error( QString::fromLocal8Bit("[File:%1, Line:%2] %3").arg(__FILE__).arg(__LINE__).arg(s));}while(0);
#define LOG_QT_FATAL(logPtr, s)  do{logPtr->fatal( QString::fromLocal8Bit("[File:%1, Line:%2] %3").arg(__FILE__).arg(__LINE__).arg(s));}while(0);

class QtFunctionTrace
{
public:
    QtFunctionTrace(int nLevel, const QString& AppNameStr, const QString& FuncNameStr );
    ~QtFunctionTrace();
private:
    int level;
    QString AppName;
    QString FuncName;
};

#define FUNTION_TRACE_TRACE()  QtFunctionTrace QFunctionTraceObject(1,__FILE__,__FUNCTION__);
#define FUNTION_TRACE_DEBUG()  QtFunctionTrace QFunctionTraceObject(2,__FILE__,__FUNCTION__);
#define FUNTION_TRACE_INFO()   QtFunctionTrace QFunctionTraceObject(3,__FILE__,__FUNCTION__);
#define FUNTION_TRACE_WARN()   QtFunctionTrace QFunctionTraceObject(4,__FILE__,__FUNCTION__);
#define FUNTION_TRACE_ERROR()  QtFunctionTrace QFunctionTraceObject(5,__FILE__,__FUNCTION__);
#define FUNTION_TRACE_FATAL()  QtFunctionTrace QFunctionTraceObject(6,__FILE__,__FUNCTION__);

#else

#define LOG_QT_TRACE(logPtr, s) {logPtr;}
#define LOG_QT_DEBUG(logPtr, s) {logPtr;}
#define LOG_QT_INFO(logPtr, s)  {logPtr;}
#define LOG_QT_WARN(logPtr, s)  {logPtr;}
#define LOG_QT_ERROR(logPtr, s) {logPtr;}
#define LOG_QT_FATAL(logPtr, s) {logPtr;}

#define FUNTION_TRACE_TRACE()
#define FUNTION_TRACE_DEBUG()
#define FUNTION_TRACE_INFO()
#define FUNTION_TRACE_WARN()
#define FUNTION_TRACE_ERROR()
#define FUNTION_TRACE_FATAL()

#endif


void DumpBuffer( Log4Qt::Logger* log, const char* pData, int bytes, const Log4Qt::Level& level =  Log4Qt::Level( Log4Qt::Level::TRACE_INT) );
void DumpBuffer( Log4Qt::Logger* log, const QByteArray& Data, const Log4Qt::Level& level =  Log4Qt::Level( Log4Qt::Level::TRACE_INT) );

#endif
#include "LogHelper.h"
#include "stdio.h"

void DumpBuffer(Log4Qt::Logger *log, const char *buffer, int length, const Log4Qt::Level& level )
{
    unsigned int i,count,index;
    char rgbDigits[]="0123456789abcdef";
    char rgbLine[100] = {0};
    int cbLine = 0;

    for(index = 0; length; length -= count, buffer += count, index += count)
    {
        count = (length > 16) ? 16:length;
        sprintf(rgbLine, "%4.4x  ",index);
        cbLine = 6;

        for(i=0;i<count;i++)
        {
            rgbLine[cbLine++] = rgbDigits[ (buffer[i] >> 4)& 0x0F ];
            rgbLine[cbLine++] = rgbDigits[buffer[i] & 0x0f];
            if(i == 7)
            {
                rgbLine[cbLine++] = ':';
            }
            else
            {
                rgbLine[cbLine++] = ' ';
            }
        }
        for(; i < 16; i++)
        {
            rgbLine[cbLine++] = ' ';
            rgbLine[cbLine++] = ' ';
            rgbLine[cbLine++] = ' ';
        }

        rgbLine[cbLine++] = ' ';

        for(i = 0; i < count; i++)
        {
            if(buffer[i] < 32 || buffer[i] > 126)
            {
                rgbLine[cbLine++] = '.';
            }
            else
            {
                rgbLine[cbLine++] = buffer[i];
            }
        }
        rgbLine[cbLine++] = 0;
        if ( level == Log4Qt::Level(Log4Qt::Level::TRACE_INT) )
        {
            log->trace(rgbLine);
        }
        else if ( level == Log4Qt::Level(Log4Qt::Level::DEBUG_INT) )
        {
            log->debug( rgbLine );
        }
        else if ( level == Log4Qt::Level(Log4Qt::Level::INFO_INT) )
        {
            log->info( rgbLine );
        }
        else if ( level == Log4Qt::Level(Log4Qt::Level::WARN_INT) )
        {
            log->warn( rgbLine );
        }
        else if ( level == Log4Qt::Level(Log4Qt::Level::ERROR_INT) )
        {
            log->error( rgbLine );
        }
        else if ( level == Log4Qt::Level(Log4Qt::Level::FATAL_INT) )
        {
            log->fatal( rgbLine );
        }
        memset(rgbLine, 0, sizeof( rgbLine) );
    }
}

void DumpBuffer(Logger *log, const QByteArray &Data, const Level &level)
{
    const char* p = Data.data();
    int length = Data.length();
    DumpBuffer( log, p, length, level);
}

#if ( defined(DEBUG) || defined(_DEBUG) || defined(RELEASE_DEBUG_MODE) )

QtFunctionTrace::QtFunctionTrace(int nLevel, const QString& AppNameStr, const QString& FuncNameStr )
{
    AppName = AppNameStr;
    FuncName = FuncNameStr;
    level = nLevel;
    Log4Qt::Logger* logPtr =  Log4Qt::Logger::rootLogger();
    if ( logPtr == NULL )
    {
        return;
    }
    if ( level == 1 )
    {
        logPtr->trace(QString::fromLocal8Bit("%1--%2--enter").arg(AppName).arg(FuncName));
    }
    else if ( level == 2 )
    {
        logPtr->debug(QString::fromLocal8Bit("%1--%2--enter").arg(AppName).arg(FuncName));
    }
    else if ( level == 3 )
    {
        logPtr->info(QString::fromLocal8Bit("%1--%2--enter").arg(AppName).arg(FuncName));
    }
    else if ( level == 4 )
    {
        logPtr->warn(QString::fromLocal8Bit("%1--%2--enter").arg(AppName).arg(FuncName));
    }
    else if ( level == 5 )
    {
        logPtr->error(QString::fromLocal8Bit("%1--%2--enter").arg(AppName).arg(FuncName));
    }
    else if ( level == 6 )
    {
        logPtr->fatal(QString::fromLocal8Bit("%1--%2--enter").arg(AppName).arg(FuncName));
    }
}

QtFunctionTrace::~QtFunctionTrace()
{
    Log4Qt::Logger* logPtr = Log4Qt::Logger::rootLogger();
    if ( logPtr == NULL )
    {
        return;
    }
    if ( level == 1 )
    {
        logPtr->trace(QString::fromLocal8Bit("%1--%2--leave").arg(AppName).arg(FuncName));
    }
    else if ( level == 2 )
    {
        logPtr->debug(QString::fromLocal8Bit("%1--%2--leave").arg(AppName).arg(FuncName));
    }
    else if ( level == 3 )
    {
        logPtr->info(QString::fromLocal8Bit("%1--%2--leave").arg(AppName).arg(FuncName));
    }
    else if ( level == 4 )
    {
        logPtr->warn(QString::fromLocal8Bit("%1--%2--leave").arg(AppName).arg(FuncName));
    }
    else if ( level == 5 )
    {
        logPtr->error(QString::fromLocal8Bit("%1--%2--leave").arg(AppName).arg(FuncName));
    }
    else if ( level == 6 )
    {
        logPtr->fatal(QString::fromLocal8Bit("%1--%2--leave").arg(AppName).arg(FuncName));
    }
}

#endif
#ifndef __Q_CONFIG_H__
#define __Q_CONFIG_H__
#include <QSettings>

class QConfig
{
private:
    static QSettings* config;
public:
    static void Init(const QString& path, const QString &fileName );
    static void Destroy();
    static QSettings& setting();
};

#endif
#include "QConfig.h"

QSettings* QConfig::config = 0;

void QConfig::Init(const QString &path, const QString &fileName)
{
    if ( config != 0 )
    {
        delete config;
    }
    QString fullFilePath = path + QString("/Config/") + fileName;
    config = new QSettings( fullFilePath, QSettings::IniFormat );
    config->setIniCodec("UTF-8" );
}

void QConfig::Destroy()
{
    if ( config != 0 )
    {
        delete config;
    }
    config = 0;
}

QSettings& QConfig::setting()
{
    return (*config);
}

将以下Config.ini文件放在编译目录XXXX/Debug/Config中

3、使用时在项目初始化时候加入以下语句

    QString libPath = QCoreApplication::applicationDirPath();
    QDir::setCurrent ( libPath );  // set current work dir
    QCoreApplication::addLibraryPath( libPath );

    QString curPath = QDir::currentPath();
    Log4Qt::PropertyConfigurator::configure(libPath + "/Config/log4qt.conf");

使用 

#include"LogHelper.h"

{
    LOG_QT_FATAL(log, QString("open srcfile[%1] fail!").arg(m_srcFileName ) );
    LOG_QT_TRACE(log, QString("Convert Success!") );
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值