]> jfr.im git - irc/quakenet/newserv.git/blame - core/error.c
Merge.
[irc/quakenet/newserv.git] / core / error.c
CommitLineData
2c5db955
CP
1/* error.c */
2
3#include <stdarg.h>
4#include <time.h>
5#include <stdio.h>
d931d0f7 6#include <stdlib.h>
2c5db955 7#include "error.h"
d931d0f7 8#include "hooks.h"
2c5db955 9
280505a5 10FILE *logfile;
11
cdc37146
CP
12static corehandler *coreh, *coret;
13
2c5db955
CP
14char *sevtostring(int severity) {
15 switch(severity) {
16 case ERR_DEBUG:
17 return "debug";
18
19 case ERR_INFO:
20 return "info";
21
22 case ERR_WARNING:
23 return "warning";
24
25 case ERR_ERROR:
26 return "error";
27
28 case ERR_FATAL:
29 return "fatal error";
b71fee1d 30
31 case ERR_STOP:
32 return "terminal error";
2c5db955
CP
33
34 default:
35 return "unknown error";
36 }
37}
38
3fe02fb2 39void reopen_logfile(int hooknum, void *arg) {
40 if (logfile)
41 fclose(logfile);
42
f3395fa6 43 logfile=fopen("logs/newserv.log","a");
3fe02fb2 44}
45
280505a5 46void init_logfile() {
f3395fa6 47 logfile=fopen("logs/newserv.log","a");
3fe02fb2 48 registerhook(HOOK_CORE_SIGUSR1, reopen_logfile);
280505a5 49}
50
4522b71a 51void fini_logfile() {
f78f7033 52 if (logfile)
53 fclose(logfile);
4522b71a 54 deregisterhook(HOOK_CORE_SIGUSR1, reopen_logfile);
4522b71a
P
55}
56
2c5db955
CP
57void Error(char *source, int severity, char *reason, ... ) {
58 char buf[512];
59 va_list va;
60 struct tm *tm;
61 time_t now;
62 char timebuf[100];
280505a5 63 struct error_event evt;
64
2c5db955
CP
65 va_start(va,reason);
66 vsnprintf(buf,512,reason,va);
67 va_end(va);
68
280505a5 69 evt.severity=severity;
70 evt.message=buf;
4011a4ee 71 evt.source=source;
280505a5 72 triggerhook(HOOK_CORE_ERROR, &evt);
73
2c5db955
CP
74 if (severity>ERR_DEBUG) {
75 now=time(NULL);
76 tm=gmtime(&now);
77 strftime(timebuf,100,"%Y-%m-%d %H:%M:%S",tm);
78 fprintf(stderr,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf);
0171b453
CP
79 fflush(stderr);
80
81 if (logfile) {
280505a5 82 fprintf(logfile,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf);
0171b453
CP
83 fflush(logfile);
84 }
2c5db955 85 }
b71fee1d 86
87 if (severity>=ERR_STOP) {
88 fprintf(stderr,"Terminal error occured, exiting...\n");
d931d0f7 89 triggerhook(HOOK_CORE_STOPERROR, NULL);
b71fee1d 90 exit(0);
91 }
2c5db955 92}
cdc37146
CP
93
94void handlecore(void) {
95 corehandler *n;
96
97 /* no attempt is made to clean these up */
98 for(n=coreh;coreh;n=coreh->next)
99 (n->fn)(n->arg);
100}
101
102corehandler *registercorehandler(CoreHandlerFn fn, void *arg) {
103 corehandler *c = (corehandler *)malloc(sizeof(corehandler));
104 /* core if we can't allocate!! */
105
106 c->fn = fn;
107 c->arg = arg;
108 c->next = NULL;
109 c->prev = coret;
110 coret = c->prev;
111
112 if(!coreh)
113 coreh = c;
114
115 return c;
116}
117
118void deregistercorehandler(corehandler *c) {
119 if(!c->prev) {
120 coreh = c->next;
121 } else {
122 c->prev->next = c->next;
123 }
124
125 if(!c->next) {
126 coret = c->prev;
127 } else {
128 c->next->prev = c->prev;
129 }
130
131 free(c);
132}
133