forked from Skycrab/Linux-C-Web-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
74 lines (60 loc) · 1.26 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "parse.h"
#include "wrap.h"
FILE* logfp=NULL;
void initlog(const char* logp)
{
logfp=Fopen(logp,"a+");
}
static int getmonth(struct tm* local) // return month index ,eg. Oct->10
{
char buf[8];
int i;
static char *months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
strftime(buf,127,"%b",local);
for(i=0;i<12;++i)
{
if(!strcmp(buf,months[i]))
return i+1;
}
return 0;
}
void writetime()
{
time_t timeval;
char other[24];
char year[8];
char together[64];
int month;
(void)time(&timeval);
struct tm *local=localtime(&timeval);
/* get year */
strftime(year,7,"%Y",local);
/*get month */
month=getmonth(local);
/*get other */
strftime(other,23,"%d %H:%M:%S",local);
/*together all */
sprintf(together,"%s/%d/%s\r\n",year,month,other);
fwrite(together,strlen(together),1,logfp);
}
char* timeModify(time_t timeval,char *time)
{
char other[24];
char year[8];
int month;
struct tm *local=localtime(&timeval);
/* get year */
strftime(year,7,"%Y",local);
/*get month */
month=getmonth(local);
/*get other */
strftime(other,23,"%d %H:%M:%S",local);
/*together all */
sprintf(time,"%s/%d/%s\r\n",year,month,other);
return time;
}
void writelog(const char* buf)
{
fwrite(buf,strlen(buf),1,logfp);
fflush(logfp);
}