]>
Commit | Line | Data |
---|---|---|
2ace9480 | 1 | /* Thales - IRC to Relational Database Gateway |
2 | * Copyright (C) 2002 Lucas Nussbaum <lucas@lucas-nussbaum.net> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
18 | ||
19 | #include "thales.h" | |
20 | #include "log.h" | |
21 | #include "misc.h" | |
22 | #include "send.h" | |
23 | ||
24 | extern char *LogFilename; | |
25 | extern char inbuf[]; | |
26 | ||
27 | extern int debug; | |
28 | extern int verbose; | |
29 | ||
30 | extern int servsock; | |
31 | ||
32 | FILE *logfile = NULL; | |
33 | ||
34 | /* Open the log file. Return -1 if the log file could not be opened, else | |
35 | * return 0. */ | |
36 | ||
37 | int open_log(void) | |
38 | { | |
39 | if (logfile) | |
40 | return 0; /* logfile already opened */ | |
41 | logfile = fopen(LogFilename, "a"); | |
42 | return logfile ? 0 : -1; | |
43 | } | |
44 | ||
45 | /* Close the log file. */ | |
46 | ||
47 | void close_log(void) | |
48 | { | |
49 | if (!logfile) | |
50 | return; | |
51 | fclose(logfile); | |
52 | logfile = NULL; | |
53 | } | |
54 | ||
55 | /* Log stuff to the log file with a datestamp. errno preserved. */ | |
56 | void log(const char *fmt, ...) | |
57 | { | |
58 | va_list args; | |
59 | time_t t; | |
60 | struct tm tm; | |
61 | char buf[256]; | |
62 | int errno_save = errno; | |
63 | ||
64 | va_start(args, fmt); | |
65 | time(&t); | |
66 | tm = *localtime(&t); | |
67 | #if HAVE_GETTIMEOFDAY | |
68 | if (verbose) | |
69 | { | |
70 | /* if verbose is on, we log the microsecond to make a more precise analysis */ | |
71 | char *s; | |
72 | struct timeval tv; | |
73 | gettimeofday(&tv, NULL); | |
74 | strftime(buf, sizeof(buf) - 1, "[%b %d %H:%M:%S", &tm); | |
75 | s = buf + strlen(buf); | |
76 | s += snprintf(s, sizeof(buf) - (s - buf), ".%06ld", tv.tv_usec); | |
77 | strftime(s, sizeof(buf) - (s - buf) - 1, " %Y] ", &tm); | |
78 | } | |
79 | else | |
80 | { | |
81 | #endif | |
82 | strftime(buf, sizeof(buf) - 1, "[%b %d %H:%M:%S %Y] ", &tm); | |
83 | #if HAVE_GETTIMEOFDAY | |
84 | } | |
85 | #endif | |
86 | if (logfile) | |
87 | { | |
88 | fputs(buf, logfile); | |
89 | vfprintf(logfile, fmt, args); | |
90 | fputc('\n', logfile); | |
91 | fflush(logfile); | |
92 | } | |
93 | if (debug) | |
94 | { /* log it to stderr */ | |
95 | fputs(buf, stderr); | |
96 | vfprintf(stderr, fmt, args); | |
97 | fputc('\n', stderr); | |
98 | } | |
99 | errno = errno_save; | |
100 | } | |
101 | ||
102 | /* Like log(), but tack a ": " and a system error message (as returned by | |
103 | * strerror()) onto the end. | |
104 | */ | |
105 | ||
106 | void log_perror(const char *fmt, ...) | |
107 | { | |
108 | va_list args; | |
109 | time_t t; | |
110 | struct tm tm; | |
111 | char buf[256]; | |
112 | int errno_save = errno; | |
113 | va_start(args, fmt); | |
114 | time(&t); | |
115 | tm = *localtime(&t); | |
116 | #if HAVE_GETTIMEOFDAY | |
117 | if (verbose) | |
118 | { | |
119 | char *s; | |
120 | struct timeval tv; | |
121 | gettimeofday(&tv, NULL); | |
122 | strftime(buf, sizeof(buf) - 1, "[%b %d %H:%M:%S", &tm); | |
123 | s = buf + strlen(buf); | |
124 | s += snprintf(s, sizeof(buf) - (s - buf), ".%06ld", tv.tv_usec); | |
125 | strftime(s, sizeof(buf) - (s - buf) - 1, " %Y] ", &tm); | |
126 | } | |
127 | else | |
128 | { | |
129 | #endif | |
130 | strftime(buf, sizeof(buf) - 1, "[%b %d %H:%M:%S %Y] ", &tm); | |
131 | #if HAVE_GETTIMEOFDAY | |
132 | } | |
133 | #endif | |
134 | if (logfile) | |
135 | { | |
136 | fputs(buf, logfile); | |
137 | vfprintf(logfile, fmt, args); | |
138 | fprintf(logfile, ": %s\n", strerror(errno_save)); | |
139 | fflush(logfile); | |
140 | } | |
141 | if (debug) | |
142 | { | |
143 | fputs(buf, stderr); | |
144 | vfprintf(stderr, fmt, args); | |
145 | fprintf(stderr, ": %s\n", strerror(errno_save)); | |
146 | } | |
147 | errno = errno_save; | |
148 | } | |
149 | ||
150 | /*************************************************************************/ | |
151 | ||
152 | /* We've hit something we can't recover from. Let people know what | |
153 | * happened, then go down. | |
154 | */ | |
155 | ||
156 | void fatal(const char *fmt, ...) | |
157 | { | |
158 | va_list args; | |
159 | time_t t; | |
160 | struct tm tm; | |
161 | char buf[256], buf2[4096]; | |
162 | ||
163 | log("IRC context : %s", inbuf); | |
164 | ||
165 | va_start(args, fmt); | |
166 | time(&t); | |
167 | tm = *localtime(&t); | |
168 | #if HAVE_GETTIMEOFDAY | |
169 | if (verbose) | |
170 | { | |
171 | char *s; | |
172 | struct timeval tv; | |
173 | gettimeofday(&tv, NULL); | |
174 | strftime(buf, sizeof(buf) - 1, "[%b %d %H:%M:%S", &tm); | |
175 | s = buf + strlen(buf); | |
176 | s += snprintf(s, sizeof(buf) - (s - buf), ".%06ld", tv.tv_usec); | |
177 | strftime(s, sizeof(buf) - (s - buf) - 1, " %Y] ", &tm); | |
178 | } | |
179 | else | |
180 | { | |
181 | #endif | |
182 | strftime(buf, sizeof(buf) - 1, "[%b %d %H:%M:%S %Y] ", &tm); | |
183 | #if HAVE_GETTIMEOFDAY | |
184 | } | |
185 | #endif | |
186 | vsnprintf(buf2, sizeof(buf2), fmt, args); | |
187 | if (logfile) | |
188 | fprintf(logfile, "%sFATAL: %s\n", buf, buf2); | |
189 | if (debug) | |
190 | fprintf(stderr, "%sFATAL: %s\n", buf, buf2); | |
191 | if (servsock >= 0) | |
192 | wallops(NULL, "FATAL ERROR! %s (IRC context : %s)", buf2, inbuf); | |
193 | exit(1); | |
194 | } | |
195 | ||
196 | ||
197 | /* Same thing, but do it like perror(). */ | |
198 | ||
199 | void fatal_perror(const char *fmt, ...) | |
200 | { | |
201 | va_list args; | |
202 | time_t t; | |
203 | struct tm tm; | |
204 | char buf[256], buf2[4096]; | |
205 | int errno_save = errno; | |
206 | ||
207 | log("IRC context : %s", inbuf); | |
208 | ||
209 | va_start(args, fmt); | |
210 | time(&t); | |
211 | tm = *localtime(&t); | |
212 | #if HAVE_GETTIMEOFDAY | |
213 | if (verbose) | |
214 | { | |
215 | char *s; | |
216 | struct timeval tv; | |
217 | gettimeofday(&tv, NULL); | |
218 | strftime(buf, sizeof(buf) - 1, "[%b %d %H:%M:%S", &tm); | |
219 | s = buf + strlen(buf); | |
220 | s += snprintf(s, sizeof(buf) - (s - buf), ".%06ld", tv.tv_usec); | |
221 | strftime(s, sizeof(buf) - (s - buf) - 1, " %Y] ", &tm); | |
222 | } | |
223 | else | |
224 | { | |
225 | #endif | |
226 | strftime(buf, sizeof(buf) - 1, "[%b %d %H:%M:%S %Y] ", &tm); | |
227 | #if HAVE_GETTIMEOFDAY | |
228 | } | |
229 | #endif | |
230 | vsnprintf(buf2, sizeof(buf2), fmt, args); | |
231 | if (logfile) | |
232 | fprintf(logfile, "%sFATAL: %s: %s\n", buf, buf2, | |
233 | strerror(errno_save)); | |
234 | if (debug) | |
235 | fprintf(stderr, "%sFATAL: %s: %s\n", buf, buf2, | |
236 | strerror(errno_save)); | |
237 | if (servsock >= 0) | |
238 | wallops(NULL, "FATAL ERROR! %s: %s (IRC context : %s)", buf2, | |
239 | strerror(errno_save), inbuf); | |
240 | exit(1); | |
241 | } | |
242 | ||
243 | /*************************************************************************/ |