标题:如何创建简单的日志文件
标题:如何创建简单的日志文件
问1:为何要创建日志文件:
答1:在项目代码中,常常需要查看日志文件的信息,来调试CODE
问2:怎么创建一个简单的日志文件
答2:需要如下步骤
-
FILE *DFile; // open log file and print time stamp DFile = fopen("log.txt","a+"); if(DFile == NULL) { printf("ERROR, Could not open LOT.TXT log file!\n"); exit(1); }
-
//-------------------------------------------------------------------------- // Debug printf to opened file DFile with a force flush // // Return: number of characters printed // int dprintf(char *format, ...) { int rt; va_list ap; if (DFile != NULL) { va_start(ap, format); rt = vfprintf(DFile,format,ap); va_end(ap); fflush(DFile); } va_start(ap, format); rt = vfprintf(stdout,format,ap); va_end(ap); return rt; }
-
<pre name="code" class="cpp"> dprintf("hello,world!\n");
问3:如何创建一个更好日志文件,包括时间,函数名,哪一个文件,具体哪一行
答3:详情见下篇