log4cpp应用 -日志管理类接口
基于log4cpp开源项目,实现一个日志记录类接口,可以在日常软件开发中节省时间以及分析问题
该接口支持文件记录 、 支持终端流输出、支持调试输出记录,以及优先级设置,我应用的版本为
log4cpp-1.1rc3.tar.gz,大家可以在官方http://log4cpp.sourceforge.net/下载最新的版本源码
编译,我选择是在VS2008下编译log4cppLIB,编译的时候大家需要注意一点,把src\Localtime.hh
与src\Localtime.cpp文件加载到log4cppLIB工程中去,否则调用编译的lib文件时会出现#locatetime#
的错误;
该接口应用于软件开发中,文件日志记录,后台字符流日志输出以及调试日志显示功能,大家也可以
根据自己的需要增加适当的功能;
接口类定义如下:
#include "log4cpp/Category.hh"
#include "log4cpp/Win32DebugAppender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/PatternLayout.hh"
typedef log4cpp::Appender __log_appender;
typedef log4cpp::OstreamAppender __log_ostream_appender;
typedef log4cpp::Win32DebugAppender __log_debug_appender;
typedef log4cpp::FileAppender __log_file_appender;
typedef log4cpp::PatternLayout __log_pattern_layout;
typedef log4cpp::Category __log_category;
typedef log4cpp::Priority __log_priority;
//method define
#define _log_method_stream 102
#define _log_method_debug 101
#define _log_method_file 100
//priority command define
#define _log_priority_info 1
#define _log_priority_debug 2
#define _log_priority_error 3
#define _log_priority_warn 4
//record command define
#define _log_record_info 11
#define _log_record_warn 12
#define _log_record_error 13
#define _log_record_fatal 14
//marco define
#define _log_path "./gaius.log"
#define _log_copy "@GAIUS"
template <int inst>
class __sgi_log_template{
protected:
__sgi_log_template(){};
virtual ~ __sgi_log_template(){};
static __log_appender *pOsAppender;
static __log_pattern_layout *pLayOut;
static __log_category& pGory;
private:
static __log_category & __sgi_get_category(const char * categoryName)
{
return __log_category::getInstance(categoryName);
}
public:
static bool __sgi_log_inital(const int&/*bk or file*/);
static void __sgi_log_priority(const int&);
static void __sgi_log_event(const std::string&, const int&);
static void __sgi_log_over();
};
template <int inst>
__log_appender * __sgi_log_template<inst>::pOsAppender = 0;
template <int inst>
__log_pattern_layout * __sgi_log_template<inst>::pLayOut = 0;
template <int inst>
__log_category * __sgi_log_template<inst>::pGory = 0;
template <int inst>
inline bool __sgi_log_template<inst>::__sgi_log_inital(const int& type)
{
switch(type)
{
case _log_method_file:
pOsAppender = new __log_file_appender("FileAppender", _log_path); /* file stream */
break;
case _log_method_debug:
pOsAppender = new __log_debug_appender("debugAppender"); /* win32 debug stream output */
break;
case _log_method_stream:
pOsAppender = new __log_ostream_appender("osAppender", &std::cout); /* terminal stream output */
break;
default:
pOsAppender = new __log_file_appender("FileAppender", _log_path); /* file stream output */
break;
}
pLayOut = new __log_pattern_layout();
if(NULL != pOsAppender && NULL != pLayOut)
{
pLayOut->setConversionPattern("%d: %p %c %x: %m%n");
pOsAppender->setLayout(pLayOut);
__sgi_get_category(_log_copy).addAppender(pOsAppender);
return 1;
}
return 0;
}
template <int inst>
inline void __sgi_log_template<inst>::__sgi_log_priority(const int& prity)
{
switch (prity)
{
case _log_priority_info:
__sgi_get_category(_log_copy).setPriority(__log_priority::INFO);
break;
case _log_priority_debug:
__sgi_get_category(_log_copy).setPriority(__log_priority::DEBUG);
break;
case _log_priority_error:
__sgi_get_category(_log_copy).setPriority(__log_priority::ERROR);
break;
case _log_priority_warn:
__sgi_get_category(_log_copy).setPriority(__log_priority::WARN);
break;
default:
__sgi_get_category(_log_copy).setPriority(__log_priority::INFO);
break;
}
return ;
}
template <int inst>
inline void __sgi_log_template<inst>::__sgi_log_event(const std::string& strout, const int& type)
{
switch (type)
{
case _log_record_info:
__sgi_get_category(_log_copy).info(strout);
break;
case _log_record_warn:
__sgi_get_category(_log_copy).warn(strout);
break;
case _log_record_error:
__sgi_get_category(_log_copy).error(strout);
break;
case _log_record_fatal:
__sgi_get_category(_log_copy).fatal(strout);
break;
default:
__sgi_get_category(_log_copy).info(strout);
break;
}
return ;
}
template <int inst>
inline void __sgi_log_template<inst>::__sgi_log_over()
{
__log_category::shutdown();
}
typedef __sgi_log_template<0> __sgi_log;
接口类调用如下:
#include <iostream>
#include "sgi_log_t.h"
using namespace std;
int main()
{
//__sgi_log::__sgi_log_inital(_log_method_file);
//__sgi_log::__sgi_log_inital(_log_method_stream);
__sgi_log::__sgi_log_inital(_log_method_debug);
__sgi_log::__sgi_log_priority(_log_priority_info);
for(int i=0; i<100; i++)
{
__sgi_log::__sgi_log_event("test info", _log_record_info);
__sgi_log::__sgi_log_event("test warn", _log_record_warn);
__sgi_log::__sgi_log_event("test error", _log_record_error);
__sgi_log::__sgi_log_event("test fatal", _log_record_fatal);
Sleep(2000);
}
__sgi_log::__sgi_log_over();
getchar();
return 0;
}