程序调试过程中的日志文件源码

主要运行在VC6++环境下!

1 日志文件记录源代码:

 1 #include<stdio.h>
 2 #include<assert.h>
 3 #include<time.h>
 4 void my_log(char *strlog)
 5 {
 6     
 7     int ret;
 8     
 9     time_t t;
10     struct tm *tp;
11     char curTime[100] = {0};
12     
13     char filename[]="info.log";
14     char *fopentype = "a+";
15     FILE *fp = NULL;
16     fp = fopen(filename, fopentype);
17     if ( NULL == fp )
18     {
19         
20         printf("不能进行[%s]日志记录!\n", filename);
21         return;
22         
23     }
24     
25     assert(strlog!= NULL);
26     
27     // 获取当前系统时间
28     t = time(NULL);
29     tp = localtime(&t);
30     sprintf(curTime, "%2.2d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
31     
32     //存储日志
33     ret = fprintf(fp, "[%s] %s\n", curTime, strlog);//另起一行
34     if(ret >= 0)
35     {
36         fflush(fp);
37     }
38     
39     fclose(fp);
40     return;
41 }

 

2 测试主函数

 

1 int main()
2 {
3 
4     my_log("Save the log information for the first time ");
5     my_log("The second commit log information");
6     my_log("The third log information");    
7     return 0;
8 }

 

 

3 最后文件中的信息

 

 

posted on 2014-01-08 15:31  鹰之翔  阅读(228)  评论(0编辑  收藏  举报

导航