]>
Commit | Line | Data |
---|---|---|
1 | /* error.c */ | |
2 | ||
3 | #include <stdarg.h> | |
4 | #include <time.h> | |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include "error.h" | |
8 | #include "hooks.h" | |
9 | ||
10 | FILE *logfile; | |
11 | ||
12 | static corehandler *coreh, *coret; | |
13 | ||
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"; | |
30 | ||
31 | case ERR_STOP: | |
32 | return "terminal error"; | |
33 | ||
34 | default: | |
35 | return "unknown error"; | |
36 | } | |
37 | } | |
38 | ||
39 | void reopen_logfile(int hooknum, void *arg) { | |
40 | if (logfile) | |
41 | fclose(logfile); | |
42 | ||
43 | logfile=fopen("logs/newserv.log","a"); | |
44 | } | |
45 | ||
46 | void init_logfile() { | |
47 | logfile=fopen("logs/newserv.log","a"); | |
48 | registerhook(HOOK_CORE_SIGUSR1, reopen_logfile); | |
49 | } | |
50 | ||
51 | void fini_logfile() { | |
52 | if (logfile) | |
53 | fclose(logfile); | |
54 | deregisterhook(HOOK_CORE_SIGUSR1, reopen_logfile); | |
55 | } | |
56 | ||
57 | void 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]; | |
63 | struct error_event evt; | |
64 | ||
65 | va_start(va,reason); | |
66 | vsnprintf(buf,512,reason,va); | |
67 | va_end(va); | |
68 | ||
69 | evt.severity=severity; | |
70 | evt.message=buf; | |
71 | evt.source=source; | |
72 | triggerhook(HOOK_CORE_ERROR, &evt); | |
73 | ||
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); | |
79 | if (logfile) | |
80 | fprintf(logfile,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf); | |
81 | } | |
82 | ||
83 | if (severity>=ERR_STOP) { | |
84 | fprintf(stderr,"Terminal error occured, exiting...\n"); | |
85 | triggerhook(HOOK_CORE_STOPERROR, NULL); | |
86 | exit(0); | |
87 | } | |
88 | } | |
89 | ||
90 | void handlecore(void) { | |
91 | corehandler *n; | |
92 | ||
93 | /* no attempt is made to clean these up */ | |
94 | for(n=coreh;coreh;n=coreh->next) | |
95 | (n->fn)(n->arg); | |
96 | } | |
97 | ||
98 | corehandler *registercorehandler(CoreHandlerFn fn, void *arg) { | |
99 | corehandler *c = (corehandler *)malloc(sizeof(corehandler)); | |
100 | /* core if we can't allocate!! */ | |
101 | ||
102 | c->fn = fn; | |
103 | c->arg = arg; | |
104 | c->next = NULL; | |
105 | c->prev = coret; | |
106 | coret = c->prev; | |
107 | ||
108 | if(!coreh) | |
109 | coreh = c; | |
110 | ||
111 | return c; | |
112 | } | |
113 | ||
114 | void deregistercorehandler(corehandler *c) { | |
115 | if(!c->prev) { | |
116 | coreh = c->next; | |
117 | } else { | |
118 | c->prev->next = c->next; | |
119 | } | |
120 | ||
121 | if(!c->next) { | |
122 | coret = c->prev; | |
123 | } else { | |
124 | c->next->prev = c->prev; | |
125 | } | |
126 | ||
127 | free(c); | |
128 | } | |
129 |