1 void log_file( const int pid,const char * str_fmt,...) 2 { 3 FILE *fp; 4 5 fp = fopen("myfile.txt", "a"); 6 7 if(fp) 8 { 9 static char log_trace[256]; 10 va_list ap; 11 struct tm *tm; 12 time_t t; 13 14 va_start(ap, str_fmt); 15 vsnprintf(log_trace, sizeof(log_trace), str_fmt, ap); 16 va_end(ap); 17 18 t = time(NULL); 19 tm = localtime(&t); 20 fprintf(fp, "[%02d/%02d/%02d/ %02d:%02d:%02d]: %s pid:%d\n", 21 tm->tm_year, 22 tm->tm_mon, 23 tm->tm_mday, 24 tm->tm_hour, 25 tm->tm_min, 26 tm->tm_sec, 27 log_trace,pid); 28 fclose(fp); 29 } 30 31 32 }
比较常用的一种,还有其它的方式~后面再补充