X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/blobdiff_plain/41ba48b4c2ec0b155830664701cc83e8eb1099f2..babee1a0fda9095a0a2b2b5c529e03382a7d0363:/core/error.c diff --git a/core/error.c b/core/error.c index 895e786c..f7e554e3 100644 --- a/core/error.c +++ b/core/error.c @@ -9,6 +9,8 @@ FILE *logfile; +static corehandler *coreh, *coret; + char *sevtostring(int severity) { switch(severity) { case ERR_DEBUG: @@ -38,14 +40,23 @@ void reopen_logfile(int hooknum, void *arg) { if (logfile) fclose(logfile); - logfile=fopen("newserv.log","a"); + logfile=fopen("logs/newserv.log","a"); } void init_logfile() { - logfile=fopen("newserv.log","a"); + 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; @@ -68,8 +79,12 @@ void Error(char *source, int severity, char *reason, ... ) { 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); - if (logfile) + fflush(stderr); + + if (logfile) { fprintf(logfile,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf); + fflush(logfile); + } } if (severity>=ERR_STOP) { @@ -78,3 +93,44 @@ void Error(char *source, int severity, char *reason, ... ) { 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); +} +