头文件:
/* *********************************************************************
* 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
* 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()
{
}
* 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
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 );
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 ]
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 ]
参考资料:
1. 在VC2003中编译x264 http://203.208.35.132/search?q=cache:iVR-sSgopycJ:xieliming.blogspot.com/2008/12/vc2003x264.html+%E5%AE%8F+%E5%8F%AF%E5%8F%98%E5%8F%82+vc2003&cd=7&hl=zh-CN&ct=clnk&gl=cn&client=aff-os-maxthon&st_usg=ALhdy28fuo_AwxsIvnD2K3IRGbPD8wlJJQ
2. 我的Log4c http://www.cppblog.com/cool-liangbing/archive/2009/02/12/73532.html
3.【原创】技术系列之 必备外围功能-log http://www.cppblog.com/CppExplore/archive/2009/02/08/52216.html