-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggerconf.c
182 lines (161 loc) · 4.17 KB
/
loggerconf.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "loggerconf.h"
#include "logger.h"
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
/* Logger type */
kConsoleLogger = 1 << 0,
kFileLogger = 1 << 1,
kMaxFileNameLen = 256,
kMaxLineLen = 512,
};
/* Console logger */
static struct { FILE *output; } s_clog;
/* File logger */
static struct {
char filename[kMaxFileNameLen];
long maxFileSize;
unsigned char maxBackupFiles;
} s_flog;
static int s_logger;
static void reset(void);
static void removeComments(char *s);
static void trim(char *s);
static void parseLine(char *line);
static int hasFlag(int flags, int flag);
int logger_configure(const char *filename) {
FILE *fp;
char line[kMaxLineLen];
if (filename == NULL) {
assert(0 && "filename must not be NULL");
return 0;
}
reset();
if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "ERROR: loggerconf: Failed to open file: `%s`\n", filename);
return 0;
}
while (fgets(line, sizeof(line), fp) != NULL) {
removeComments(line);
trim(line);
if (line[0] == '\0') {
continue;
}
parseLine(line);
}
fclose(fp);
if (hasFlag(s_logger, kConsoleLogger)) {
if (!logger_initConsoleLogger(s_clog.output)) {
return 0;
}
}
if (hasFlag(s_logger, kFileLogger)) {
if (!logger_initFileLogger(s_flog.filename, s_flog.maxFileSize,
s_flog.maxBackupFiles)) {
return 0;
}
}
if (s_logger == 0) {
return 0;
}
return fileno(fp);
}
static void reset(void) {
s_logger = 0;
memset(&s_clog, 0, sizeof(s_clog));
memset(&s_flog, 0, sizeof(s_flog));
}
static void removeComments(char *s) {
int i;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] == '#') {
s[i] = '\0';
}
}
}
static void trim(char *s) {
size_t len;
int i;
if (s == NULL) {
return;
}
len = strlen(s);
if (len == 0) {
return;
}
/* trim right */
for (i = len - 1; i >= 0 && isspace(s[i]); i--) {
}
s[i + 1] = '\0';
/* trim left */
for (i = 0; s[i] != '\0' && isspace(s[i]); i++) {
}
memmove(s, &s[i], len - i);
s[len - i] = '\0';
}
static LogLevel parseLevel(const char *s);
static void parseLine(char *line) {
char *key, *val;
int nfiles;
key = strtok(line, "=");
val = strtok(NULL, "=");
if (strcmp(key, "level") == 0) {
logger_setLevel(parseLevel(val));
} else if (strcmp(key, "autoFlush") == 0) {
logger_autoFlush(atol(val));
} else if (strcmp(key, "logger") == 0) {
if (strcmp(val, "console") == 0) {
s_logger |= kConsoleLogger;
} else if (strcmp(val, "file") == 0) {
s_logger |= kFileLogger;
} else {
fprintf(stderr, "ERROR: loggerconf: Invalid logger: `%s`\n", val);
s_logger = 0;
}
} else if (strcmp(key, "logger.console.output") == 0) {
if (strcmp(val, "stdout") == 0) {
s_clog.output = stdout;
} else if (strcmp(val, "stderr") == 0) {
s_clog.output = stderr;
} else {
fprintf(stderr,
"ERROR: loggerconf: Invalid logger.console.output: `%s`\n", val);
s_clog.output = NULL;
}
} else if (strcmp(key, "logger.file.filename") == 0) {
strncpy(s_flog.filename, val, sizeof(s_flog.filename));
} else if (strcmp(key, "logger.file.maxFileSize") == 0) {
s_flog.maxFileSize = atol(val);
} else if (strcmp(key, "logger.file.maxBackupFiles") == 0) {
nfiles = atoi(val);
if (nfiles < 0) {
fprintf(stderr,
"ERROR: loggerconf: Invalid logger.file.maxBackupFiles: `%s`\n",
val);
nfiles = 0;
}
s_flog.maxBackupFiles = nfiles;
}
}
static LogLevel parseLevel(const char *s) {
if (strcmp(s, "TRACE") == 0) {
return LogLevel_TRACE;
} else if (strcmp(s, "DEBUG") == 0) {
return LogLevel_DEBUG;
} else if (strcmp(s, "INFO") == 0) {
return LogLevel_INFO;
} else if (strcmp(s, "WARN") == 0) {
return LogLevel_WARN;
} else if (strcmp(s, "ERROR") == 0) {
return LogLevel_ERROR;
} else if (strcmp(s, "FATAL") == 0) {
return LogLevel_FATAL;
} else {
fprintf(stderr, "ERROR: loggerconf: Invalid level: `%s`\n", s);
return logger_getLevel();
}
}
static int hasFlag(int flags, int flag) { return (flags & flag) == flag; }