头文件:
#ifndef LOG4CPP
#define LOG4CPP
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/PatternLayout.hh>
namespace niiwoo
{
namespace util
{
namespace log
{
enum LOG_LEVEL
{
LOG_LEVEL_NONE = 0, // 不打日志
LOG_LEVEL_ERROR = 1,
LOG_LEVEL_WARN = 2,
LOG_LEVEL_INFO = 3,
LOG_LEVEL_DEBUG = 4,
LOG_LEVEL_MAX = 5
};
enum ERR_CODE
{
ERR_CODE_OK = 0,
ERR_CODE_PATH = -1,
OTHER_ERR = -2
};
// 调用NiiwooLog* GetInstance()->Init() 初始化之后,即可使用以下宏打印日志
// DEBUG级别日志会同时打印到屏幕一份
#define LOG4_DEBUG(...) NiiwooLog::GetInstance()->Log(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define LOG4_WARN(...) NiiwooLog::GetInstance()->Log(LOG_LEVEL_WARN, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define LOG4_INFO(...) NiiwooLog::GetInstance()->Log(LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define LOG4_ERROR(...) NiiwooLog::GetInstance()->Log(LOG_LEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
class NiiwooLog
{
public:
ERR_CODE Init(std::string strLogPath, std::string strModName, LOG_LEVEL lev, long iLogFileSize, long iLogPathSize);//ilogFileSize:size of per log file:Byte,iLogPathSize:Maxsize of LogPath:Byte
private:
NiiwooLog();
virtual ~NiiwooLog();
public:
<span style="white-space:pre"> </span>static NiiwooLog* GetInstance();
<span style="white-space:pre"> </span>int Log(LOG_LEVEL lev, char const *file, int line, char const *func, char const *format, ...);
<span style="white-space:pre"> </span>int SetFileName(std::string& FileName, std::string strModName);
<span style="white-space:pre"> </span>ERR_CODE CheckPath(std::string &strLogPath);
<span style="white-space:pre"> </span>int SetLev(LOG_LEVEL lev);
<span style="white-space:pre"> </span>int RebuildCurLog();
<span style="white-space:pre"> </span>void LogClearTimer(std::string strLogPath, long lPathSize);
<span style="white-space:pre"> </span>private:
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>LOG_LEVEL m_lev;
<span style="white-space:pre"> </span>log4cpp::Category *m_pTotal;
<span style="white-