]> jfr.im git - irc/quakenet/newserv.git/blobdiff - core/error.c
LUA: add function for channel chanop notice
[irc/quakenet/newserv.git] / core / error.c
index 7a98081f3c6fd277a77614647cca2006a27498c1..f7e554e3f8f97d356cd96460513c03d6d30408ad 100644 (file)
@@ -3,7 +3,13 @@
 #include <stdarg.h>
 #include <time.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include "error.h"
+#include "hooks.h"
+
+FILE *logfile;
+
+static corehandler *coreh, *coret;
 
 char *sevtostring(int severity) {
   switch(severity) {
@@ -30,26 +36,101 @@ char *sevtostring(int severity) {
   }
 }
 
+void reopen_logfile(int hooknum, void *arg) {
+  if (logfile)
+    fclose(logfile);
+  
+  logfile=fopen("logs/newserv.log","a");
+}
+
+void init_logfile() {
+  logfile=fopen("logs/newserv.log","a");
+  if (!logfile) {
+    fprintf(stderr,"Failed to open logfile...\n");
+  }
+  registerhook(HOOK_CORE_SIGUSR1, reopen_logfile);
+}
+
+void fini_logfile() {
+  deregisterhook(HOOK_CORE_SIGUSR1, reopen_logfile);
+  if (logfile)
+    fclose(logfile);
+}
+
 void Error(char *source, int severity, char *reason, ... ) {
   char buf[512];
   va_list va;
   struct tm *tm;
   time_t now;
   char timebuf[100];
-  
+  struct error_event evt;
+    
   va_start(va,reason);
   vsnprintf(buf,512,reason,va);
   va_end(va);
   
+  evt.severity=severity;
+  evt.message=buf;
+  evt.source=source;
+  triggerhook(HOOK_CORE_ERROR, &evt);
+  
   if (severity>ERR_DEBUG) {
     now=time(NULL);
     tm=gmtime(&now);
     strftime(timebuf,100,"%Y-%m-%d %H:%M:%S",tm);
     fprintf(stderr,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf);
+    fflush(stderr);
+
+    if (logfile)  {
+      fprintf(logfile,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf);
+      fflush(logfile);
+    }
   }
   
   if (severity>=ERR_STOP) {
     fprintf(stderr,"Terminal error occured, exiting...\n");
+    triggerhook(HOOK_CORE_STOPERROR, NULL);
     exit(0);
   }
 }
+
+void handlecore(void) { 
+  corehandler *n;
+
+  /* no attempt is made to clean these up */
+  for(n=coreh;coreh;n=coreh->next)
+    (n->fn)(n->arg);
+}
+
+corehandler *registercorehandler(CoreHandlerFn fn, void *arg) {
+  corehandler *c = (corehandler *)malloc(sizeof(corehandler));
+  /* core if we can't allocate!! */
+
+  c->fn = fn;
+  c->arg = arg;
+  c->next = NULL;
+  c->prev = coret;
+  coret = c->prev;
+
+  if(!coreh)
+    coreh = c;
+
+  return c;
+}
+
+void deregistercorehandler(corehandler *c) {
+  if(!c->prev) {
+    coreh = c->next;
+  } else {
+    c->prev->next = c->next;
+  }
+
+  if(!c->next) {
+    coret = c->prev;
+  } else {
+    c->next->prev = c->prev;
+  }
+
+  free(c);
+}
+