38 #include <time.h> |
38 #include <time.h> |
39 |
39 |
40 #include "log.h" |
40 #include "log.h" |
41 #include "../util/strbuf.h" |
41 #include "../util/strbuf.h" |
42 #include "../util/io.h" |
42 #include "../util/io.h" |
43 |
43 #include "../ucx/map.h" |
44 int is_initialized = 0; |
44 |
45 |
45 static int is_initialized = 0; |
46 int log_file_fd; |
46 |
47 int log_level = 0; |
47 static int log_file_fd; |
|
48 static int log_level = 0; |
48 |
49 |
49 /* |
50 /* |
50 * if the log file is uninitialized, output is written to the ui_buffer |
51 * if the log file is uninitialized, output is written to the ui_buffer |
51 */ |
52 */ |
52 sbuf_t *ui_buffer = NULL; |
53 static sbuf_t *ui_buffer = NULL; |
|
54 |
|
55 /* |
|
56 * access logfile map |
|
57 */ |
|
58 static UcxMap *access_log_files; // map of AccessLog* |
|
59 static AccessLog *default_access_log; |
|
60 |
53 |
61 |
54 char *log_date_month[] = { |
62 char *log_date_month[] = { |
55 "Jan", |
63 "Jan", |
56 "Feb", |
64 "Feb", |
57 "Mar", |
65 "Mar", |
213 free(lmsg.ptr); |
221 free(lmsg.ptr); |
214 free(message.ptr); |
222 free(message.ptr); |
215 |
223 |
216 return 0; |
224 return 0; |
217 } |
225 } |
|
226 |
|
227 |
|
228 /* |
|
229 * access log |
|
230 * This source file only manages access log files. IO is performed directly |
|
231 * by AddLog safs. |
|
232 */ |
|
233 AccessLog* get_access_log(sstr_t file, sstr_t format) { |
|
234 if(!access_log_files) { |
|
235 access_log_files = ucx_map_new(4); |
|
236 } |
|
237 |
|
238 if(file.ptr == NULL || file.length == 0) { |
|
239 return NULL; |
|
240 } |
|
241 |
|
242 AccessLog *log = ucx_map_sstr_get(access_log_files, file); |
|
243 if(log != NULL) { |
|
244 // TODO: ref |
|
245 return log; |
|
246 } |
|
247 |
|
248 // the log file is not opened |
|
249 // check first if we can open it |
|
250 sstr_t path = sstrdup(file); |
|
251 FILE *out = fopen(path.ptr, "a"); |
|
252 if(out == NULL) { |
|
253 free(path.ptr); |
|
254 return NULL; |
|
255 } |
|
256 |
|
257 // create access log object |
|
258 log = calloc(1, sizeof(AccessLog)); |
|
259 log->file = path; |
|
260 if(format.ptr != NULL) { |
|
261 log->format = sstrdup(format); |
|
262 } |
|
263 log->log = out; |
|
264 // TODO: log->ref = 1; |
|
265 |
|
266 // add access log to the map |
|
267 ucx_map_sstr_put(access_log_files, file, log); |
|
268 |
|
269 if(!default_access_log) { |
|
270 default_access_log = log; |
|
271 } |
|
272 |
|
273 return log; |
|
274 } |
|
275 |
|
276 AccessLog* get_default_access_log() { |
|
277 // TODO: ref |
|
278 return default_access_log; |
|
279 } |