C/C++格式化日志庫實(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"
/*
*最終生成的日志字符串最大長度,要特別注意
*超過此長度程序會(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語言
下一篇:C++中rapidjson組裝map和數(shù)組array的代碼示例
本文標(biāo)題:C/C++格式化日志庫實(shí)現(xiàn)代碼
本文地址:http://m.jygsgssxh.com/a1/Cyuyan/359.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


