其他分享
首页 > 其他分享> > 打印LOG信息到串口和输出到文件的函数

打印LOG信息到串口和输出到文件的函数

作者:互联网

BOOL WriteLog(const char *str, ...)
{
 char *szLogFileName = (char *)malloc(MAX_PATH*sizeof(char));
 TCHAR *wzLogFileName = (TCHAR *)malloc(MAX_PATH*sizeof(TCHAR));
 if(!wzLogFileName || !szLogFileName)
 {
  if(szLogFileName) free(szLogFileName);
  if(wzLogFileName) free(wzLogFileName);
 }
 else
 {
  memset(wzLogFileName,0,MAX_PATH*sizeof(TCHAR));
  GetModuleFileName(NULL,wzLogFileName,MAX_PATH);
  _tcsrchr(wzLogFileName,'.')[1]='\0';
  _tcscat(wzLogFileName,TEXT("log"));
  wcstombs(szLogFileName,wzLogFileName,MAX_PATH);
  free(wzLogFileName);
  FILE *fp=fopen(szLogFileName,"a");
  free(szLogFileName);
  if(fp!=NULL)
  {
   va_list va;
   va_start(va, str);
   vprintf(str, va);
   vfprintf(fp, str, va);
   va_end(va);
   fclose(fp);
   return TRUE;
  }
 }
 return FALSE;
}

 

标签:va,szLogFileName,MAX,打印,TCHAR,PATH,串口,wzLogFileName,LOG
来源: https://blog.51cto.com/u_15298588/3034263