C/C++格式化日志庫(kù)實(shí)現(xiàn)代碼
更新時(shí)間:2019年04月07日 21:31:58 作者:葉紫真
這篇文章主要介紹了C/C++格式化日志庫(kù)實(shí)現(xià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"
/*
*最終生成的日志字符串最大長(zhǎng)度,要特別注意
*超過(guò)此長(zhǎng)度程序會(huì)崩潰,用戶可以自定義長(zhǎng)度
*/
#define LOG_MAX 1024
/*
*時(shí)間長(zhǎng)度,不需要修改
*/
#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);
}
好了這篇文章就介紹到這,需要的朋友可以參考一下。
相關(guān)文章
C語(yǔ)言詳細(xì)分析講解struct與union使用方法
最近開(kāi)始自學(xué)C語(yǔ)言,從最基礎(chǔ)部分的開(kāi)始學(xué)起。今天看書的時(shí)候注意到了struct和union似乎很像,除了名字不同,看起來(lái)幾乎沒(méi)有區(qū)別。<BR>既然C中定義了struct和union兩個(gè)關(guān)鍵字,那么它們肯定是有區(qū)別的,在查了一些資料之后我來(lái)總結(jié)一下他們的使用2022-04-04
C++關(guān)于類結(jié)構(gòu)體大小和構(gòu)造順序,析構(gòu)順序的測(cè)試詳解
這篇文章主要介紹了C++類結(jié)構(gòu)體大小和構(gòu)造順序,析構(gòu)順序的測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
C++實(shí)現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C/C++實(shí)現(xiàn)圖形學(xué)掃描線填充算法
這篇文章主要介紹了C/C++實(shí)現(xiàn)圖形學(xué)掃描線填充算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C/C++哈希表優(yōu)化LeetCode題解997找到小鎮(zhèn)的法官
這篇文章主要為大家介紹了C/C++哈希表優(yōu)化題解997找到小鎮(zhèn)的法官示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

