C++ 打印日志方法

来源:互联网 发布:淘宝五金零配件 编辑:程序博客网 时间:2024/06/06 00:42

首先写一个头文件 

////CommonFun.h

#pragma once

#include <assert.h>
//log等级定义 
#define  LOG_DEBUG_LEVEL 4
#define  LOG_APP_LEVEL 3
#define  LOG_WARN_LEVEL 2
#define  LOG_ERROR_LEVEL 1


#define RETURN_OK0
#define  RETURN_ERROR -1


#define LOG_TRACE(level,Action,szformat,...) \
FormatTrace(level,Action,__FILE__,__LINE__,__FUNCTION__,szformat,__VA_ARGS__)


#define G_LOG_TRACE(level,Action,szformat,...) \
G_FormatTrace(level,Action,__FILE__,__LINE__,__FUNCTION__,szformat,__VA_ARGS__)


void G_FormatTrace(int nlevel,const char* szAction,const char* argPath,long argLine, const char* szFunction, const char* szFormat, ...);


#if (defined(WIN32)) 
void InitMinDump();
#endif


#define MACRO_PRINTF(szformat,...) \
Macro_Printf(szformat,__VA_ARGS__)


void Macro_Printf(const char* szFormat, ...);


#ifdef _DEBUG
#define  assert ASSERT

#endif


写一个DataSet类

FormatTrace()函数写法:

void CDataSet::FormatTrace(int nlevel,const char* szAction,const char* argPath,long argLine, const char* szFunction, const char* szFormat, ...)
{
char szHead[256] = {0};
char szParam[1024] = {0};
char szLeve[10] = {0};
switch (nlevel)
{
case LOG_APP_LEVEL:
strcpy(szLeve,"<app>");
break;
case LOG_DEBUG_LEVEL:
strcpy(szLeve,"<dbg>");
break;
case LOG_WARN_LEVEL:
strcpy(szLeve,"<war>");
break;
case LOG_ERROR_LEVEL:
strcpy(szLeve,"<err>");
break;
}
va_list args;
va_start(args,szFormat);
_vsnprintf_s(szParam, 1000, szFormat, args);
va_end(args);


#ifdef _PER_DEBUG


std::string strFile;
const char* pPot=strrchr(argPath,'\\')+1;
if (pPot==NULL)
{
strFile.append(argPath);
}
else
{
strFile.append(pPot);
}
strFile.append(":");
char szLine[16]={0};
sprintf_s(szLine,"%d",argLine);
strFile.append(szLine);


const char *pMethod =NULL;
if(strrchr(szFunction, ':') == NULL)
{
pMethod=szFunction;
}
else
{
pMethod = strrchr(szFunction, ':')+1;
}
memset(szHead,0,sizeof(szHead));
sprintf_s(szHead,"%s[%30s][%30s]",szLeve,strFile.c_str(),pMethod);


#if (defined(_DEBUG) && defined(WIN32))
char szTmpOut[1024]={0};
sprintf_s(szTmpOut,"[--------]%s %s \n",szHead,szParam);
OutputDebugString(szTmpOut);
#endif


#else
memset(szHead,0,sizeof(szHead));


sprintf_s(szHead,"%s[%12s][%12s]", szLeve,"DATASET", szAction);
#endif
m_pLog->Trace(nlevel,"%s %s",szHead,szParam);


}

G_FormatTrace()函数写法:

 void G_FormatTrace(int nlevel,const char* szAction,const char* argPath,long argLine, const char* szFunction, const char* szFormat, ...)
{
char szHead[256] = {0};
char szParam[1024] = {0};
char szLeve[10] = {0};
switch (nlevel)
{
case LOG_APP_LEVEL:
strcpy(szLeve,"<app>");
break;
case LOG_DEBUG_LEVEL:
strcpy(szLeve,"<dbg>");
break;
case LOG_WARN_LEVEL:
strcpy(szLeve,"<war>");
break;
case LOG_ERROR_LEVEL:
strcpy(szLeve,"<err>");
break;
}
va_list args;
va_start(args,szFormat);
_vsnprintf_s(szParam, 1000, szFormat, args);
va_end(args);


#ifdef _PER_DEBUG


std::string strFile;
const char* pPot=strrchr(argPath,'\\')+1;
if (pPot==NULL)
{
strFile.append(argPath);
}
else
{
strFile.append(pPot);
}
strFile.append(":");
char szLine[16]={0};
sprintf_s(szLine,"%d",argLine);
strFile.append(szLine);


const char *pMethod =NULL;
if(strrchr(szFunction, ':') == NULL)
{
pMethod=szFunction;
}
else
{
pMethod = strrchr(szFunction, ':')+1;
}
memset(szHead,0,sizeof(szHead));
sprintf_s(szHead,"%s[%30s][%30s]",szLeve,strFile.c_str(),pMethod);


#if (defined(_DEBUG) && defined(WIN32))
char szTmpOut[1024]={0};
sprintf_s(szTmpOut,"[--------]%s %s \n",szHead,szParam);
OutputDebugString(szTmpOut);
#endif


#else
memset(szHead,0,sizeof(szHead));


sprintf_s(szHead,"%s[%12s][%12s]", szLeve," ", szAction);
#endif
g_pLog->Trace(nlevel,"%s %s",szHead,szParam);


}


Macro_Printf()函数写法:

void Macro_Printf(const char* szFormat, ...)
{
char szParam[1024] = {0};


va_list args;
va_start(args,szFormat);
_vsnprintf_s(szParam, 1000, szFormat, args);
va_end(args);
SYSTEMTIME stLocal;  
::GetLocalTime(&stLocal); 
printf("[%02u:%02u:%02u]%s\r\n",stLocal.wHour,stLocal.wMinute,stLocal.wSecond,szParam);
}

其中 m_pLog 是自己写的日志库。