log4cplus的包裹类(log4cplus Wrapper)

log4cplus的包裹类(log4cplus Wrapper)

头文件:

/**********************************************************************
* Copyright (C) 2008 - - All Rights Reserved
*
* 文件名称:        LoggerModule.h
* 摘    要:        日志包裹类,日志模块的接口
*     
* 作    者:        yanglinbo, 
* 修    改:        查看文件最下方.

***********************************************************************/


#ifndef __LoggerModule_H__
#define __LoggerModule_H__


// 导出定义
#ifndef LOGGER_MODULE_CLASS
    #ifdef  LOGGER_MODULE_DLL
         #define LOGGER_MODULE_CLASS _declspec(dllexport)
     #else
         #define LOGGER_MODULE_CLASS _declspec(dllimport)
     #endif
#endif

// 模块定义
#ifdef _DEBUG
     #define LOGGER_MODULE_DLL_NAME    TEXT("LoggerModuleD.dll")             // 组件 DLL 名字
#else
     #define LOGGER_MODULE_DLL_NAME    TEXT("LoggerModule.dll")             // 组件 DLL 名字
#endif


/// 包裹类
/// @note 在程序启动的时候调用Logger::Start方法,例如:
///       g_Logger.StartSystem("logModule.properties");
///       打日志的示例如下:
///       g_Logger.Debug(__FILE__, __LINE__, "Debug log[%d]", 100);
///       用法基本上和printf()和CSting::Format()方法差不多。
///       如果是VS2005以及以上的开发环境,可以使用宏打日志,更加方便:
///       LOG_DEBUG("Debug log[%d]", 100);

class LOGGER_MODULE_CLASS Logger
{
public:
    Logger();
    virtual ~Logger();

    /// 启动日志系统
    /// @param[in] properties_filename 日志系统配置文件文件名

    void StartSystem(const char* properties_filename);

    /// 关闭日志系统
    void StopSystem();

public:
    void Debug(const char* filename, const int fileline, const char* pFormat, );

    void Error(const char* filename, const int fileline, const char* pFormat, );

    void Fatal(const char* filename, const int fileline, const char* pFormat, );

    void Info(const char* filename, const int fileline, const char* pFormat, );

    void Warn(const char* filename, const int fileline, const char* pFormat, );

    void Trace(const char* filename, const int fileline, const char* pFormat, );

public:
    static inline Logger* getSingletonPtr()
    {
        return &getSingleton();
    }

    static inline Logger& getSingleton()
    {
        static Logger _instance; 
        return _instance;
    }

}
;
#define g_Logger Logger::getSingleton()
#define g_pLogger Logger::getSingleton()


