C++ 打印日志方法

//首先写一个头文件 
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 是自己写的日志库。


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值