全部博文(96)
分类: C/C++
2010-11-08 15:26:49
直接上源代码了。
/*
* Copyright (c) 2009, chrono
* All rights reserved.
*
* 文件名称:stdlog.cpp
* 文件标识:
* 摘 要:
*
* 版 本:1.0
* 作 者:chrono
* 创建日期:2009/10/19 9:41
*/
#include "stdlog.h"
#include
#include
#include
#include
// for vsprintf [2009/10/20 chrono]
#include
#include
#include
#include
// #include
using namespace std;
using namespace eplog;
using namespace log4cplus;
// 日志的输出格式 [2009/10/19 chrono]
#define MYLOG_PATTERN "{%D} {%-5p} {%t} - %m %n"
#define LOG_OBJ Logger::getRoot()
// 日志单件 [2009/10/19 chrono]
stdlog& logger = stdlog::instance();
// namespace
// {
// static Logger root = Logger::getRoot();
// Logger root = Logger::getInstance("test");
// }
/** stdlog
* fullname: eplog::stdlog::stdlog
* access: private
* @brief
*
* @param void
* @return 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/20 10:05
*/
stdlog::stdlog(void)
{
operator()();
m_infoLevel = InfoLevel;
m_logLevel = TraceLevel;
}
stdlog::~stdlog(void)
{
}
/** instance
* fullname: eplog::stdlog::instance
* access: public
* @brief
*
* @return stdlog& 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 9:45
*/
stdlog& stdlog::instance()
{
static stdlog alog;
return alog;
}
/** debug
* fullname: eplog::stdlog::debug
* access: public
* @brief
*
* @param const char * msg
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 9:51
*/
void stdlog::debug( const char* msg )
{
LOG4CPLUS_DEBUG(LOG_OBJ, msg);
}
/** info
* fullname: eplog::stdlog::info
* access: public
* @brief
*
* @param const char * msg
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 12:07
*/
void stdlog::info( const char* msg )
{
LOG4CPLUS_INFO(LOG_OBJ, msg);
}
/** warning
* fullname: eplog::stdlog::warning
* access: public
* @brief
*
* @param const char * msg
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 14:13
*/
void stdlog::warning( const char* msg )
{
LOG4CPLUS_WARN(LOG_OBJ, msg);
}
/** error
* fullname: eplog::stdlog::error
* access: public
* @brief
*
* @param const char * msg
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 14:13
*/
void stdlog::error( const char* msg )
{
LOG4CPLUS_ERROR(LOG_OBJ, msg);
}
/** fatal
* fullname: eplog::stdlog::fatal
* access: public
* @brief
*
* @param const char * msg
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 14:13
*/
void stdlog::fatal( const char* msg )
{
LOG4CPLUS_FATAL(LOG_OBJ, msg);
}
/** trace
* fullname: eplog::stdlog::trace
* access: public
* @brief
*
* @param const char * msg
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 14:13
*/
void stdlog::trace( const char* msg )
{
LOG4CPLUS_TRACE(LOG_OBJ, msg);
}
/** operator<<
* fullname: eplog::stdlog::operator<<
* access: public
* @brief
*
* @param StdLogLevel * level
* @return stdlog& 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 13:40
*/
stdlog& stdlog::operator<<( StdLogLevel level )
{
m_infoLevel = level;
return *this;
}
/** operator<<
* fullname: eplog::stdlog::operator<<
* access: public
* @brief 重载《《,输出info日志
*
* @param const char * msg
* @return stdlog& 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 13:20
*/
stdlog& stdlog::operator<<( const char *msg )
{
switch(m_infoLevel)
{
case TraceLevel:
trace(msg);break;
case DebugLevel:
debug(msg);break;
case InfoLevel:
info(msg);break;
case WarningLevel:
warning(msg);break;
case ErrorLevel:
error(msg);break;
case FatalLevel:
fatal(msg);break;
default://off
break;//info(msg);
}
return *this;
}
/** format
* fullname: eplog::stdlog::format
* access: public
* @brief
*
* @param const char * szFormat
* @param ...
* @return std::string 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 16:51
*/
const char* stdlog::format( const char* szFormat, ... )
{
static vector
va_list args;
// retrieve the variable arguments
va_start( args, szFormat );
// _vscprintf只能在windows上使用 [2009/10/19 chrono]
buffer.reserve (_vscprintf( szFormat, args ) + 1);
vsprintf( &buffer[0], szFormat, args ); // C4996
return &buffer[0];
}
/** bin2str
* fullname: eplog::stdlog::bin2str
* access: public
* @brief 将缓冲数据转换为十六进制字符串
*
* @param unsigned char * buf
* @param int nstrlen
* @return std::string 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 10:00
*/
std::string stdlog::bin2str( const unsigned char *buf, int nstrlen )
{
stringstream ss;
ss.setf(ios::hex | ios::uppercase );
ss.width(2);
ss.fill('0');
for (int i = 0;i < nstrlen ; ++i)
{
ss << hex << (short)buf[i];
}
return ss.str();
}
/** setLogLevel
* fullname: eplog::stdlog::setLogLevel
* access: public
* @brief 设置日志记录器的级别
*
* @param StdLogLevel level
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 14:22
*/
void stdlog::setLogLevel( StdLogLevel level )
{
m_logLevel = level;
switch(m_logLevel)
{
case TraceLevel:
LOG_OBJ.setLogLevel(TRACE_LOG_LEVEL);break;
case DebugLevel:
LOG_OBJ.setLogLevel(DEBUG_LOG_LEVEL);break;
case WarningLevel:
LOG_OBJ.setLogLevel(WARN_LOG_LEVEL);break;
case ErrorLevel:
LOG_OBJ.setLogLevel(ERROR_LOG_LEVEL);break;
case FatalLevel:
LOG_OBJ.setLogLevel(FATAL_LOG_LEVEL);break;
case OffLevel:
LOG_OBJ.setLogLevel(OFF_LOG_LEVEL);break;
default:
LOG_OBJ.setLogLevel(INFO_LOG_LEVEL);break;
}
}
/** operator>>
* fullname: eplog::stdlog::operator>>
* access: public
* @brief 同setLogLevel
*
* @param StdLogLevel level
* @return stdlog& 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 14:38
*/
stdlog& stdlog::operator=( StdLogLevel level )
{
setLogLevel(level);
return *this;
}
/** useConsole
* fullname: eplog::stdlog::useConsole
* access: private
* @brief
*
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 15:02
*/
void stdlog::useConsole()
{
// m_append = Console;
LOG_OBJ.removeAllAppenders();
SharedAppenderPtr append(new ConsoleAppender());
append->setName("mylog");
string pattern = MYLOG_PATTERN;
auto_ptr
append->setLayout(lay);
LOG_OBJ.addAppender(append);
}
/** useRollingFile
* fullname: eplog::stdlog::useRollingFile
* access: private
* @brief
*
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 15:04
*/
void stdlog::useRollingFile( const char* szFileName )
{
// m_append = RollingFile;
LOG_OBJ.removeAllAppenders();
SharedAppenderPtr append(new RollingFileAppender(szFileName));
append->setName("myrflog");
string pattern = MYLOG_PATTERN;
auto_ptr
append->setLayout(lay);
LOG_OBJ.addAppender(append);
}
/** useDailyRollingFile
* fullname: eplog::stdlog::useDailyRollingFile
* access: public
* @brief
*
* @param const char * szFileName
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 15:20
*/
void stdlog::useDailyRollingFile( const char* szFileName /*= "d:\\mylog.log"*/ )
{
// m_append = DailyRollingFile;
LOG_OBJ.removeAllAppenders();
SharedAppenderPtr append(
new DailyRollingFileAppender(szFileName));
append->setName("mydrflog");
string pattern = MYLOG_PATTERN;
auto_ptr
append->setLayout(lay);
LOG_OBJ.addAppender(append);
}
/** operator<<
* fullname: eplog::stdlog::operator<<
* access: public
* @brief
*
* @param std::string & msg
* @return stdlog& 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 15:56
*/
stdlog& stdlog::operator<<( std::string &msg )
{
return operator<<(msg.c_str());
}
/** operator()
* fullname: eplog::stdlog::operator()
* access: public
* @brief 用重载()模仿构造函数的用法
*
* @param StdLogAppend app
* @param const char * szFileName
* @return void 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/19 16:22
*/
stdlog& stdlog::operator()( StdLogAppend app /*= Console*/,
const char* szFileName /*= "d:\\mylog.log"*/ )
{
m_append = app;
switch(app)
{
case RollingFile:
{
useRollingFile(szFileName);
break;
}
case DailyRollingFile:
{
useDailyRollingFile(szFileName);
break;
}
case Console:
default:
{
useConsole();
break;
}
}
return *this;
}
/** operator>>
* fullname: eplog::stdlog::operator>>
* access: public
* @brief
*
* @param StdLogAppend app
* @return stdlog& 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/20 10:07
*/
stdlog& stdlog::operator>>( StdLogAppend app )
{
m_append = app;
if (app == Console)
{
useConsole();
}
return *this;
}
/** operator>>
* fullname: eplog::stdlog::operator>>
* access: public
* @brief
*
* @param const char * szFileName
* @return stdlog& 函数执行情况
* @retval 0代表成功,非0代表失败
* @author chrono
* @since 2009/10/20 10:10
*/
stdlog& stdlog::operator>>( const char* szFileName )
{
if (m_append == Console)
{
return *this;
}
return operator()(m_append, szFileName);
}
from:http://blog.163.com/chrono_law/blog/static/101814084200911402438726/
chinaunix网友2010-11-09 16:27:49
很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com