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

主要运行在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 最后文件中的信息

 

 

转载于:https://www.cnblogs.com/gjianw217/p/3510753.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值