src/server/daemon/log.c

changeset 179
ef6827505bd2
parent 176
f2268fcbe487
child 256
19259b6c5cf7
--- a/src/server/daemon/log.c	Fri Feb 24 11:17:53 2017 +0100
+++ b/src/server/daemon/log.c	Mon Mar 06 17:32:26 2017 +0100
@@ -36,6 +36,7 @@
 #include <unistd.h>
 #include <aio.h>
 #include <time.h>
+#include <pthread.h>
 
 #include "log.h"
 #include "../util/strbuf.h"
@@ -43,12 +44,17 @@
 #include "../util/atomic.h"
 
 #include <ucx/map.h>
+#include <ucx/list.h>
 
 static int is_initialized = 0;
 
 static int log_file_fd;
 static int log_level = 0;
 
+static uint32_t log_dup_count = 0;
+static UcxList *log_dup_list = NULL;
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+
 WSBool main_is_daemon(void);
 
 /*
@@ -84,7 +90,8 @@
     "failure",
     "catastrophe",
     "info",
-    "verbose"
+    "verbose",
+    "debug"
 };
 
 static int can_log[] = {
@@ -94,7 +101,8 @@
     1, // failure
     1, // catastrophe
     1, // info
-    0  // verbose
+    0, // verbose
+    0  // debug
 };
 
 int init_log_file(LogConfig *cfg) {
@@ -114,10 +122,11 @@
         can_log[LOG_INFORM] = 0;
     } else if(!strcmp(cfg->level, "WARNING")) {
         can_log[LOG_INFORM] = 0;
-    } else if(!strcmp(cfg->level, "INFO")) {
-        
     } else if(!strcmp(cfg->level, "VERBOSE")) {
         can_log[LOG_VERBOSE] = 1;
+    } else if(!strcmp(cfg->level, "DEBUG")) {
+        can_log[LOG_VERBOSE] = 1;
+        can_log[LOG_DEBUG] = 1;
     }
     
     if(cfg->log_stdout) {
@@ -178,6 +187,21 @@
     if(!main_is_daemon()) {
         writev(STDOUT_FILENO, io, 2);
     }
+    
+    if(log_dup_count > 0) {
+        char *msg = malloc(len + 1);
+        memcpy(msg, str, len);
+        msg[len] = '\n';
+        
+        pthread_mutex_lock(&mutex);
+        UCX_FOREACH(elm, log_dup_list) {
+            LogDup *dup = elm->data;
+            dup->write(dup->cookie, msg, len + 1);
+        }        
+        pthread_mutex_unlock(&mutex);
+        
+        free(msg);
+    }
 }
 
 sstr_t log_get_prefix(int level) {
@@ -211,6 +235,27 @@
     return d;
 }
 
+void log_add_logdup(LogDup *dup) {
+    pthread_mutex_lock(&mutex);  
+    log_dup_list = ucx_list_append(log_dup_list, dup);
+    ws_atomic_inc32(&log_dup_count);   
+    pthread_mutex_unlock(&mutex);
+}
+
+void log_remove_logdup(LogDup *ldup) {
+    pthread_mutex_lock(&mutex);
+    UcxList *elm = log_dup_list;
+    while(elm) {
+        if(elm->data == ldup) {
+            log_dup_list = ucx_list_remove(log_dup_list, elm);
+            ws_atomic_dec32(&log_dup_count);
+            break;
+        }
+        elm = elm->next;
+    }
+    pthread_mutex_unlock(&mutex);
+}
+
 
 /*
  * log api functions
@@ -225,10 +270,10 @@
 }
 
 int log_ereport_v(int degree, const char *format, va_list args) { 
-    if(degree > 6) {
+    if(degree < 0 || degree > 7) {
         return 0;
     }
-    if(degree > 0 && !can_log[degree]) {
+    if(!can_log[degree]) {
         return 0;
     }
     

mercurial