1 const char errlog_rcs[] = "$Id: errlog.c,v 1.4 2001/05/21 19:32:54 jongfoster 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.4 2001/05/21 19:32:54 jongfoster
37 * Added another #ifdef _WIN_CONSOLE
39 * Revision 1.3 2001/05/20 01:11:40 jongfoster
40 * Added support for LOG_LEVEL_FATAL
41 * Renamed LOG_LEVEL_FRC to LOG_LEVEL_FORCE,
42 * and LOG_LEVEL_REF to LOG_LEVEL_RE_FILTER
44 * Revision 1.2 2001/05/17 22:42:01 oes
45 * - Cleaned CRLF's from the sources and related files
46 * - Repaired logging for REF and FRC
48 * Revision 1.1.1.1 2001/05/15 13:58:51 oes
49 * Initial import of version 2.9.3 source tree
52 *********************************************************************/
64 #endif /* ndef _WIN32 */
67 /* #include <pthread.h> */
73 #endif /* ndef _WIN_CONSOLE */
74 #endif /* def _WIN32 */
79 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
83 * LOG_LEVEL_FATAL, LOG_LEVEL_ERROR and LOG_LEVEL_INFO
84 * cannot be turned off. (There are some exceptional situations
85 * where we need to get a message to the user).
87 * FIXME: Do we need LOG_LEVEL_INFO here?
89 #define LOG_LEVEL_MINIMUM (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO)
91 /* where to log (default: stderr) */
92 static FILE *logfp = NULL;
94 /* where to log (NULL == stderr) */
95 static char * logfilename = NULL;
97 /* logging detail level. */
98 static int debug = LOG_LEVEL_MINIMUM;
100 static void fatal_error(const char * error_message);
103 /*********************************************************************
105 * Function : fatal_error
107 * Description : Displays a fatal error to standard error (or, on
108 * a WIN32 GUI, to a dialog box), and exits
109 * JunkBuster with status code 1.
112 * 1 : error_message = The error message to display.
114 * Returns : Does not return.
116 *********************************************************************/
117 static void fatal_error(const char * error_message)
119 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
120 MessageBox(NULL, error_message, "Internet JunkBuster Error",
121 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
122 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
123 fputs(error_message, stderr);
124 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
130 /*********************************************************************
132 * Function : init_errlog
134 * Description : Initializes the logging module. Must call before
138 * 1 : prog_name = The program name.
139 * 2 : logfname = The logfile name, or NULL for stderr.
140 * 3 : debuglevel = The debugging level.
144 *********************************************************************/
145 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
149 /* FIXME RACE HAZARD: should start critical section error_log_use here */
151 /* set the logging detail level */
152 debug = debuglevel | LOG_LEVEL_MINIMUM;
154 if ((logfp != NULL) && (logfp != stderr))
160 /* set the designated log file */
163 if( !(fp = fopen(logfname, "a")) )
165 log_error(LOG_LEVEL_FATAL, "init_errlog(): can't open logfile: %s", logfname);
168 /* set logging to be completely unbuffered */
174 log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
175 if (prog_name != NULL)
177 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
180 /* FIXME RACE HAZARD: should end critical section error_log_use here */
182 } /* init_error_log */
185 /*********************************************************************
187 * Function : log_error
189 * Description : This is the error-reporting and logging function.
192 * 1 : loglevel = the type of message to be logged
193 * 2 : fmt = the main string we want logged, printf-like
194 * 3 : ... = arguments to be inserted in fmt (printf-like).
198 *********************************************************************/
199 void log_error(int loglevel, char *fmt, ...)
205 long this_thread = 1; /* was: pthread_t this_thread;*/
207 /* verify if loglevel applies to current settings and bail out if negative */
208 if(!(loglevel & debug))
213 /* FIXME get current thread id */
214 /* this_thread = (long)pthread_self(); */
218 case LOG_LEVEL_ERROR:
219 outc = sprintf(outbuf, "IJB(%d) Error: ", this_thread);
221 case LOG_LEVEL_FATAL:
222 outc = sprintf(outbuf, "IJB(%d) Fatal error: ", this_thread);
225 outc = sprintf(outbuf, "IJB(%d) Request: ", this_thread);
227 case LOG_LEVEL_CONNECT:
228 outc = sprintf(outbuf, "IJB(%d) Connect: ", this_thread);
230 case LOG_LEVEL_HEADER:
231 outc = sprintf(outbuf, "IJB(%d) Header: ", this_thread);
234 outc = sprintf(outbuf, "IJB(%d) Info: ", this_thread);
237 case LOG_LEVEL_RE_FILTER:
238 outc = sprintf(outbuf, "IJB(%d) Re-Filter: ", this_thread);
240 #endif /* def PCRS */
242 case LOG_LEVEL_FORCE:
243 outc = sprintf(outbuf, "IJB(%d) Force: ", this_thread);
245 #endif /* def FORCE_LOAD */
246 #ifdef FAST_REDIRECTS
247 case LOG_LEVEL_REDIRECTS:
248 outc = sprintf(outbuf, "IJB(%d) Redirect: ", this_thread);
250 #endif /* def FAST_REDIRECTS */
252 outc = sprintf(outbuf, "IJB(%d) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
256 /* get ready to scan var. args. */
259 /* build formatted message from fmt and var-args */
260 while ((*src) && (outc < BUFSIZ-2))
262 char tempbuf[BUFSIZ];
281 outbuf[outc++] = '%';
284 ival = va_arg( ap, int );
286 outc += sprintf(tempbuf, "%d", ival);
289 strcpy(outbuf + oldoutc, tempbuf);
293 outbuf[oldoutc] = '\0';
297 uval = va_arg( ap, unsigned );
299 outc += sprintf(tempbuf, "%u", uval);
302 strcpy(outbuf + oldoutc, tempbuf);
306 outbuf[oldoutc] = '\0';
310 /* this is a modifier that must be followed by u or d */
314 lval = va_arg( ap, long );
316 outc += sprintf(tempbuf, "%ld", lval);
320 ulval = va_arg( ap, unsigned long );
322 outc += sprintf(tempbuf, "%lu", ulval);
327 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
329 "Exiting.", this_thread, fmt);
330 /* FIXME RACE HAZARD: should start critical section error_log_use here */
335 fputs(outbuf, logfp);
336 /* FIXME RACE HAZARD: should end critical section error_log_use here */
343 strcpy(outbuf + oldoutc, tempbuf);
347 outbuf[oldoutc] = '\0';
352 * Note that char paramaters are converted to int, so we need to
353 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
355 outbuf[outc++] = (char) va_arg( ap, int );
358 sval = va_arg( ap, char * );
360 outc += strlen(sval);
363 strcpy(outbuf + oldoutc, sval);
367 outbuf[oldoutc] = '\0';
371 /* Non-standard: Print error code from errno */
374 sval = strerror(ival);
375 #else /* def NOSTRERROR */
377 #endif /* def NOSTRERROR */
380 sprintf(tempbuf, "(errno = %d)", ival);
384 outc += strlen(sval);
387 strcpy(outbuf + oldoutc, sval);
391 outbuf[oldoutc] = '\0';
395 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
397 "Exiting.", this_thread, fmt);
398 /* FIXME RACE HAZARD: should start critical section error_log_use here */
403 fputs(outbuf, logfp);
404 /* FIXME RACE HAZARD: should end critical section error_log_use here */
413 /* done with var. args */
416 if (outc >= BUFSIZ-2)
418 /* insufficient room for newline and trailing null. */
420 static const char warning[] = "... [too long, truncated]\n";
424 /* Need to add terminating null in this case. */
428 /* Truncate output */
429 outbuf[BUFSIZ - sizeof(warning)] = '\0';
432 strcat(outbuf, warning);
436 /* Add terminating newline and null */
437 outbuf[outc++] = '\n';
441 /* FIXME RACE HAZARD: should start critical section error_log_use here */
443 /* deal with glibc stupidity - it won't let you initialize logfp */
449 fputs(outbuf, logfp);
451 if (loglevel == LOG_LEVEL_FATAL)
457 /* FIXME RACE HAZARD: should end critical section error_log_use here */
459 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
460 /* Write to display */
461 LogPutString(outbuf);
463 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */