logOper2.h
#pragma once
#include "LogOper.h"
class CLogOperEx :public CLogOper
{
public:
CLogOperEx();
~CLogOperEx();
public:
//日志记录:DEBUG等级
void LogDebug(const char* pszStringLog, ...);
void LogInfo(const char* pszStringLog, ...);
void LogWarn(const char* pszStringLog, ...);
void LogError(const char* pszStringLog, ...);
void LogFatal(const char* pszStringLog, ...);
};
logOper2.cpp
#include "LogOperEx.h"
#include "log4cpp/Category.hh"
#include "log4cpp/PropertyConfigurator.hh"
#include "log4cpp/PatternLayout.hh"
#include "log4cpp/DailyRollingFileAppender.hh"
#include "log4cpp/Priority.hh"
#define LOG_OPER_MAX_SIZE 204800
CLogOperEx::CLogOperEx()
{
}
CLogOperEx::~CLogOperEx()
{
}
void CLogOperEx::LogDebug(const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->debug(szTmpBuf);
}
}
void CLogOperEx::LogInfo(const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->info(szTmpBuf);
}
}
void CLogOperEx::LogWarn(const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->warn(szTmpBuf);
}
}
void CLogOperEx::LogError(const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->error(szTmpBuf);
}
}
void CLogOperEx::LogFatal(const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->fatal(szTmpBuf);
}
}
logOper.h
#pragma once
class CLogOper
{
public:
CLogOper();
~CLogOper();
public:
void GetLogFileName(const char* pszLogFileDir, const char* pszDeviceCode,
int iFileNum, char* pszLogFileName);//获得日志的文件名
bool SetConfigureFile(const char* pszConfigureFileName,
const char* pszInstanceName, const char* pszLogFileName,
int iMaxFileSize = 10485760, int iMaxBackupIndex = 10,
const char* pszPriority = "DEBUG");//设置日志配置文件
bool InitLogInstance(const char* pszConfigureFileName,
const char* pszInstanceName);//初始化日志实例
//日志记录:DEBUG等级
void LogDebug(const char* pszStringLog, ...);
void LogDebug(const char* pszLogCode, const char* pszLogSubCode,
const char* pszStringLog, ...);
void LogDebug(const int iLogCode, const int iLogSubCode,
const char* pszStringLog, ...);
//日志记录:INFO等级
void LogInfo(const char* pszStringLog, ...);
void LogInfo(const char* pszLogCode, const char* pszLogSubCode,
const char* pszStringLog, ...);
void LogInfo(const int iLogCode, const int iLogSubCode,
const char* pszStringLog, ...);
//日志记录:WARN等级
void LogWarn(const char* pszStringLog, ...);
void LogWarn(const char* pszLogCode, const char* pszLogSubCode,
const char* pszStringLog, ...);
void LogWarn(const int iLogCode, const int iLogSubCode,
const char* pszStringLog, ...);
//日志记录:ERROR等级
void LogError(const char* pszStringLog, ...);
void LogError(const char* pszLogCode, const char* pszLogSubCode,
const char* pszStringLog, ...);
void LogError(const int iLogCode, const int iLogSubCode,
const char* pszStringLog, ...);
//日志记录:FATAL等级
void LogFatal(const char* pszStringLog, ...);
void LogFatal(const char* pszLogCode, const char* pszLogSubCode,
const char* pszStringLog, ...);
void LogFatal(const int iLogCode, const int iLogSubCode,
const char* pszStringLog, ...);
protected:
void* m_hCategory; //生成实例的句柄
};
源文件
#include "LogOper.h"
#include "log4cpp/Category.hh"
#include "log4cpp/PropertyConfigurator.hh"
#include "log4cpp/PatternLayout.hh"
#include "log4cpp/DailyRollingFileAppender.hh"
#include "log4cpp/Priority.hh"
#define LOG_OPER_MAX_SIZE 204800
CLogOper::CLogOper()
{
m_hCategory = NULL;
}
CLogOper::~CLogOper()
{
}
//获得日志的文件名
void CLogOper::GetLogFileName(const char* pszLogFileDir,
const char* pszDeviceCode, int iFileNum, char* pszLogFileName) {
// TODO Auto-generated destructor stub
}
//设置日志配置文件
bool CLogOper::SetConfigureFile(const char* pszConfigureFileName,
const char* pszInstanceName, const char* pszLogFileName,
int iMaxFileSize, int iMaxBackupIndex, const char* pszPriority) {
if (m_hCategory)
{
delete (log4cpp::Category*)m_hCategory;
m_hCategory = NULL;
}
bool bCfgExists = true;
// 1 读取解析配置文件
// 读取出错, 完全可以忽略,可以定义一个缺省策略或者使用系统缺省策略
// BasicLayout输出所有优先级日志到ConsoleAppender
try
{
log4cpp::PropertyConfigurator::configure(pszConfigureFileName);
}
catch (log4cpp::ConfigureFailure& f)
{
std::cout << "Configure Problem " << f.what() << std::endl;
bCfgExists = false;
}
bool bCreateNewCategory = false;
if (bCfgExists)
{
m_hCategory = log4cpp::Category::exists(std::string(pszInstanceName));
if (m_hCategory == NULL)
{
bCreateNewCategory = true;
}
}
else
{
bCreateNewCategory = true;
}
if (bCreateNewCategory)
{
log4cpp::PatternLayout* pLayout1 = new log4cpp::PatternLayout();
pLayout1->setConversionPattern("$%5p %d{%Y-%m-%d %H:%M:%S.%l}@%m%n");
//log4cpp::RollingFileAppender* rollfileAppender = new log4cpp::RollingFileAppender("RollingFileAppender",
// pszLogFileName, iMaxFileSize, iMaxBackupIndex);
log4cpp::DailyRollingFileAppender * rollfileAppender = new log4cpp::DailyRollingFileAppender("RollingFileAppender",
pszLogFileName, iMaxFileSize, iMaxBackupIndex);
rollfileAppender->setLayout(pLayout1);
m_hCategory = &(log4cpp::Category::getInstance(std::string(pszInstanceName)));
((log4cpp::Category*) m_hCategory)->addAppender(rollfileAppender);
((log4cpp::Category*) m_hCategory)->setAdditivity(false);
try
{
log4cpp::Priority::Value valPriority = log4cpp::Priority::getPriorityValue(pszPriority);
((log4cpp::Category*) m_hCategory)->setPriority(valPriority);
}
catch (...)
{
std::cout << "Unknown Priority \" " << pszPriority << "\""
<< std::endl;
}
}
//m_hCategory = log4cpp::Category::exists(std::string(pszInstanceName));
if (m_hCategory == NULL)
return false;
return true;
}
//初始化日志实例
bool CLogOper::InitLogInstance(const char* pszConfigureFileName,const char* pszInstanceName)
{
if (m_hCategory == NULL)
{
// 1 读取解析配置文件
// 读取出错, 完全可以忽略,可以定义一个缺省策略或者使用系统缺省策略
// BasicLayout输出所有优先级日志到ConsoleAppender
try
{
log4cpp::PropertyConfigurator::configure(pszConfigureFileName);
}
catch (log4cpp::ConfigureFailure& f)
{
std::cout << "Configure Problem " << f.what() << std::endl;
return false;
}
m_hCategory = log4cpp::Category::exists(std::string(pszInstanceName));
if (m_hCategory == NULL)
return false;
}
return true;
}
void CLogOper::LogDebug(const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->debug(szTmpBuf);
}
}
void CLogOper::LogDebug(const char* pszLogCode, const char* pszLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset
= sprintf(szTmpBuf + offset, "%s@%s@", pszLogCode,
pszLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->debug(szTmpBuf);
}
}
void CLogOper::LogDebug(const int iLogCode, const int iLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory) {
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset
= sprintf(szTmpBuf + offset, "%08d@%04d@", iLogCode,
iLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->debug(szTmpBuf);
}
}
void CLogOper::LogInfo(const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->info(szTmpBuf);
}
}
void CLogOper::LogInfo(const char* pszLogCode, const char* pszLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset
= sprintf(szTmpBuf + offset, "%s@%s@", pszLogCode,
pszLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->info(szTmpBuf);
}
}
void CLogOper::LogInfo(const int iLogCode, const int iLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset
= sprintf(szTmpBuf + offset, "%08d@%04d@", iLogCode,
iLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->info(szTmpBuf);
}
}
void CLogOper::LogWarn(const char* pszStringLog, ...)
{
if (m_hCategory) {
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->warn(szTmpBuf);
}
}
void CLogOper::LogWarn(const char* pszLogCode, const char* pszLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset
= sprintf(szTmpBuf + offset, "%s@%s@", pszLogCode,
pszLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->warn(szTmpBuf);
}
}
void CLogOper::LogWarn(const int iLogCode, const int iLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset
= sprintf(szTmpBuf + offset, "%08d@%04d@", iLogCode,
iLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->warn(szTmpBuf);
}
}
void CLogOper::LogError(const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->error(szTmpBuf);
}
}
void CLogOper::LogError(const char* pszLogCode, const char* pszLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset
= sprintf(szTmpBuf + offset, "%s@%s@", pszLogCode,
pszLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->error(szTmpBuf);
}
}
void CLogOper::LogError(const int iLogCode, const int iLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory) {
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset
= sprintf(szTmpBuf + offset, "%08d@%04d@", iLogCode,
iLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->error(szTmpBuf);
}
}
void CLogOper::LogFatal(const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->fatal(szTmpBuf);
}
}
void CLogOper::LogFatal(const char* pszLogCode, const char* pszLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset = sprintf(szTmpBuf + offset, "%s@%s@", pszLogCode,pszLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->fatal(szTmpBuf);
}
}
void CLogOper::LogFatal(const int iLogCode, const int iLogSubCode,const char* pszStringLog, ...)
{
if (m_hCategory)
{
char szTmpBuf[LOG_OPER_MAX_SIZE] = { 0 };
int offset = 0;
offset = sprintf(szTmpBuf + offset, "%08d@%04d@", iLogCode,iLogSubCode);
va_list va;
va_start(va, pszStringLog);
vsprintf(szTmpBuf + offset, pszStringLog, va);
va_end(va);
((log4cpp::Category*) m_hCategory)->fatal(szTmpBuf);
}
}