1 const char errlog_rcs[] = "$Id: errlog.c,v 1.3 2001/05/20 01:11:40 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.3 2001/05/20 01:11:40 jongfoster
37 * Added support for LOG_LEVEL_FATAL
38 * Renamed LOG_LEVEL_FRC to LOG_LEVEL_FORCE,
39 * and LOG_LEVEL_REF to LOG_LEVEL_RE_FILTER
41 * Revision 1.2 2001/05/17 22:42:01 oes
42 * - Cleaned CRLF's from the sources and related files
43 * - Repaired logging for REF and FRC
45 * Revision 1.1.1.1 2001/05/15 13:58:51 oes
46 * Initial import of version 2.9.3 source tree
49 *********************************************************************/
61 #endif /* ndef _WIN32 */
64 /* #include <pthread.h> */
68 #ifndef _WIN_CONSOLE
\r
70 #endif /* ndef _WIN_CONSOLE */
\r
71 #endif /* def _WIN32 */
76 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
80 * LOG_LEVEL_FATAL, LOG_LEVEL_ERROR and LOG_LEVEL_INFO
\r
81 * cannot be turned off. (There are some exceptional situations
\r
82 * where we need to get a message to the user).
\r
84 * FIXME: Do we need LOG_LEVEL_INFO here?
\r
86 #define LOG_LEVEL_MINIMUM (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO)
88 /* where to log (default: stderr) */
89 static FILE *logfp = NULL;
91 /* where to log (NULL == stderr) */
92 static char * logfilename = NULL;
94 /* logging detail level. */
95 static int debug = LOG_LEVEL_MINIMUM;
97 static void fatal_error(const char * error_message);
\r
100 /*********************************************************************
\r
102 * Function : fatal_error
\r
104 * Description : Displays a fatal error to standard error (or, on
\r
105 * a WIN32 GUI, to a dialog box), and exits
\r
106 * JunkBuster with status code 1.
\r
109 * 1 : error_message = The error message to display.
\r
111 * Returns : Does not return.
\r
113 *********************************************************************/
\r
114 static void fatal_error(const char * error_message)
\r
116 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
\r
117 MessageBox(NULL, error_message, "Internet JunkBuster Error",
\r
118 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
\r
119 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
\r
120 fputs(error_message, stderr);
\r
121 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
\r
127 /*********************************************************************
129 * Function : init_errlog
131 * Description : Initializes the logging module. Must call before
135 * 1 : prog_name = The program name.
136 * 2 : logfname = The logfile name, or NULL for stderr.
137 * 3 : debuglevel = The debugging level.
141 *********************************************************************/
142 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
146 /* FIXME RACE HAZARD: should start critical section error_log_use here */
148 /* set the logging detail level */
149 debug = debuglevel | LOG_LEVEL_MINIMUM;
151 if ((logfp != NULL) && (logfp != stderr))
157 /* set the designated log file */
160 if( !(fp = fopen(logfname, "a")) )
162 log_error(LOG_LEVEL_FATAL, "init_errlog(): can't open logfile: %s", logfname);
165 /* set logging to be completely unbuffered */
171 log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
172 if (prog_name != NULL)
174 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
177 /* FIXME RACE HAZARD: should end critical section error_log_use here */
179 } /* init_error_log */
182 /*********************************************************************
184 * Function : log_error
186 * Description : This is the error-reporting and logging function.
189 * 1 : loglevel = the type of message to be logged
190 * 2 : fmt = the main string we want logged, printf-like
191 * 3 : ... = arguments to be inserted in fmt (printf-like).
195 *********************************************************************/
196 void log_error(int loglevel, char *fmt, ...)
202 long this_thread = 1; /* was: pthread_t this_thread;*/
204 /* verify if loglevel applies to current settings and bail out if negative */
205 if(!(loglevel & debug))
210 /* FIXME get current thread id */
211 /* this_thread = (long)pthread_self(); */
215 case LOG_LEVEL_ERROR:
\r
216 outc = sprintf(outbuf, "IJB(%d) Error: ", this_thread);
\r
218 case LOG_LEVEL_FATAL:
\r
219 outc = sprintf(outbuf, "IJB(%d) Fatal error: ", this_thread);
\r
222 outc = sprintf(outbuf, "IJB(%d) Request: ", this_thread);
224 case LOG_LEVEL_CONNECT:
225 outc = sprintf(outbuf, "IJB(%d) Connect: ", this_thread);
227 case LOG_LEVEL_HEADER:
228 outc = sprintf(outbuf, "IJB(%d) Header: ", this_thread);
230 case LOG_LEVEL_INFO:
\r
231 outc = sprintf(outbuf, "IJB(%d) Info: ", this_thread);
\r
233 case LOG_LEVEL_RE_FILTER:
234 outc = sprintf(outbuf, "IJB(%d) Re-Filter: ", this_thread);
236 case LOG_LEVEL_FORCE:
237 outc = sprintf(outbuf, "IJB(%d) Force: ", this_thread);
240 outc = sprintf(outbuf, "IJB(%d) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
244 /* get ready to scan var. args. */
247 /* build formatted message from fmt and var-args */
248 while ((*src) && (outc < BUFSIZ-2))
250 char tempbuf[BUFSIZ];
269 outbuf[outc++] = '%';
272 ival = va_arg( ap, int );
274 outc += sprintf(tempbuf, "%d", ival);
277 strcpy(outbuf + oldoutc, tempbuf);
281 outbuf[oldoutc] = '\0';
285 uval = va_arg( ap, unsigned );
287 outc += sprintf(tempbuf, "%u", uval);
290 strcpy(outbuf + oldoutc, tempbuf);
294 outbuf[oldoutc] = '\0';
298 /* this is a modifier that must be followed by u or d */
302 lval = va_arg( ap, long );
304 outc += sprintf(tempbuf, "%ld", lval);
308 ulval = va_arg( ap, unsigned long );
310 outc += sprintf(tempbuf, "%lu", ulval);
315 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
317 "Exiting.", this_thread, fmt);
318 /* FIXME RACE HAZARD: should start critical section error_log_use here */
323 fputs(outbuf, logfp);
324 /* FIXME RACE HAZARD: should end critical section error_log_use here */
325 fatal_error(outbuf);
\r
326 /* Never get here */
\r
331 strcpy(outbuf + oldoutc, tempbuf);
335 outbuf[oldoutc] = '\0';
340 * Note that char paramaters are converted to int, so we need to
341 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
343 outbuf[outc++] = (char) va_arg( ap, int );
346 sval = va_arg( ap, char * );
348 outc += strlen(sval);
351 strcpy(outbuf + oldoutc, sval);
355 outbuf[oldoutc] = '\0';
359 /* Non-standard: Print error code from errno */
362 sval = strerror(ival);
363 #else /* def NOSTRERROR */
365 #endif /* def NOSTRERROR */
368 sprintf(tempbuf, "(errno = %d)", ival);
372 outc += strlen(sval);
375 strcpy(outbuf + oldoutc, sval);
379 outbuf[oldoutc] = '\0';
383 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
385 "Exiting.", this_thread, fmt);
386 /* FIXME RACE HAZARD: should start critical section error_log_use here */
391 fputs(outbuf, logfp);
392 /* FIXME RACE HAZARD: should end critical section error_log_use here */
393 fatal_error(outbuf);
\r
394 /* Never get here */
\r
401 /* done with var. args */
404 if (outc >= BUFSIZ-2)
406 /* insufficient room for newline and trailing null. */
408 static const char warning[] = "... [too long, truncated]\n";
412 /* Need to add terminating null in this case. */
416 /* Truncate output */
417 outbuf[BUFSIZ - sizeof(warning)] = '\0';
420 strcat(outbuf, warning);
424 /* Add terminating newline and null */
425 outbuf[outc++] = '\n';
429 /* FIXME RACE HAZARD: should start critical section error_log_use here */
431 /* deal with glibc stupidity - it won't let you initialize logfp */
437 fputs(outbuf, logfp);
439 if (loglevel == LOG_LEVEL_FATAL)
\r
441 fatal_error(outbuf);
\r
442 /* Never get here */
\r
445 /* FIXME RACE HAZARD: should end critical section error_log_use here */
447 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
448 /* Write to display */
449 LogPutString(outbuf);
451 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */