C/C++포맷 로그 라 이브 러 리 구현 코드

헤더 파일 은 다음 과 같 습 니 다:

/*****************************************************/
/*        ,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, ...);
 
/******************************************************/
/*           _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
", pszLog); DeleteBuf(&pszDescribeInfo); DeleteBuf(&pszLog); DeleteBuf(&pszTime); }
자,이 글 은 여기까지 소개 하 겠 습 니 다.필요 한 친 구 는 참고 하 셔 도 됩 니 다.

좋은 웹페이지 즐겨찾기