src/server/daemon/log.c

changeset 45
a24aa388f02f
parent 44
3da1f7b6847f
child 60
feb2f1e115c6
--- a/src/server/daemon/log.c	Tue Jan 01 19:22:56 2013 +0100
+++ b/src/server/daemon/log.c	Wed Jan 02 16:03:50 2013 +0100
@@ -40,16 +40,24 @@
 #include "log.h"
 #include "../util/strbuf.h"
 #include "../util/io.h"
-
-int is_initialized = 0;
+#include "../ucx/map.h"
 
-int log_file_fd;
-int log_level = 0;
+static int is_initialized = 0;
+
+static int log_file_fd;
+static int log_level = 0;
 
 /*
  * if the log file is uninitialized, output is written to the ui_buffer
  */
-sbuf_t *ui_buffer = NULL;
+static sbuf_t *ui_buffer = NULL;
+
+/*
+ * access logfile map
+ */
+static UcxMap    *access_log_files; // map of AccessLog*
+static AccessLog *default_access_log;
+
 
 char *log_date_month[] = {
     "Jan",
@@ -215,3 +223,57 @@
     
     return 0;
 }
+
+
+/*
+ * access log
+ * This source file only manages access log files. IO is performed directly
+ * by AddLog safs. 
+ */
+AccessLog* get_access_log(sstr_t file, sstr_t format) {
+    if(!access_log_files) {
+        access_log_files = ucx_map_new(4);
+    }
+    
+    if(file.ptr == NULL || file.length == 0) {
+        return NULL;
+    }
+    
+    AccessLog *log = ucx_map_sstr_get(access_log_files, file);
+    if(log != NULL) {
+        // TODO: ref
+        return log;
+    }
+    
+    // the log file is not opened
+    // check first if we can open it
+    sstr_t path = sstrdup(file);
+    FILE *out = fopen(path.ptr, "a");
+    if(out == NULL) {
+        free(path.ptr);
+        return NULL;
+    }
+    
+    // create access log object
+    log = calloc(1, sizeof(AccessLog));
+    log->file = path;
+    if(format.ptr != NULL) {
+        log->format = sstrdup(format);
+    }
+    log->log = out;
+    // TODO: log->ref = 1;
+    
+    // add access log to the map
+    ucx_map_sstr_put(access_log_files, file, log);
+    
+    if(!default_access_log) {
+        default_access_log = log;
+    }
+    
+    return log;
+}
+
+AccessLog* get_default_access_log() {
+    // TODO: ref
+    return default_access_log;
+}

mercurial