-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcp_log.c
121 lines (100 loc) · 2.94 KB
/
dhcp_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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdarg.h>
#include<time.h>
#include<sys/types.h>
#include "dhcp_log.h"
struct log_config gobal_file_config =
{
.log_enabled = 0,
.log_level = 3,
.log_file_dir = "/var/log",
};
int log_init(char *config_file)
{
//parse configuration file
FILE *file = fopen(config_file, "r");
if(NULL == file)
{
return -1;
}
char buffer[CONFIG_BUFFER_SIZE];
while(!feof(file))
{
if(NULL != fgets(buffer, CONFIG_BUFFER_SIZE, file))
{
int index = 0;
for(; '\0' != buffer[index] && '=' != buffer[index]; index++);
if('\0' == buffer[index])
{
continue;
}
buffer[index] = '\0';
char *value_begin = buffer + index + 1;
int value_length = strlen(value_begin);
if(0 == strcmp(buffer, CONDIF_LOG_ENABLED))
{
memcpy(&gobal_file_config.log_enabled, value_begin, 1);
gobal_file_config.log_enabled = gobal_file_config.log_enabled - '0';
}
else if(0 == strcmp(buffer, CONFIG_LOG_LEVEL))
{
memcpy(&gobal_file_config.log_level, value_begin, 1);
gobal_file_config.log_level = gobal_file_config.log_level - '0';
}
else if(0 == strcmp(buffer, CONFIG_LOG_FILE_DIR))
{
if('\n' == value_begin[value_length - 1])
{
value_begin[value_length - 1] = '\0';
}
strncpy(gobal_file_config.log_file_dir, value_begin , MAX_FILE_PATH);
}
}
}
fclose(file);
}
void dhcp_log(char level, const char *source, const char *func, int line, char *message, ...)
{
if(0 == gobal_file_config.log_enabled || level < gobal_file_config.log_level)
{
return;
}
time_t now;
time(&now);
struct tm *tm_now = gmtime(&now);
char file_path[MAX_FILE_PATH] = {0};
snprintf(file_path, MAX_FILE_PATH, "%s/%s_%4d-%02d-%02d.log", gobal_file_config.log_file_dir, LOG_FILE_NAME_PREFIX, tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday);
FILE *log_file = fopen(file_path, "a+");
if(NULL == log_file)
{
return;
}
va_list arg_list;
char buffer[LOG_BUFFER_SIZE];
va_start(arg_list, message);
vsnprintf(buffer, LOG_BUFFER_SIZE, message, arg_list);
va_end(arg_list);
fprintf(log_file, "%4d-%02d-%02d %02d:%02d:%02d %s[%s][%d]\t%05d:%05d %5s %s\n", tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec, source, func, line, getgid(), getpid(), log_level_string(level), buffer);
fclose(log_file);
}
char * log_level_string(char log_level)
{
switch(log_level)
{
case LOG_INFO:
return LOG_INFO_STRING;
case LOG_DEBUG:
return LOG_DEBUG_STRING;
case LOG_WARN:
return LOG_WARN_STRING;
case LOG_ERROR:
return LOG_ERROR_STRING;
case LOG_FATAL:
return LOG_FATAL_STRING;
default:
break;
}
return NULL;
}