[C++][Logging] 项目中写日志模块的实现

一提到log,相信很多人就知道我要写些什么东西了,在所有的项目中都有自己一套写日志的模块。下面呢,我就根据平时的项目的需求写了一个比较简单使用的logger,方便输出各种等级的log。

事先声明一下,代码仅供大家学习参考之用,有什么不足的地方还请大家提出来。 :)在实际使用中可以考虑把log放入一个list,然后用一个线程专门负责写log.

下面logger的结构,有需要的源代码的,索取一下密码,谢谢!

 class Logger
{
public:
static void InitLoggerConfig(void);
static void StringLog(LogLevel logLevel, const string& strFuctionName, int lineNumber, string strFileName, const char* strInfo);
static void Log(LogLevel logLevel, const string& strFuctionName, int lineNumber, string strFileName, const char* strFormat, ...);
static void Log(LogLevel logLevel, StringBuilder& stringBuilder, const string& strFuctionName, int lineNumber, string strFileName);
static void RetureLog(LogLevel logLevel, const string& strFuctionName, int lineNumber, string strFileName, const char* strInfo);
static void LogPrinter(LogLevel logLevel, const char* strInfo, ...);

private:
static string GetFileMainPath();

private:
static string mStrLog;
static string mStrFileName;
static string mStrFileMainPath;
static time_t mTimeValue;
static LogLevel mLogLevel;
static tm mDateTime;
static LogManager mLogManager;

};


//测试代码
int main()
{
const char *pInfo = "strInfo";
const char *pWarringInfo = "Waring Log";
const char *pErrorInfo = "Error Log";
const char *pFatalInfo = "Fatal Log";
const char *pDebugInfo = "Debug Log";

INIT_LOG();

INFO_LOG(pInfo);
WARN_LOG(pWarringInfo);
ERR_LOG(pErrorInfo);
FATAL_LOG(pFatalInfo);
DEBUG_LOG(pDebugInfo);

INFO("%s", pInfo);
WARN("%s",pWarringInfo);
ERR (" %s",pErrorInfo);
FATAL(" %s",pFatalInfo);
DEBUG(" %s",pDebugInfo);

int i = 0;
XX_INFO_LOG( pInfo << &i);
XX_WARN_LOG( pWarringInfo << 1 );
XX_ERR_LOG( pErrorInfo << 2 );
XX_FATAL_LOG( pFatalInfo << 3 );
XX_DEBUG_LOG( pDebugInfo << 4 );

LOG_PRINTER(INFO,"%d %f %c %s", 1, 1.2, 'c', "123");
LOG_PRINTER(FATAL,"%d %f %c", 1, 1.2, 'c');
LOG_PRINTER(ERR,"%d %f %c", 1, 1.2, 'c');

return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值