]>
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 CP |
9 | |
10 | char *sevtostring(int severity) { | |
11 | switch(severity) { | |
12 | case ERR_DEBUG: | |
13 | return "debug"; | |
14 | ||
15 | case ERR_INFO: | |
16 | return "info"; | |
17 | ||
18 | case ERR_WARNING: | |
19 | return "warning"; | |
20 | ||
21 | case ERR_ERROR: | |
22 | return "error"; | |
23 | ||
24 | case ERR_FATAL: | |
25 | return "fatal error"; | |
b71fee1d | 26 | |
27 | case ERR_STOP: | |
28 | return "terminal error"; | |
2c5db955 CP |
29 | |
30 | default: | |
31 | return "unknown error"; | |
32 | } | |
33 | } | |
34 | ||
35 | void Error(char *source, int severity, char *reason, ... ) { | |
36 | char buf[512]; | |
37 | va_list va; | |
38 | struct tm *tm; | |
39 | time_t now; | |
40 | char timebuf[100]; | |
41 | ||
42 | va_start(va,reason); | |
43 | vsnprintf(buf,512,reason,va); | |
44 | va_end(va); | |
45 | ||
46 | if (severity>ERR_DEBUG) { | |
47 | now=time(NULL); | |
48 | tm=gmtime(&now); | |
49 | strftime(timebuf,100,"%Y-%m-%d %H:%M:%S",tm); | |
50 | fprintf(stderr,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf); | |
51 | } | |
b71fee1d | 52 | |
53 | if (severity>=ERR_STOP) { | |
54 | fprintf(stderr,"Terminal error occured, exiting...\n"); | |
d931d0f7 | 55 | triggerhook(HOOK_CORE_STOPERROR, NULL); |
b71fee1d | 56 | exit(0); |
57 | } | |
2c5db955 | 58 | } |