src/server/daemon/log.c

changeset 415
d938228c382e
parent 392
0aef555055ee
child 434
ff576305ae6e
--- a/src/server/daemon/log.c	Wed Nov 02 19:19:01 2022 +0100
+++ b/src/server/daemon/log.c	Sun Nov 06 15:53:32 2022 +0100
@@ -43,8 +43,9 @@
 #include "../util/io.h"
 #include "../util/atomic.h"
 
-#include <ucx/map.h>
-#include <ucx/list.h>
+#include <cx/hash_map.h>
+#include <cx/linked_list.h>
+#include <cx/compare.h>
 
 static int is_initialized = 0;
 
@@ -52,7 +53,7 @@
 static int log_level = 0;
 
 static uint32_t log_dup_count = 0;
-static UcxList *log_dup_list = NULL;
+static CxList *log_dup_list = NULL;
 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
 WSBool main_is_daemon(void);
@@ -65,7 +66,7 @@
 /*
  * access log file map
  */
-static UcxMap *access_log_files; // map of LogFile*
+static CxMap *access_log_files; // map of LogFile*
 
 
 static char *log_date_month[] = {
@@ -110,6 +111,8 @@
         return 0;
     }
     
+    log_dup_list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_ptr);
+    
     /* open the log file */
     mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
     log_file_fd = open(cfg->file, O_WRONLY | O_CREAT | O_APPEND, mode);
@@ -165,7 +168,7 @@
         }
     }
     
-    sstr_t s;
+    cxstring s;
     s.ptr = str;
     s.length = len;
     
@@ -195,8 +198,8 @@
         msg[len] = '\n';
         
         pthread_mutex_lock(&mutex);
-        UCX_FOREACH(elm, log_dup_list) {
-            LogDup *dup = elm->data;
+        CxIterator i = cxListIterator(log_dup_list, 0);
+        cx_foreach(LogDup *, dup, i) {
             dup->write(dup->cookie, msg, len + 1);
         }        
         pthread_mutex_unlock(&mutex);
@@ -205,10 +208,10 @@
     }
 }
 
-sstr_t log_get_prefix(int level) {
+cxmutstr log_get_prefix(int level) {
     time_t t = time(NULL);
     
-    sstr_t d;
+    cxmutstr d;
     d.ptr = NULL;
     d.length = 0;
     
@@ -238,21 +241,22 @@
 
 void log_add_logdup(LogDup *dup) {
     pthread_mutex_lock(&mutex);  
-    log_dup_list = ucx_list_append(log_dup_list, dup);
+    cxListAdd(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);
+    CxIterator i = cxListIterator(log_dup_list, 0);
+    WSBool finished = 0;
+    cx_foreach(LogDup *, dup, i) {
+        if(finished) break;
+        if(dup == ldup) {
+            i.remove = 1;
+            finished = 1;
             ws_atomic_dec32(&log_dup_count);
-            break;
         }
-        elm = elm->next;
     }
     pthread_mutex_unlock(&mutex);
 }
@@ -278,18 +282,18 @@
         return 0;
     }
     
-    sstr_t lmsg;
+    cxmutstr lmsg;
     lmsg.ptr = NULL;
     
     /* create log message prefix */
-    sstr_t lpre = log_get_prefix(degree); 
+    cxmutstr lpre = log_get_prefix(degree); 
     
     /* format message */
     int len = vasprintf(&lmsg.ptr, format, args);
     lmsg.length = len;
     
     /* create message string */
-    sstr_t message = sstrcat(2, lpre, lmsg);
+    cxmutstr message = cx_strcat(2, lpre, lmsg);
     
     /* write message to the log file */ 
     log_file_writeln(message.ptr, message.length);
@@ -336,18 +340,19 @@
  * This source file only manages access log files. IO is performed directly
  * by AddLog safs. 
  */
-LogFile* get_access_log_file(scstr_t file) {
+LogFile* get_access_log_file(cxstring file) {
     // TODO: this looks dubious
     
     if(!access_log_files) {
-        access_log_files = ucx_map_new(4);
+        access_log_files = cxHashMapCreate(cxDefaultAllocator, 4);
     }
     
     if(file.ptr == NULL || file.length == 0) {
         return NULL;
     }
     
-    LogFile *log = ucx_map_sstr_get(access_log_files, file);
+    CxHashKey key = cx_hash_key_bytes((unsigned const char*)file.ptr, file.length);
+    LogFile *log = cxMapGet(access_log_files, key);
     if(log != NULL) {
         ws_atomic_inc32(&log->ref);
         return log;
@@ -366,7 +371,7 @@
     log->ref = 1;
     
     // add access log to the map
-    ucx_map_sstr_put(access_log_files, file, log);
+    cxMapPut(access_log_files, key, log);
     
     return log;
 }

mercurial