写日志的函数,直接复制使用

来源:互联网 发布:论文格式 知乎 编辑:程序博客网 时间:2024/05/20 16:13

为了方便调试,直接复制下面函数就可以使用日志打印功能

void PrintDebugLog(CString pValue, CString pEvent="")
{
char V[100];
char E[100];
char szFilename[1024];
CString strPath = CINIFile::GetAppPath() +"\\TempDebugLog\\";
CreateDirectory("TempDebugLog",NULL);

SHCreateDirectoryEx(NULL, strPath, NULL);


CTime time = CTime::GetCurrentTime();


wsprintf(szFilename, "%s%d%d%d%d-log.txt",
strPath , time.GetYear(), time.GetMonth(), time.GetDay(),time.GetHour());
FILE *fp = fopen( szFilename, "a" );
if (NULL == fp)
{
return;
}


//插入时间
char cTime[30];
::memset(cTime,0,sizeof(cTime));
sprintf(cTime,"[%d:%d:%d] ",time.GetHour(),time.GetMinute(),time.GetSecond());
fprintf(fp,cTime);
if (pEvent.GetLength() > 0)
{
sprintf(E,"事件:%s;",pEvent);
fprintf(fp, E);
}


sprintf(V,"值:%s",pValue);
va_list arg;
va_start( arg, V );
vfprintf(fp, V, arg );
fprintf(fp,"\n");
fclose(fp);

}


第二种方法可进行参数格式化:

void DebugPrintf(const char *p, ...)
{
char szFilename[256];
CString strPath = CINIFile::GetAppPath() +"\\TempDebugLog\\";
CreateDirectory("TempDebugLog",NULL);

CTime time = CTime::GetCurrentTime();


sprintf(szFilename,"%s%d%d%d_log.txt",strPath ,time.GetYear(), time.GetMonth(), time.GetDay());
FILE *fp = fopen( szFilename, "a" );
if (NULL == fp)
{
return;
}


//插入时间
char cTime[30];
::memset(cTime,0,sizeof(cTime));
sprintf(cTime,"[%d:%d:%d] ",time.GetHour(),time.GetMinute(),time.GetSecond());
fprintf(fp,cTime);


va_list arg;
va_start( arg, p );
vfprintf(fp, p, arg );
fprintf(fp,"\n");


fclose(fp);
}

使用格式:

int i = 0;

DebugPrintf("参数【%d】,参数【%d】,参数【%d】",i,++i,++i);


0 0
原创粉丝点击