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 #include "../ucx/map.h" |
43 #include "../ucx/map.h" |
|
44 #include "../util/atomic.h" |
44 |
45 |
45 static int is_initialized = 0; |
46 static int is_initialized = 0; |
46 |
47 |
47 static int log_file_fd; |
48 static int log_file_fd; |
48 static int log_level = 0; |
49 static int log_level = 0; |
51 * if the log file is uninitialized, output is written to the ui_buffer |
52 * if the log file is uninitialized, output is written to the ui_buffer |
52 */ |
53 */ |
53 static sbuf_t *ui_buffer = NULL; |
54 static sbuf_t *ui_buffer = NULL; |
54 |
55 |
55 /* |
56 /* |
56 * access logfile map |
57 * access log file map |
57 */ |
58 */ |
58 static UcxMap *access_log_files; // map of AccessLog* |
59 static UcxMap *access_log_files; // map of LogFile* |
59 static AccessLog *default_access_log; |
|
60 |
60 |
61 |
61 |
62 static char *log_date_month[] = { |
62 static char *log_date_month[] = { |
63 "Jan", |
63 "Jan", |
64 "Feb", |
64 "Feb", |
246 /* |
246 /* |
247 * access log |
247 * access log |
248 * This source file only manages access log files. IO is performed directly |
248 * This source file only manages access log files. IO is performed directly |
249 * by AddLog safs. |
249 * by AddLog safs. |
250 */ |
250 */ |
251 AccessLog* get_access_log(sstr_t file, sstr_t format) { |
251 LogFile* get_access_log_file(sstr_t file) { |
252 if(!access_log_files) { |
252 if(!access_log_files) { |
253 access_log_files = ucx_map_new(4); |
253 access_log_files = ucx_map_new(4); |
254 } |
254 } |
255 |
255 |
256 if(file.ptr == NULL || file.length == 0) { |
256 if(file.ptr == NULL || file.length == 0) { |
257 return NULL; |
257 return NULL; |
258 } |
258 } |
259 |
259 |
260 AccessLog *log = ucx_map_sstr_get(access_log_files, file); |
260 LogFile *log = ucx_map_sstr_get(access_log_files, file); |
261 if(log != NULL) { |
261 if(log != NULL) { |
262 // TODO: ref |
262 ws_atomic_inc32(&log->ref); |
263 return log; |
263 return log; |
264 } |
264 } |
265 |
265 |
266 // the log file is not opened |
266 // the log file is not opened |
267 // check first if we can open it |
267 // check first if we can open it |
268 sstr_t path = sstrdup(file); |
268 FILE *out = fopen(file.ptr, "a"); |
269 FILE *out = fopen(path.ptr, "a"); |
|
270 if(out == NULL) { |
269 if(out == NULL) { |
271 free(path.ptr); |
|
272 return NULL; |
270 return NULL; |
273 } |
271 } |
274 |
272 |
275 // create access log object |
273 // create LogFile object |
276 log = calloc(1, sizeof(AccessLog)); |
274 log = calloc(1, sizeof(LogFile)); |
277 log->file = path; |
275 log->file = out; |
278 if(format.ptr != NULL) { |
276 log->ref = 1; |
279 log->format = sstrdup(format); |
|
280 } |
|
281 log->log = out; |
|
282 // TODO: log->ref = 1; |
|
283 |
277 |
284 // add access log to the map |
278 // add access log to the map |
285 ucx_map_sstr_put(access_log_files, file, log); |
279 ucx_map_sstr_put(access_log_files, file, log); |
286 |
280 |
287 if(!default_access_log) { |
|
288 default_access_log = log; |
|
289 } |
|
290 |
|
291 return log; |
281 return log; |
292 } |
282 } |
293 |
283 |
294 AccessLog* get_default_access_log() { |
|
295 // TODO: ref |
|
296 return default_access_log; |
|
297 } |
|