]>
Commit | Line | Data |
---|---|---|
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 | 10 | FILE *logfile; |
11 | ||
cdc37146 CP |
12 | static corehandler *coreh, *coret; |
13 | ||
2c5db955 CP |
14 | char *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 | 39 | void reopen_logfile(int hooknum, void *arg) { |
40 | if (logfile) | |
41 | fclose(logfile); | |
42 | ||
5ec20d22 | 43 | logfile=fopen("logs/newserv.log","a"); |
3fe02fb2 | 44 | } |
45 | ||
280505a5 | 46 | void init_logfile() { |
5ec20d22 | 47 | logfile=fopen("logs/newserv.log","a"); |
9bfd636b P |
48 | if (!logfile) { |
49 | fprintf(stderr,"Failed to open logfile...\n"); | |
50 | } | |
3fe02fb2 | 51 | registerhook(HOOK_CORE_SIGUSR1, reopen_logfile); |
280505a5 | 52 | } |
53 | ||
f074aada P |
54 | void fini_logfile() { |
55 | deregisterhook(HOOK_CORE_SIGUSR1, reopen_logfile); | |
9bfd636b P |
56 | if (logfile) |
57 | fclose(logfile); | |
f074aada P |
58 | } |
59 | ||
2c5db955 CP |
60 | void Error(char *source, int severity, char *reason, ... ) { |
61 | char buf[512]; | |
62 | va_list va; | |
63 | struct tm *tm; | |
64 | time_t now; | |
65 | char timebuf[100]; | |
280505a5 | 66 | struct error_event evt; |
67 | ||
2c5db955 CP |
68 | va_start(va,reason); |
69 | vsnprintf(buf,512,reason,va); | |
70 | va_end(va); | |
71 | ||
280505a5 | 72 | evt.severity=severity; |
73 | evt.message=buf; | |
4011a4ee | 74 | evt.source=source; |
280505a5 | 75 | triggerhook(HOOK_CORE_ERROR, &evt); |
76 | ||
2c5db955 CP |
77 | if (severity>ERR_DEBUG) { |
78 | now=time(NULL); | |
79 | tm=gmtime(&now); | |
80 | strftime(timebuf,100,"%Y-%m-%d %H:%M:%S",tm); | |
81 | fprintf(stderr,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf); | |
280505a5 | 82 | if (logfile) |
83 | fprintf(logfile,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf); | |
2c5db955 | 84 | } |
b71fee1d | 85 | |
86 | if (severity>=ERR_STOP) { | |
87 | fprintf(stderr,"Terminal error occured, exiting...\n"); | |
d931d0f7 | 88 | triggerhook(HOOK_CORE_STOPERROR, NULL); |
b71fee1d | 89 | exit(0); |
90 | } | |
2c5db955 | 91 | } |
cdc37146 CP |
92 | |
93 | void handlecore(void) { | |
94 | corehandler *n; | |
95 | ||
96 | /* no attempt is made to clean these up */ | |
97 | for(n=coreh;coreh;n=coreh->next) | |
98 | (n->fn)(n->arg); | |
99 | } | |
100 | ||
101 | corehandler *registercorehandler(CoreHandlerFn fn, void *arg) { | |
102 | corehandler *c = (corehandler *)malloc(sizeof(corehandler)); | |
103 | /* core if we can't allocate!! */ | |
104 | ||
105 | c->fn = fn; | |
106 | c->arg = arg; | |
107 | c->next = NULL; | |
108 | c->prev = coret; | |
109 | coret = c->prev; | |
110 | ||
111 | if(!coreh) | |
112 | coreh = c; | |
113 | ||
114 | return c; | |
115 | } | |
116 | ||
117 | void deregistercorehandler(corehandler *c) { | |
118 | if(!c->prev) { | |
119 | coreh = c->next; | |
120 | } else { | |
121 | c->prev->next = c->next; | |
122 | } | |
123 | ||
124 | if(!c->next) { | |
125 | coret = c->prev; | |
126 | } else { | |
127 | c->next->prev = c->prev; | |
128 | } | |
129 | ||
130 | free(c); | |
131 | } | |
132 |