雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C/C++格式化日志庫實(shí)現(xiàn)代碼

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊:

頭文件如下:

/*****************************************************/
/* 跨平臺(tái)日志函數(shù),Linux下與windows下親測(cè)有效      */
/*****************************************************/
 
 
#ifndef _LOG_FORMAT_H_
#define _LOG_FORMAT_H_
 
// 日志等級(jí)
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, ...);
 
/******************************************************/
/* 日志函數(shù)調(diào)用方法  _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"
 
/* 
*最終生成的日志字符串最大長度,要特別注意
*超過此長度程序會(huì)崩潰,用戶可以自定義長度
*/
#define LOG_MAX 1024
 
/*
*時(shí)間長度,不需要修改
*/
#define FORMAT_TIME_SIZE 24
 
/*
*日志等級(jí)字符串,與頭文件中定義的枚舉必須一致
*/
static std::string g_LogLevel[] = { "TRACE","INFO","WARN","ERROR","FATAL" };
 
/*
*內(nèi)存申請(qǐng)函數(shù),用戶可以自定義
*/
static char *NewBuf(const uint32_t unBufSize)
{
	return new char[unBufSize];
}
 
/*
*內(nèi)存釋放函數(shù),必須與NewBuf對(duì)應(yīng)
*/
static void DeleteBuf(char ** pszBuf)
{
	if (NULL != *pszBuf)
	{
		delete[] *pszBuf;
		*pszBuf = NULL;
	}
	*pszBuf = NULL;
}
 
/*
*獲取當(dāng)前時(shí)間字符串函數(shù)
*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;
	}
 
	// 組裝日志,[時(shí)間][等級(jí)][文件名(行數(shù))][自定義字符串]
	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
 
	// 日志默認(rèn)輸出到控制臺(tái),用戶可以自行修改
	printf("%s\n", pszLog);
 
	DeleteBuf(&pszDescribeInfo);
	DeleteBuf(&pszLog);
	DeleteBuf(&pszTime);
}

好了這篇文章就介紹到這,需要的朋友可以參考一下。 

上一篇:C++類中變量也可以是引用的代碼實(shí)例

欄    目:C語言

下一篇:C++中rapidjson組裝map和數(shù)組array的代碼示例

本文標(biāo)題:C/C++格式化日志庫實(shí)現(xiàn)代碼

本文地址:http://m.jygsgssxh.com/a1/Cyuyan/359.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有