src/server/daemon/log.c

branch
srvctrl
changeset 167
4be7dd2b75b9
parent 166
c07122f66676
child 176
f2268fcbe487
--- a/src/server/daemon/log.c	Sun Jan 29 09:04:06 2017 +0100
+++ b/src/server/daemon/log.c	Sun Jan 29 09:40:04 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);
 
 /*
@@ -181,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) {
@@ -214,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 *dup) {
+    pthread_mutex_lock(&mutex);
+    UcxList *elm = log_dup_list;
+    while(elm) {
+        if(elm->data = dup) {
+            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

mercurial