打印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;
}

 

上一篇:C语言 vprintf 函数和 printf 函数区别 - C语言零基础入门教程


下一篇:C语言 exit 函数 - C语言零基础入门教程