//
//  断言日志
//
#define ASSERT_LOG(expr)\
     if ( (expr) )  {;}  else g_Logger.Error(__FILE__, __LINE__, #expr);


//
//  以下的宏只有VS2005以及之上的版本可以使用!因为VS2005之下的版本不支持可变参数宏
//
#if defined(_MSC_VER) && _MSC_VER > 1400 
     #define LOG_DEBUG( )    g_Logger.Debug(__FILE__, __LINE__, __VA_ARGS__);
     #define LOG_ERROR( )    g_Logger.Error(__FILE__, __LINE__, __VA_ARGS__);
     #define LOG_FATAL( )    g_Logger.Fatal(__FILE__, __LINE__, __VA_ARGS__);
     #define LOG_INFO( )    g_Logger.Info(__FILE__, __LINE__, __VA_ARGS__);
     #define LOG_WARN( )    g_Logger.Warn(__FILE__, __LINE__, __VA_ARGS__);
     #define LOG_TRACE( )    g_Logger.Trace(__FILE__, __LINE__, __VA_ARGS__);
#endif


#endif



cpp文件:

/**********************************************************************
* Copyright (C) 2008 -  - All Rights Reserved
*
* 文件名称:        LoggerModule.cpp
* 摘    要:        日志包裹类,日志模块的接口
*     
* 作    者:        yanglinbo, 
* 修    改:        查看文件最下方.

***********************************************************************/


#include "LoggerModule.h"

#include <cstdlib>
#include <iostream>
#include <log4cplus/config.hxx>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/helpers/stringhelper.h>
#include <log4cplus/helpers/socket.h>
#include <log4cplus/helpers/threads.h>
#include <log4cplus/spi/loggerimpl.h>
#include <log4cplus/spi/loggingevent.h>
// #include <stdarg.h>

Logger::Logger()
{

}


Logger::~Logger()
{
    log4cplus::Logger _logger = log4cplus::Logger::getRoot();
    LOG4CPLUS_INFO(_logger, "Logger System Stop Finish.");
}


#define DO_LOGGER(logLevel, filename, fileline, pFormat, bufSize)\
    log4cplus::Logger _logger = log4cplus::Logger::getRoot();\
    \
     if(_logger.isEnabledFor(logLevel))\
{                \
    va_list args;            \
    va_start(args, pFormat);        \
    char buf[bufSize] = {0};        \
    _vsnprintf(buf, sizeof(buf), pFormat, args);    \
    va_end(args);           \
    _logger.forcedLog(logLevel, buf, filename, fileline); \
}


void Logger::Debug(  const  char* filename,  const  int fileline,  const  char* pFormat,   )
{
    DO_LOGGER(log4cplus::DEBUG_LOG_LEVEL, filename, fileline, pFormat, 1024);
}


void Logger::Error(  const  char* filename,  const  int fileline,  const  char* pFormat,   )
{
    DO_LOGGER(log4cplus::ERROR_LOG_LEVEL, filename, fileline, pFormat, 256);
}


void Logger::Fatal(  const  char* filename,  const  int fileline,  const  char* pFormat,   )
{
    DO_LOGGER(log4cplus::FATAL_LOG_LEVEL, filename, fileline, pFormat, 256);
}


void Logger::Info(  const  char* filename,  const  int fileline,  const  char* pFormat,   )
{
    DO_LOGGER(log4cplus::INFO_LOG_LEVEL, filename, fileline, pFormat, 512);
}


void Logger::Warn(  const  char* filename,  const  int fileline,  const  char* pFormat,   )
{
    DO_LOGGER(log4cplus::WARN_LOG_LEVEL, filename, fileline, pFormat, 256);
}


void Logger::Trace(  const  char* filename,  const  int fileline,  const  char* pFormat,   )
{
    DO_LOGGER(log4cplus::TRACE_LOG_LEVEL, filename, fileline, pFormat, 1024);
}


void Logger::StartSystem(  const  char* properties_filename )
{
    if (properties_filename==NULL) return;

    log4cplus::helpers::LogLog::getLogLog()->setInternalDebugging(false);
    log4cplus::PropertyConfigurator::doConfigure(properties_filename);
    log4cplus::Logger _logger = log4cplus::Logger::getRoot();
    LOG4CPLUS_INFO(_logger, "Logger System Start Finish.");
}


void Logger::StopSystem()
{

}


自定义配置文件样例:

# Define the root logger
log4cplus.rootLogger=TRACE, consoleAppender, fileAppender

# Define a file appender named "consoleAppender"
log4cplus.appender.consoleAppender=log4cplus::ConsoleAppender
log4cplus.appender.consoleAppender.layout=log4cplus::PatternLayout
log4cplus.appender.consoleAppender.layout.ConversionPattern=%-5p-[%t][%D {%H:%M:%S %Q}]%m

# Define a file appender named "fileAppender"
log4cplus.appender.fileAppender=log4cplus::DailyRollingFileAppender
log4cplus.appender.fileAppender.MaxFileSize=200KB
log4cplus.appender.fileAppender.File=./log.log
log4cplus.appender.fileAppender.MaxBackupIndex=3
log4cplus.appender.fileAppender.layout=log4cplus::PatternLayout
log4cplus.appender.fileAppender.layout.ConversionPattern=%d {%m/%d/%y %H:%M:%S}  - %m [%l]%n


使用的例子:

    g_Logger.StartSystem("logModule.properties");
    g_Logger.Debug(__FILE__, __LINE__, "Debug log[%d]", 100);


打印出来的结果如下所示:

06/08/09 10:52:42  - Logger System Start Finish. [e:\workspace\共享组件\日志组件\loggermodule.cpp:32]
06/08/09 10:52:42  - Debug log[100] [e:\workspace\服务器组件\好友服务器\friendserverdlg.cpp:160]
06/08/09 10:52:44  - Logger System Stop Finish. [e:\workspace\共享组件\日志组件\loggermodule.cpp:38]

转载于:https://www.cnblogs.com/rosesmall/archive/2012/05/02/2479352.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值