private: };
|
Demo.cpp 文件主要内容:
Demo::Demo() Demo::~Demo() void Demo::TestIt(int i, int j)
|
再写个例子来运行,可以看到类似输出
[INFO] - Demo::Demo() -
[INFO] - Demo::TestIt() - i=1, j=2
[INFO] - Demo::~Demo() -
最后附上完整的
LogUtils.h
LogUtils.cpp
LogTracer.h (这个文件是因为 vc 不支持可变参数的宏,所以采用的取巧方法,这方法是从网上搜来的,出处忘了,请见谅。)
LogUtils.h
#include <string> #if defined(_LOG4CPP) #include <log4cpp/Category.hh> namespace log4cpp #endif namespace dk void InitializeLog4cpp(const std::string & logfile); #if defined (_LOG4CPP) #if defined (_LOG4CPP) void suck(const char * fmt, ...); #if defined(_LOG4CPP) && defined(WIN32) } #endif |
LogUtils.cpp
#if defined(_LOG4CPP) #include <log4cpp/PropertyConfigurator.hh> // appenders #include <log4cpp/Appender.hh> #ifdef LOG4CPP_HAVE_SYSLOG // layouts #include <log4cpp/Layout.hh> #include <log4cpp/Priority.hh> #endif namespace dk // log4cpp::Appender * appender = new log4cpp::Win32DebugAppender("console"); //#else log4cpp::Appender * appender = new log4cpp::OstreamAppender("console", &std::cout); log4cpp::PatternLayout * patternLayout = new log4cpp::PatternLayout(); appender->setLayout(patternLayout); void ReleaseLog4cpp() #if defined(_LOG4CPP) return log4cpp::Category::getInstance(name); void suck(const char * fmt, ...) } |
LogTracer.h
#if defined(_LOG4CPP) && defined(WIN32) #include <log4cpp/Category.hh> namespace log4cpp namespace dk #include <stdarg.h> class LogTracer va_start(ap, fmt); AppendString(mMsg, fmt, ap); va_end(ap); void AppendString(std::string & message, const char * format, va_list args) if ((n > -1) && (static_cast<size_t>(n) < size)) size = (n > -1) ? size * 2; // twice the old size delete [] buffer; private: LogTracer(const LogTracer &); protected: class LogDebuger class LogInfoer class LogNoticer class LogErrorer } #endif #endif |