bug:
problem: http://bugs.gentoo.org/217755
solution: http://bugs.gentoo.org/attachment.cgi?id=149763&action=view
5 包含头文件
日志记录
#include <log4cpp/Category.hh>
日志配置读取
#include <log4cpp/PropertyConfigurator.hh>
NDC
#include <log4cpp/NDC.hh>
9 日志代码
每个类可以有自己的类别(log4cpp::Category),
可以在配置文件中添加该类别并设置日志级别。
所有的log4cpp::Category都使用同一个Appender
不同的Category配置为不同的日志级别,就可以控制日志输出的范围。
一般只使用四个记录日志级:DEBUG,INFO,WARN,ERROR
如:
log4cpp::Category::getRoot().info("Now run line %d", __LINE__);
或使用非根类别
log4cpp::Category::getInstance("MyCat").info("Now run line %d", __LINE__);
使用流:
log4cpp::Category::getInstance("main_cat").infoStream()
<< "This will show up as "
<< 1 << " emergency message"
<< log4cpp::CategoryStream::ENDLINE;
具体的函数说明见api文档.
7 读取配置代码
读取log配置文件,应在log4cpp静态成员初始化之后。
如在CXXXApp::InitInstance()中
try
{
log4cpp::PropertyConfigurator::configure("log.ini");
}
catch (log4cpp::ConfigureFailure e)
{
log4cpp::Category::getRoot().warn(e.what());
}
8 配置文件
[log4cpp]
# Set root category priority to DEBUG and its only appender to A1.
# priority enum: "FATAL", "ALERT", "CRIT", "ERROR", "WARN",
# "NOTICE", "INFO", "DEBUG", "NOTSET", "UNKNOWN"
rootCategory=DEBUG,A1
additivity.rootCategory=false
# define appender
appender.A1=RollingFileAppender
#appender.A1.threshold=NOTSET
appender.A1.fileName=XXXX.log
#appender.A1.maxFileSize=10485760
#appender.A1.maxBackupIndex=1
appender.A1.layout=PatternLayout
appender.A1.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S}](%p)%c %x: %m%n
appender.Info_Cons=ConsoleAppender
appender.Info_Cons.threshold=INFO
appender.Info_Cons.layout=PatternLayout
appender.Info_Cons.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S}](%p)%c %x: %m%n
# category for sub1
category.sub1=DEBUG,A1,Info_Cons
additivity.sub1=false
category.sub2=INFO,A1
additivity.sub1=false
# other categories
[others]
djkf=dksajf
参考:
1, http://www.ibm.com/developerworks/cn/linux/l-log4cpp/index.html
2, http://www.usidcbbs.com/simple/?t1514.html
3, http://hi.baidu.com/pc_room/blog/item/5db43bfa46e8a415a8d31194.html
4, 重写日志库的感悟: http://blog.csdn.net/zlei12/archive/2007/02/08/1505663.aspx
5, http://blog.csdn.net/blueseason/archive/2010/07/27/5769389.aspx
#if defined(_LOG4CPP) && defined(WIN32)
# define MakePrefix std::string(__FUNCTION__).append("() - ")
# define LogDebug (LogDebuger(log, MakePrefix))
# define LogInfo (LogInfoer(log, MakePrefix))
# define LogNotice (LogNoticer(log, MakePrefix))
# define LogError (LogErrorer(log, MakePrefix))
#elif defined(_LOG4CPP) && !defined(WIN32)
# define MakePrefix(fmt) std::string(__FILE__).append("::").append(__FUNCTION__).append("() - ").append(fmt).c_str()
# define LogDebug(fmt, ...) log.debug(MakePrefix(fmt), ##__VA_ARGS__)
# define LogInfo(fmt, ...) log.info(MakePrefix(fmt), ##__VA_ARGS__)
# define LogNotice(fmt, ...) log.notice(MakePrefix(fmt), ##__VA_ARGS__)
# define LogError(fmt, ...) log.error(MakePrefix(fmt), ##__VA_ARGS__)
#else
# define LogDebug suck
# define LogInfo suck
# define LogNotice suck
# define LogError suck
#endif
参考: http://blog.csdn.net/simbi/archive/2009/11/20/4840582.aspx
/*#define MAKE_PREFIX(fmt) std::string(__FILE__).append(" ").append(__FUNCTION__).append(fmt).c_str()*/
#define MAKE_PREFIX(fmt) std::string("%s line %d:").append(fmt).c_str(),__FILE__ ,__LINE__
#define LOG_DEBUG(fmt,...) Logger::file_log.debug(MAKE_PREFIX(fmt),##__VA_ARGS__)
#define LOG_ERROR(fmt,...) Logger::file_log.error(MAKE_PREFIX(fmt),##__VA_ARGS__)
#define LOG_INFO(fmt,...) Logger::file_log.info(MAKE_PREFIX(fmt),##__VA_ARGS__)
#define LOG_NOTICE(fmt,...) Logger::file_log.notice(MAKE_PREFIX(fmt), ##__VA_ARGS__)
参考: http://blog.csdn.net/blueseason/archive/2010/07/27/5769389.aspx
方案一:
int my_printf(const char* format, ...)
{
va_list arg;
int done;
va_start(arg, format);
done = vprintf(format, arg);
va_end(arg);
return done;
}
方案二:
// file: global.h
#ifndef CLOBAL_H
#define GLOBAL_H
#include <stdio.h>
int funca(void);
int funcb(void);
#define DEBUGFMT "%s(%d)-%s"
#define DEBUGARGS __FILE__,__LINE__,__FUNCTION__
#endif
#include "global.h"
int funca(void)
{
printf (DEBUGFMT " this is function\n",DEBUGARGS);
return 0;
}
方案三:
#ifndef DBG_PRINTF(_x_)
#ifdef DEBUG
#define DBG_PRINTF(_x_) \
do { \
printf("%s(%d)--:",__FILE__,__LINE__); \
printf _x_; \
} while(0);
#else
#define DBG_PRINTF(_x_)
#endif
#endif
方案四:
#ifndef DBG_PRINTF
#ifdef DEBUG
#define DBG_PRINTF \
printf("%s(%d)--:",__FILE__,__LINE__); \
printf
#else
#define DBG_PRINTF
#endif
#endif
方案五:
#define my_printf(file, line, ARGS... ) \
do { printf("%s(%d)--:", file, line); \
printf(ARGS); } while(0)
#define DEG_PRINTF(...) my_printf(__FILE__, __LINE__, ##__VA_ARGS__);
参考: http://www.upsdn.net/html/2006-08/692.html
方案六:
void (*publish)(PublishLoc *, RLogChannel *, const char *format, ... )
PRINTF_FP(3,4);
RLogNode *pub;
const char *component;
const char *fileName;
const char *functionName;
int lineNum;
RLogChannel *channel;
};
#define _rMessageDef(ID, COMPONENT) \
static rlog::PublishLoc ID ={&rlog::RLog_Register, 0, STR(COMPONENT), \
__FILE__, __FUNCTION__, __LINE__, 0};
#if HAVE_PRINTF_FP || !HAVE_PRINTF_ATTR
# define _rMessageCall(ID, CHANNEL, ARGS...) \
if(unlikely(ID.publish!=0)) (*ID.publish)( &ID, CHANNEL, ##ARGS );
#else
# define _rMessageCall(ID, CHANNEL, ARGS...) \
if(unlikely(ID.publish!=0)) \
{ \
(*ID.publish)( &ID, CHANNEL, ##ARGS ); \
rlog::__checkArgs( 0, ##ARGS ); \
}
#endif
#define _rMessage(ID, CHANNEL, ARGS... ) \
do { _rMessageDef(ID, RLOG_COMPONENT) \
_rMessageCall(ID, CHANNEL, ##ARGS ) } while(0)
#define rDebug(ARGS...) \
_rMessage( LOGID, rlog::_RLDebugChannel, ##ARGS )
#define rInfo(ARGS...) \
_rMessage( LOGID, rlog::_RLInfoChannel, ##ARGS )
#define rWarning(ARGS...) \
_rMessage( LOGID, rlog::_RLWarningChannel, ##ARGS )
#define rError(ARGS...) \
_rMessage( LOGID, rlog::_RLErrorChannel, ##ARGS )
#define rLog(channel, ARGS...) \
_rMessage( LOGID, channel, ##ARGS )