srvctrl gets log messages srvctrl

Sun, 29 Jan 2017 09:40:04 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Jan 2017 09:40:04 +0100
branch
srvctrl
changeset 167
4be7dd2b75b9
parent 166
c07122f66676
child 173
63b8d52db390

srvctrl gets log messages

src/server/daemon/log.c file | annotate | diff | comparison | revisions
src/server/daemon/log.h file | annotate | diff | comparison | revisions
src/server/daemon/srvctrl.c file | annotate | diff | comparison | revisions
src/server/daemon/srvctrl.h file | annotate | diff | comparison | revisions
--- 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
--- a/src/server/daemon/log.h	Sun Jan 29 09:04:06 2017 +0100
+++ b/src/server/daemon/log.h	Sun Jan 29 09:40:04 2017 +0100
@@ -31,6 +31,7 @@
 
 #include "../public/nsapi.h"
 #include <ucx/string.h>
+#include <ucx/utils.h>
 
 #include <inttypes.h>
 
@@ -56,6 +57,11 @@
     LogFile  *log;
 } AccessLog;
 
+typedef void (*log_writefunc)(void *cookie, char *msg, size_t length);
+typedef struct {
+    log_writefunc write;
+    void *cookie;
+} LogDup;
 // server logging
 int init_log_file(LogConfig *cfg);
 
@@ -65,6 +71,8 @@
 
 sstr_t log_get_prefix(int level);
 
+void log_add_logdup(LogDup *dup);
+void log_remove_logdup(LogDup *dup);
 
 // access logging
 LogFile* get_access_log_file(sstr_t file);
--- a/src/server/daemon/srvctrl.c	Sun Jan 29 09:04:06 2017 +0100
+++ b/src/server/daemon/srvctrl.c	Sun Jan 29 09:40:04 2017 +0100
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "log.h"
 #include "webserver.h"
 
 #include "../util/systhr.h"
@@ -140,6 +141,11 @@
 }
 
 void* srvctrl_thread(SrvCtrlClient *client) {
+    LogDup log;
+    log.write = (log_writefunc)srvctrl_log;
+    log.cookie = client;
+    log_add_logdup(&log);
+    
     char buf[64];
     UcxBuffer *line = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
     
@@ -163,6 +169,8 @@
         }
     }
     
+    log_remove_logdup(&log);
+    
     ucx_buffer_free(line);
     close(client->fd);
     free(client);
@@ -172,8 +180,7 @@
 
 int srvctrl_handle_cmd(SrvCtrlClient *client, sstr_t cmd) {
     if(!sstrcmp(cmd, S("reconfig"))) {
-        write(client->fd, "reconfig\n", 9);
-        
+        log_ereport(LOG_INFORM, "reconfigure server");
         if(webserver_reconfig()) {
             log_ereport(LOG_FAILURE, "cannot reload config");
         } else {
@@ -190,3 +197,15 @@
     }
     return 1;
 }
+
+void srvctrl_log(SrvCtrlClient *client, char *msg, size_t len) {
+    size_t pos = 0;
+    ssize_t w = 0;
+    while(pos < len) {
+        w = write(client->fd, msg + pos, len - pos);
+        if(w <= 0) {
+            break;
+        }
+        pos += w;
+    }
+}
--- a/src/server/daemon/srvctrl.h	Sun Jan 29 09:04:06 2017 +0100
+++ b/src/server/daemon/srvctrl.h	Sun Jan 29 09:40:04 2017 +0100
@@ -53,6 +53,7 @@
 
 void* srvctrl_thread(SrvCtrlClient *client);
 int srvctrl_handle_cmd(SrvCtrlClient *client, sstr_t cmd);
+void srvctrl_log(SrvCtrlClient *client, char *msg, size_t len);
 
 
 #ifdef __cplusplus

mercurial