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 是自己写的日志库。
- Linux c日志打印
- C++ 打印日志方法
- c语言 DEBUG 日志打印
- shell脚本打印日志方法
- shell脚本打印日志方法
- shell脚本打印日志方法
- Android学习第三篇;在java,c++,c中用日志方法打印信息
- 【C#】写日志方法
- Object-C使用NSLog打印日志
- Object C 利用NSLog 打印日志
- C语言简单的日志宏打印
- C/C++ 打印信息控制,包括日志
- Android C程序打印logcat日志
- c 宏定义log日志简单打印
- 用C语言打印日志(Log)
- 打印日志开关的定义方法
- iOS NSLog 打印日志->方法集合
- JNI 在native方法中打印日志
- Error:Execution failed for task ':app:clean'
- web前端CSS2学习2017.6.17
- hadoop命令
- CNN结构:用于检测的CNN结构进化-一站式方法
- mysql 下 计算 两点 经纬度 之间的距离 含具体sql语句以及伪列作为查询条件实现
- C++ 打印日志方法
- Hibernate入门实例
- PHP学习笔记4-php的MYSQL数据库访问
- 利用Arrays.sort();方法对相应数组进行升序、降序排列
- 微信授权登录
- 关于flask表单验证json数据不通过总是返回False的解决方法(flask wtforms: Validation always false)
- CI框架代码运行最详细的流程 v3.1
- 实现一个轮播控件并封装起来
- 2017.08.18 python第14次笔记