C/C++格式化日志庫實(shí)現(xiàn)代碼
更新時間:2019年04月07日 21:31:58 作者:葉紫真
這篇文章主要介紹了C/C++格式化日志庫實(shí)現(xiàn)代碼,需要的朋友可以參考下
頭文件如下:
/*****************************************************/
/* 跨平臺日志函數(shù),Linux下與windows下親測有效 */
/*****************************************************/
#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, ...);
/******************************************************/
/* 日志函數(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"
/*
*最終生成的日志字符串最大長度,要特別注意
*超過此長度程序會崩潰,用戶可以自定義長度
*/
#define LOG_MAX 1024
/*
*時間長度,不需要修改
*/
#define FORMAT_TIME_SIZE 24
/*
*日志等級字符串,與頭文件中定義的枚舉必須一致
*/
static std::string g_LogLevel[] = { "TRACE","INFO","WARN","ERROR","FATAL" };
/*
*內(nèi)存申請函數(shù),用戶可以自定義
*/
static char *NewBuf(const uint32_t unBufSize)
{
return new char[unBufSize];
}
/*
*內(nèi)存釋放函數(shù),必須與NewBuf對應(yīng)
*/
static void DeleteBuf(char ** pszBuf)
{
if (NULL != *pszBuf)
{
delete[] *pszBuf;
*pszBuf = NULL;
}
*pszBuf = NULL;
}
/*
*獲取當(dāng)前時間字符串函數(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ù))][自定義字符串]
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)輸出到控制臺,用戶可以自行修改
printf("%s\n", pszLog);
DeleteBuf(&pszDescribeInfo);
DeleteBuf(&pszLog);
DeleteBuf(&pszTime);
}
好了這篇文章就介紹到這,需要的朋友可以參考一下。
相關(guān)文章
C++關(guān)于類結(jié)構(gòu)體大小和構(gòu)造順序,析構(gòu)順序的測試詳解
這篇文章主要介紹了C++類結(jié)構(gòu)體大小和構(gòu)造順序,析構(gòu)順序的測試,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
C++實(shí)現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C/C++實(shí)現(xiàn)圖形學(xué)掃描線填充算法
這篇文章主要介紹了C/C++實(shí)現(xiàn)圖形學(xué)掃描線填充算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04
C/C++哈希表優(yōu)化LeetCode題解997找到小鎮(zhèn)的法官
這篇文章主要為大家介紹了C/C++哈希表優(yōu)化題解997找到小鎮(zhèn)的法官示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

