[IOMultiplexerV2] coding style fixes
[NextIRCd.git] / src / IOHandler / IOLog.c
index 20637c14578f5f5585a2564d331dc713831432b2..6a298b95288f640aef6cf6bb7186bbfc404c4a8b 100644 (file)
 
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
+
+struct iolog_callback_entry {
+       iolog_callback *callback;
+       struct iolog_callback_entry *next;
+};
+static struct iolog_callback_entry *iolog_callbacks = NULL;
 
 void iolog_init() {
 
@@ -29,20 +36,29 @@ void iolog_init() {
 #define MAXLOG 1024
 
 void iolog_trigger(enum IOLogType type, char *text, ...) {
-    va_list arg_list;
-    char logBuf[MAXLOG+1];
-    int pos;
-    logBuf[0] = '\0';
-    va_start(arg_list, text);
-    pos = vsnprintf(logBuf, MAXLOG - 1, text, arg_list);
-    va_end(arg_list);
-    if (pos < 0 || pos > (MAXLOG - 1)) pos = MAXLOG - 1;
-    logBuf[pos] = '\n';
-    logBuf[pos+1] = '\0';
-    
-    printf("%s", logBuf);
+       va_list arg_list;
+       char logBuf[MAXLOG+1];
+       int pos;
+       logBuf[0] = '\0';
+       va_start(arg_list, text);
+       pos = vsnprintf(logBuf, MAXLOG - 1, text, arg_list);
+       va_end(arg_list);
+       if (pos < 0 || pos > (MAXLOG - 1)) pos = MAXLOG - 1;
+       logBuf[pos] = '\n';
+       logBuf[pos+1] = '\0';
+       
+       struct iolog_callback_entry *callback;
+       for(callback = iolog_callbacks; callback; callback = callback->next)
+               callback->callback(type, logBuf);
 }
 
 void iolog_register_callback(iolog_callback *callback) {
-
+       struct iolog_callback_entry *logcb = malloc(sizeof(*logcb));
+       if(!logcb) {
+               iolog_trigger(IOLOG_ERROR, "Failed to allocate memory for iolog_callback_entry in %s:%d", __FILE__, __LINE__);
+               return;
+       }
+       logcb->callback = callback;
+       logcb->next = iolog_callbacks;
+       iolog_callbacks = logcb;
 }