温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在C/C++中格式化日志库

发布时间:2021-05-31 17:52:57 来源:亿速云 阅读:155 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关怎么在C/C++中格式化日志库,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

头文件如下:

 
 
#ifndef _LOG_FORMAT_H_
#define _LOG_FORMAT_H_
 
// 日志等级
enum LogLevel { _LOG_TRACE, _LOG_INFO, _LOG_WARN, _LOG_ERROR, _LOG_FATAL };
void LogFormat(const char *pszFileName, const int nLine, LogLevel Level, const char *Format, ...);
 
/******************************************************/
/* 日志函数调用方法  _LOG(_LOG_FATAL, "%s", "sss"); */
/******************************************************/
#ifndef _LOG
#define _LOG(Level, Format, ...) LogFormat(__FILE__, __LINE__, Level, Format, ##__VA_ARGS__)
#endif
 
#endif // _LOG_FORMAT_H_

 源文件如下:

#include <string>
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
 
#if defined __GNUC__ || defined LINUX
#include <sys/time.h>
#define MY_VA_LIST _G_va_list
#else
#include <windows.h>
#define MY_VA_LIST va_list
#endif
 
#include "LogFormat.h"
 
/* 
*最终生成的日志字符串最大长度,要特别注意
*超过此长度程序会崩溃,用户可以自定义长度
*/
#define LOG_MAX 1024
 
/*
*时间长度,不需要修改
*/
#define FORMAT_TIME_SIZE 24
 
/*
*日志等级字符串,与头文件中定义的枚举必须一致
*/
static std::string g_LogLevel[] = { "TRACE","INFO","WARN","ERROR","FATAL" };
 
/*
*内存申请函数,用户可以自定义
*/
static char *NewBuf(const uint32_t unBufSize)
{
	return new char[unBufSize];
}
 
/*
*内存释放函数,必须与NewBuf对应
*/
static void DeleteBuf(char ** pszBuf)
{
	if (NULL != *pszBuf)
	{
		delete[] *pszBuf;
		*pszBuf = NULL;
	}
	*pszBuf = NULL;
}
 
/*
*获取当前时间字符串函数
*2018-08-08 08:08:08.888
*/
static char* GetTime()
{
	char *pszFormatTime = NewBuf(FORMAT_TIME_SIZE);
#if defined __GNUC__ || defined LINUX
	struct timeval tv;
	struct timezone tz;
	gettimeofday(&tv, &tz);
	struct tm *current_time = localtime(&tv.tv_sec);
	sprintf(pszFormatTime,
		"%04d-%02d-%02d %02d:%02d:%02d.%03d",
		current_time->tm_year + 1900,
		current_time->tm_mon + 1,
		current_time->tm_mday,
		current_time->tm_hour,
		current_time->tm_min,
		current_time->tm_sec,
		tv.tv_usec / 1000);
#else
	SYSTEMTIME sys_time;
	GetLocalTime(&sys_time);
#if _MSC_VER
	sprintf_s(pszFormatTime,
		FORMAT_TIME_SIZE,
		"%04d-%02d-%02d %02d:%02d:%02d.%03d",
		sys_time.wYear,
		sys_time.wMonth,
		sys_time.wDay,
		sys_time.wHour,
		sys_time.wMinute,
		sys_time.wSecond,
		sys_time.wMilliseconds);
#else
	sprintf(pszFormatTime,
		"%04d-%02d-%02d %02d:%02d:%02d.%03d",
		sys_time.wYear,
		sys_time.wMonth,
		sys_time.wDay,
		sys_time.wHour,
		sys_time.wMinute,
		sys_time.wSecond,
		sys_time.wMilliseconds);
#endif
#endif
	return pszFormatTime;
}
 
void LogFormat(const char *pszFileName, const int nLine, LogLevel Level, const char *Format, ...)
{
	int ret = 0;
	char *pszDescribeInfo = NewBuf(LOG_MAX);
 
	MY_VA_LIST ap;
	va_start(ap, Format);
#if defined __GNUC__ || defined LINUX
	ret = vsnprintf(pszDescribeInfo,
		LOG_MAX,
		Format,
		ap);
#else
#if _MSC_VER
	ret = _vsnprintf_s(pszDescribeInfo,
		LOG_MAX,
		_TRUNCATE,
		Format,
		ap);
#else
	ret = _vsnprintf(pszDescribeInfo,
		LOG_MAX,
		Format,
		ap);
#endif
#endif
	va_end(ap);
	if ((LOG_MAX <= ret) || (0 > ret))
	{
		DeleteBuf(&pszDescribeInfo);
		return;
	}
 
	// 组装日志,[时间][等级][文件名(行数)][自定义字符串]
	char *pszLog = NewBuf(LOG_MAX);
	char *pszTime = GetTime();
#if defined __GNUC__ || defined LINUX
	ret = sprintf(pszLog,
		"[%s][%s][%s(%d)][%s]",
		pszTime,
		g_LogLevel[Level].c_str(),
		pszFileName,
		nLine,
		pszDescribeInfo);
#else
#if _MSC_VER
	ret = sprintf_s(pszLog,
		LOG_MAX,
		"[%s][%s][%s(%d)][%s]",
		pszTime,
		g_LogLevel[Level].c_str(),
		pszFileName,
		nLine,
		pszDescribeInfo);
#else
	ret = sprintf(pszLog,
		"[%s][%s][%s(%d)][%s]",
		pszTime,
		g_LogLevel[Level].c_str(),
		pszFileName,
		nLine,
		pszDescribeInfo);
#endif
#endif
 
	// 日志默认输出到控制台,用户可以自行修改
	printf("%s\n", pszLog);
 
	DeleteBuf(&pszDescribeInfo);
	DeleteBuf(&pszLog);
	DeleteBuf(&pszTime);
}

上述就是小编为大家分享的怎么在C/C++中格式化日志库了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI