分类: C/C++
2009-02-08 22:39:43
#ifndef _LOGGER_H #define _LOGGER_H #include using namespace std; #ifdef USE_LOG4CPP #include "log4cpp/Category.hh" #include "log4cpp/CategoryStream.hh" #include "log4cpp/PropertyConfigurator.hh" using namespace log4cpp; #elif defined USE_LOG4CXX #include #include using namespace log4cxx; #endif #define LoggerName "MyApp" #define AppenderName "rootAppender" #ifdef USE_LOG4CPP extern log4cpp::Category *logger_; #define LOG_DEBUG(msg) \ LOG4CPP_DEBUG_S((*logger_))< LOG4CPP_INFO_S((*logger_))< LOG4CPP_WARN_S((*logger_))< LOG4CPP_ERROR_S((*logger_))< LOG4CPP_FATAL_S((*logger_))< #elif defined USE_LOG4CXX extern LoggerPtr logger_; #define LOG_DEBUG(msg) \ {\ stringstream temp_; \ temp_< } #define LOG_INFO(msg) \ {\ stringstream temp_; \ temp_< } #define LOG_WARN(msg) \ {\ stringstream temp_; \ temp_< } #define LOG_ERROR(msg) \ {\ stringstream temp_; \ temp_< } #define LOG_FATAL(msg) \ {\ stringstream temp_; \ temp_< } #endif void InitLog(const char *conf_file); #endif /*_LOGGER_H*/ |
#include "logger.h" #ifdef USE_LOG4CPP log4cpp::Category *logger_; #elif defined USE_LOG4CXX LoggerPtr logger_(Logger::getLogger(LoggerName)); #endif void InitLog(const char *conf_file) { #ifdef USE_LOG4CPP log4cpp::PropertyConfigurator::configure(conf_file); log4cpp::Category& log = log4cpp::Category::getInstance(string(AppenderName)); logger_ = &log; #elif defined USE_LOG4CXX PropertyConfigurator::configure(File(conf_file)); // Configure file, log4j format logger_ = Logger::getLogger("org.apache.log4j.RollingFileAppender"); // Appender name #endif } #if 0 int main(int argc, char* argv[]) { string conf_file = "./log.conf"; InitLog(conf_file.c_str()); LOG_DEBUG("This is Some Debug Message."); LOG_INFO("This is Some Info Message."); LOG_WARN("This is Some Warn Message."); LOG_ERROR("This is Some Error Message."); LOG_FATAL("This is Some Fatal Message."); return 0; } #endif |
log4j.rootCategory=INFO, rootAppender log4j.Category.rootAppender=INFO log4j.appender.rootAppender=org.apache.log4j.RollingFileAppender #由于log4cpp无法处理"MB"等字母表示的文件大小,为了兼容性,建议直接使用数字表示。 #log4j.appender.rootAppender.maxFileSize = 1MB log4j.appender.rootAppender.maxFileSize = 1024000 log4j.appender.rootAppender.maxBackupIndex = 2 log4j.appender.rootAppender.append=true log4j.appender.rootAppender.fileName=emule.log log4j.appender.rootAppender.layout=org.apache.log4j.PatternLayout #log4j.appender.rootAppender.layout.ConversionPattern=%p [%d] %F | %m%n log4j.appender.rootAppender.layout.ConversionPattern=[%d] %-5p | %m%n |
CXX=g++ CXXFLAGS=-g -fPIC -c -DUSE_LOG4CPP LDFLAGS=-llog4cpp OBJS= logger.o PROG=t_log all: t_log .cpp.o : $(CXX) $(CXXFLAGS) $(INCLUDE) $< t_log: t_log.o $(OBJS) $(CXX) -o $@ $^ $(LDFLAGS) clean: rm -f *.o rm -f $(PROG) |
|