#include "stdlog.h"
#include <log4cplus/logger.h>
#include <log4cplus/consoleappender.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/layout.h>
#include <cstdio>
#include <cstdarg>
#include <vector>
#include <sstream>
// #include <iostream>
using namespace std;
using namespace eplog;
using namespace log4cplus;
#define MYLOG_PATTERN "{%D} {%-5p} {%t} - %m %n"
#define LOG_OBJ Logger::getRoot()
stdlog& logger = stdlog::instance();
/** 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<char> buffer;
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<Layout> lay(new PatternLayout(pattern));
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<Layout> lay(new PatternLayout(pattern));
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<Layout> lay(new PatternLayout(pattern));
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);
}