]>
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 | ||
2c5db955 CP |
12 | char *sevtostring(int severity) { |
13 | switch(severity) { | |
14 | case ERR_DEBUG: | |
15 | return "debug"; | |
16 | ||
17 | case ERR_INFO: | |
18 | return "info"; | |
19 | ||
20 | case ERR_WARNING: | |
21 | return "warning"; | |
22 | ||
23 | case ERR_ERROR: | |
24 | return "error"; | |
25 | ||
26 | case ERR_FATAL: | |
27 | return "fatal error"; | |
b71fee1d | 28 | |
29 | case ERR_STOP: | |
30 | return "terminal error"; | |
2c5db955 CP |
31 | |
32 | default: | |
33 | return "unknown error"; | |
34 | } | |
35 | } | |
36 | ||
3fe02fb2 | 37 | void reopen_logfile(int hooknum, void *arg) { |
38 | if (logfile) | |
39 | fclose(logfile); | |
40 | ||
41 | logfile=fopen("newserv.log","a"); | |
42 | } | |
43 | ||
280505a5 | 44 | void init_logfile() { |
45 | logfile=fopen("newserv.log","a"); | |
3fe02fb2 | 46 | registerhook(HOOK_CORE_SIGUSR1, reopen_logfile); |
280505a5 | 47 | } |
48 | ||
2c5db955 CP |
49 | void Error(char *source, int severity, char *reason, ... ) { |
50 | char buf[512]; | |
51 | va_list va; | |
52 | struct tm *tm; | |
53 | time_t now; | |
54 | char timebuf[100]; | |
280505a5 | 55 | struct error_event evt; |
56 | ||
2c5db955 CP |
57 | va_start(va,reason); |
58 | vsnprintf(buf,512,reason,va); | |
59 | va_end(va); | |
60 | ||
280505a5 | 61 | evt.severity=severity; |
62 | evt.message=buf; | |
4011a4ee | 63 | evt.source=source; |
280505a5 | 64 | triggerhook(HOOK_CORE_ERROR, &evt); |
65 | ||
2c5db955 CP |
66 | if (severity>ERR_DEBUG) { |
67 | now=time(NULL); | |
68 | tm=gmtime(&now); | |
69 | strftime(timebuf,100,"%Y-%m-%d %H:%M:%S",tm); | |
70 | fprintf(stderr,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf); | |
280505a5 | 71 | if (logfile) |
72 | fprintf(logfile,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf); | |
2c5db955 | 73 | } |
b71fee1d | 74 | |
75 | if (severity>=ERR_STOP) { | |
76 | fprintf(stderr,"Terminal error occured, exiting...\n"); | |
d931d0f7 | 77 | triggerhook(HOOK_CORE_STOPERROR, NULL); |
b71fee1d | 78 | exit(0); |
79 | } | |
2c5db955 | 80 | } |