]>
jfr.im git - irc/thales.git/blob - src/log.c
1 /* Thales - IRC to Relational Database Gateway
2 * Copyright (C) 2002 Lucas Nussbaum <lucas@lucas-nussbaum.net>
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.
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.
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
24 extern char *LogFilename
;
34 /* Open the log file. Return -1 if the log file could not be opened, else
40 return 0; /* logfile already opened */
41 logfile
= fopen(LogFilename
, "a");
42 return logfile
? 0 : -1;
45 /* Close the log file. */
55 /* Log stuff to the log file with a datestamp. errno preserved. */
56 void log(const char *fmt
, ...)
62 int errno_save
= errno
;
70 /* if verbose is on, we log the microsecond to make a more precise analysis */
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
);
82 strftime(buf
, sizeof(buf
) - 1, "[%b %d %H:%M:%S %Y] ", &tm
);
89 vfprintf(logfile
, fmt
, args
);
94 { /* log it to stderr */
96 vfprintf(stderr
, fmt
, args
);
102 /* Like log(), but tack a ": " and a system error message (as returned by
103 * strerror()) onto the end.
106 void log_perror(const char *fmt
, ...)
112 int errno_save
= errno
;
116 #if HAVE_GETTIMEOFDAY
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
);
130 strftime(buf
, sizeof(buf
) - 1, "[%b %d %H:%M:%S %Y] ", &tm
);
131 #if HAVE_GETTIMEOFDAY
137 vfprintf(logfile
, fmt
, args
);
138 fprintf(logfile
, ": %s\n", strerror(errno_save
));
144 vfprintf(stderr
, fmt
, args
);
145 fprintf(stderr
, ": %s\n", strerror(errno_save
));
150 /*************************************************************************/
152 /* We've hit something we can't recover from. Let people know what
153 * happened, then go down.
156 void fatal(const char *fmt
, ...)
161 char buf
[256], buf2
[4096];
163 log("IRC context : %s", inbuf
);
168 #if HAVE_GETTIMEOFDAY
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
);
182 strftime(buf
, sizeof(buf
) - 1, "[%b %d %H:%M:%S %Y] ", &tm
);
183 #if HAVE_GETTIMEOFDAY
186 vsnprintf(buf2
, sizeof(buf2
), fmt
, args
);
188 fprintf(logfile
, "%sFATAL: %s\n", buf
, buf2
);
190 fprintf(stderr
, "%sFATAL: %s\n", buf
, buf2
);
192 wallops(NULL
, "FATAL ERROR! %s (IRC context : %s)", buf2
, inbuf
);
197 /* Same thing, but do it like perror(). */
199 void fatal_perror(const char *fmt
, ...)
204 char buf
[256], buf2
[4096];
205 int errno_save
= errno
;
207 log("IRC context : %s", inbuf
);
212 #if HAVE_GETTIMEOFDAY
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
);
226 strftime(buf
, sizeof(buf
) - 1, "[%b %d %H:%M:%S %Y] ", &tm
);
227 #if HAVE_GETTIMEOFDAY
230 vsnprintf(buf2
, sizeof(buf2
), fmt
, args
);
232 fprintf(logfile
, "%sFATAL: %s: %s\n", buf
, buf2
,
233 strerror(errno_save
));
235 fprintf(stderr
, "%sFATAL: %s: %s\n", buf
, buf2
,
236 strerror(errno_save
));
238 wallops(NULL
, "FATAL ERROR! %s: %s (IRC context : %s)", buf2
,
239 strerror(errno_save
), inbuf
);
243 /*************************************************************************/