]> jfr.im git - irc/quakenet/newserv.git/blob - core/error.c
Merge
[irc/quakenet/newserv.git] / core / error.c
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 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";
28
29 case ERR_STOP:
30 return "terminal error";
31
32 default:
33 return "unknown error";
34 }
35 }
36
37 void reopen_logfile(int hooknum, void *arg) {
38 if (logfile)
39 fclose(logfile);
40
41 logfile=fopen("newserv.log","a");
42 }
43
44 void init_logfile() {
45 logfile=fopen("newserv.log","a");
46 registerhook(HOOK_CORE_SIGUSR1, reopen_logfile);
47 }
48
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];
55 struct error_event evt;
56
57 va_start(va,reason);
58 vsnprintf(buf,512,reason,va);
59 va_end(va);
60
61 evt.severity=severity;
62 evt.message=buf;
63 evt.source=source;
64 triggerhook(HOOK_CORE_ERROR, &evt);
65
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);
71 if (logfile)
72 fprintf(logfile,"[%s] %s(%s): %s\n",timebuf,sevtostring(severity),source,buf);
73 }
74
75 if (severity>=ERR_STOP) {
76 fprintf(stderr,"Terminal error occured, exiting...\n");
77 triggerhook(HOOK_CORE_STOPERROR, NULL);
78 exit(0);
79 }
80 }