1 const char errlog_rcs[] = "$Id: errlog.c,v 1.2 2001/05/17 22:42:01 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/errlog.c,v $
6 * Purpose : Log errors to a designated destination in an elegant,
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * IJBSWA team. http://ijbswa.sourceforge.net
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 * Revision 1.2 2001/05/17 22:42:01 oes
37 * - Cleaned CRLF's from the sources and related files
38 * - Repaired logging for REF and FRC
40 * Revision 1.1.1.1 2001/05/15 13:58:51 oes
41 * Initial import of version 2.9.3 source tree
44 *********************************************************************/
56 #endif /* ndef _WIN32 */
59 /* #include <pthread.h> */
64 #endif /* def _WIN32 */
69 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
73 * LOG_LEVEL_FATAL, LOG_LEVEL_ERROR and LOG_LEVEL_INFO
\r
74 * cannot be turned off. (There are some exceptional situations
\r
75 * where we need to get a message to the user).
\r
77 * FIXME: Do we need LOG_LEVEL_INFO here?
\r
79 #define LOG_LEVEL_MINIMUM (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO)
81 /* where to log (default: stderr) */
82 static FILE *logfp = NULL;
84 /* where to log (NULL == stderr) */
85 static char * logfilename = NULL;
87 /* logging detail level. */
88 static int debug = LOG_LEVEL_MINIMUM;
90 static void fatal_error(const char * error_message);
\r
93 /*********************************************************************
\r
95 * Function : fatal_error
\r
97 * Description : Displays a fatal error to standard error (or, on
\r
98 * a WIN32 GUI, to a dialog box), and exits
\r
99 * JunkBuster with status code 1.
\r
102 * 1 : error_message = The error message to display.
\r
104 * Returns : Does not return.
\r
106 *********************************************************************/
\r
107 static void fatal_error(const char * error_message)
\r
109 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
\r
110 MessageBox(NULL, error_message, "Internet JunkBuster Error",
\r
111 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
\r
112 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
\r
113 fputs(error_message, stderr);
\r
114 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
\r
120 /*********************************************************************
122 * Function : init_errlog
124 * Description : Initializes the logging module. Must call before
128 * 1 : prog_name = The program name.
129 * 2 : logfname = The logfile name, or NULL for stderr.
130 * 3 : debuglevel = The debugging level.
134 *********************************************************************/
135 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
139 /* FIXME RACE HAZARD: should start critical section error_log_use here */
141 /* set the logging detail level */
142 debug = debuglevel | LOG_LEVEL_MINIMUM;
144 if ((logfp != NULL) && (logfp != stderr))
150 /* set the designated log file */
153 if( !(fp = fopen(logfname, "a")) )
155 log_error(LOG_LEVEL_FATAL, "init_errlog(): can't open logfile: %s", logfname);
158 /* set logging to be completely unbuffered */
164 log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
165 if (prog_name != NULL)
167 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
170 /* FIXME RACE HAZARD: should end critical section error_log_use here */
172 } /* init_error_log */
175 /*********************************************************************
177 * Function : log_error
179 * Description : This is the error-reporting and logging function.
182 * 1 : loglevel = the type of message to be logged
183 * 2 : fmt = the main string we want logged, printf-like
184 * 3 : ... = arguments to be inserted in fmt (printf-like).
188 *********************************************************************/
189 void log_error(int loglevel, char *fmt, ...)
195 long this_thread = 1; /* was: pthread_t this_thread;*/
197 /* verify if loglevel applies to current settings and bail out if negative */
198 if(!(loglevel & debug))
203 /* FIXME get current thread id */
204 /* this_thread = (long)pthread_self(); */
208 case LOG_LEVEL_ERROR:
\r
209 outc = sprintf(outbuf, "IJB(%d) Error: ", this_thread);
\r
211 case LOG_LEVEL_FATAL:
\r
212 outc = sprintf(outbuf, "IJB(%d) Fatal error: ", this_thread);
\r
215 outc = sprintf(outbuf, "IJB(%d) Request: ", this_thread);
217 case LOG_LEVEL_CONNECT:
218 outc = sprintf(outbuf, "IJB(%d) Connect: ", this_thread);
220 case LOG_LEVEL_HEADER:
221 outc = sprintf(outbuf, "IJB(%d) Header: ", this_thread);
223 case LOG_LEVEL_INFO:
\r
224 outc = sprintf(outbuf, "IJB(%d) Info: ", this_thread);
\r
226 case LOG_LEVEL_RE_FILTER:
227 outc = sprintf(outbuf, "IJB(%d) Re-Filter: ", this_thread);
229 case LOG_LEVEL_FORCE:
230 outc = sprintf(outbuf, "IJB(%d) Force: ", this_thread);
233 outc = sprintf(outbuf, "IJB(%d) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
237 /* get ready to scan var. args. */
240 /* build formatted message from fmt and var-args */
241 while ((*src) && (outc < BUFSIZ-2))
243 char tempbuf[BUFSIZ];
262 outbuf[outc++] = '%';
265 ival = va_arg( ap, int );
267 outc += sprintf(tempbuf, "%d", ival);
270 strcpy(outbuf + oldoutc, tempbuf);
274 outbuf[oldoutc] = '\0';
278 uval = va_arg( ap, unsigned );
280 outc += sprintf(tempbuf, "%u", uval);
283 strcpy(outbuf + oldoutc, tempbuf);
287 outbuf[oldoutc] = '\0';
291 /* this is a modifier that must be followed by u or d */
295 lval = va_arg( ap, long );
297 outc += sprintf(tempbuf, "%ld", lval);
301 ulval = va_arg( ap, unsigned long );
303 outc += sprintf(tempbuf, "%lu", ulval);
308 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
310 "Exiting.", this_thread, fmt);
311 /* FIXME RACE HAZARD: should start critical section error_log_use here */
316 fputs(outbuf, logfp);
317 /* FIXME RACE HAZARD: should end critical section error_log_use here */
318 fatal_error(outbuf);
\r
319 /* Never get here */
\r
324 strcpy(outbuf + oldoutc, tempbuf);
328 outbuf[oldoutc] = '\0';
333 * Note that char paramaters are converted to int, so we need to
334 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
336 outbuf[outc++] = (char) va_arg( ap, int );
339 sval = va_arg( ap, char * );
341 outc += strlen(sval);
344 strcpy(outbuf + oldoutc, sval);
348 outbuf[oldoutc] = '\0';
352 /* Non-standard: Print error code from errno */
355 sval = strerror(ival);
356 #else /* def NOSTRERROR */
358 #endif /* def NOSTRERROR */
361 sprintf(tempbuf, "(errno = %d)", ival);
365 outc += strlen(sval);
368 strcpy(outbuf + oldoutc, sval);
372 outbuf[oldoutc] = '\0';
376 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
378 "Exiting.", this_thread, fmt);
379 /* FIXME RACE HAZARD: should start critical section error_log_use here */
384 fputs(outbuf, logfp);
385 /* FIXME RACE HAZARD: should end critical section error_log_use here */
386 fatal_error(outbuf);
\r
387 /* Never get here */
\r
394 /* done with var. args */
397 if (outc >= BUFSIZ-2)
399 /* insufficient room for newline and trailing null. */
401 static const char warning[] = "... [too long, truncated]\n";
405 /* Need to add terminating null in this case. */
409 /* Truncate output */
410 outbuf[BUFSIZ - sizeof(warning)] = '\0';
413 strcat(outbuf, warning);
417 /* Add terminating newline and null */
418 outbuf[outc++] = '\n';
422 /* FIXME RACE HAZARD: should start critical section error_log_use here */
424 /* deal with glibc stupidity - it won't let you initialize logfp */
430 fputs(outbuf, logfp);
432 if (loglevel == LOG_LEVEL_FATAL)
\r
434 fatal_error(outbuf);
\r
435 /* Never get here */
\r
438 /* FIXME RACE HAZARD: should end critical section error_log_use here */
440 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
441 /* Write to display */
442 LogPutString(outbuf);
444 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */