-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.cpp
99 lines (81 loc) · 1.66 KB
/
log.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*Copyright (C) Guanyu@ntu
*Author:Guanyu
*Date:2014-5-25
*/
#include "log.h"
char level[][10] =
{
"info",
"debug",
"warn",
"error"
};
FILE* fplog[4] = {NULL};
static void getcurtime(char* buf, int len)
{
struct tm* newtime;
char* databuf = buf;
time_t nowtime;
time(&nowtime);
newtime=localtime(&nowtime);
strftime(databuf, len, "%g%m%d%H%M%S", newtime);
}
int creatlogfile()
{
int i;
char filename[FILE_NAME_LEN];
char databuf[DATA_LEN];
getcurtime(databuf, DATA_LEN);
for (i = INFO; i <= ERROR; ++i)
{
strncpy(filename, SERV_NAME, 20);
strncat(filename, "_", 1);
strncat(filename, databuf, DATA_LEN);
strncat(filename, "_", 1);
strncat(filename, level[i], 20);
strncat(filename, ".log", 20);
fplog[i] = fopen(filename, "w+");
printf(filename);
}
return 0;
}
int closelogfile()
{
int i;
for (i = INFO; i <= ERROR; ++i)
fclose(fplog[i]);
return 0;
}
int servlog(TYPE type, const char* format, ...)
{
assert(type >= INFO && type <= ERROR);
char logbuf[LOG_MAX_LEN];
char line[MAX_LINE_LEN];
char databuf[DATA_LEN];
va_list argptr;
int cnt;
memset(line, 0, MAX_LINE_LEN);
va_start(argptr, format);
cnt = vsnprintf(logbuf, LOG_MAX_LEN, format, argptr);
va_end(argptr);
getcurtime(databuf, DATA_LEN);
strncpy(line, databuf, DATA_LEN);
strncat(line, ":" , 1);
strncat(line, logbuf , cnt);
strncat(line, "\n" , 1);
fwrite(line, 1, strlen(line), fplog[type]);
//#ifdef _DEBUG
fflush(fplog[type]);
//#endif
return 0;
}
/*Usage
int main(void)
{
creatlogfile();
servlog(ERROR, "%s %d", "hello", 1);
servlog(DEBUG, "%s %s", "debug", "guanyu");
servlog(DEBUG, "%s", "hello world");
closelogfile();
return 0;
} */