C++ 打印日志方法
2017-08-18 15:24
316 查看
//首先写一个头文件
////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 是自己写的日志库。
////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 是自己写的日志库。
相关文章推荐
- c c++ 函数入口和出口的hook(gcc 编译选项),然后打印出函数调用关系的方法
- 在C++中打印出变量的方法
- C++ log4cxx日志打印配置文件详解
- rest-assured 将log()中的信息打印到log日志中去的方法
- c++训练题(求某个数的最大质数因子)兼自己写的一个打印一定范围内质数方法
- 网页中显示hyper-v,调用IMsRdpClient8时打印OnDisconnected方法的错误日志的代码
- C++软件添加dump调试打印日志(推荐)
- 在C++中打印出变量的方法
- Android 日志打印工具类 可显示打印所在的方法和行号
- SpringBoot 让 MyBatis 打印日志的方法
- onvif server端在海思板子上不打印自定义日志问题的解决方法
- C++ 打印输出日志文件
- voltDB打印出C++中调用某个方法的trace
- C++基础星形打印方法
- 关于打印日志几个方法的列举
- c和C++的调试打印输出方法
- shell脚本打印日志方法
- C/C++ 打印文件名、行号、函数名的方法
- C/C++ 打印文件名、行号、函数名的方法 .
- C++中关于写日志文件Log的方法