1 const char errlog_rcs[] = "$Id: errlog.c,v 1.1.1.1 2001/05/15 13:58:51 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.1.1.1 2001/05/15 13:58:51 oes
37 * Initial import of version 2.9.3 source tree
40 *********************************************************************/
52 #endif /* ndef _WIN32 */
55 /* #include <pthread.h> */
60 #endif /* def _WIN32 */
65 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
67 /* LOG_LEVEL_ERROR and LOG_LEVEL_INFO cannot be turned off. FIXME: Why?*/
68 #define LOG_LEVEL_MINIMUM (LOG_LEVEL_ERROR | LOG_LEVEL_INFO)
70 /* where to log (default: stderr) */
71 static FILE *logfp = NULL;
73 /* where to log (NULL == stderr) */
74 static char * logfilename = NULL;
76 /* logging detail level. */
77 static int debug = LOG_LEVEL_MINIMUM;
80 /*********************************************************************
82 * Function : init_errlog
84 * Description : Initializes the logging module. Must call before
88 * 1 : prog_name = The program name.
89 * 2 : logfname = The logfile name, or NULL for stderr.
90 * 3 : debuglevel = The debugging level.
94 *********************************************************************/
95 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
99 /* FIXME RACE HAZARD: should start critical section error_log_use here */
101 /* set the logging detail level */
102 debug = debuglevel | LOG_LEVEL_MINIMUM;
104 if ((logfp != NULL) && (logfp != stderr))
110 /* set the designated log file */
113 if( !(fp = fopen(logfname, "a")) )
115 log_error(LOG_LEVEL_ERROR, "init_errlog(): can't open logfile: %s", logfname);
116 #if defined(_WIN32) && ! defined (_WIN_CONSOLE)
117 MessageBox(NULL, "init_errlog(): can't open logfile", "Internet JunkBuster Error",
118 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
119 #endif /* defined(_WIN32) && ! defined (_WIN_CONSOLE) */
123 /* set logging to be completely unbuffered */
129 log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
130 if (prog_name != NULL)
132 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
135 /* FIXME RACE HAZARD: should end critical section error_log_use here */
137 } /* init_error_log */
140 /*********************************************************************
142 * Function : log_error
144 * Description : This is the error-reporting and logging function.
147 * 1 : loglevel = the type of message to be logged
148 * 2 : fmt = the main string we want logged, printf-like
149 * 3 : ... = arguments to be inserted in fmt (printf-like).
153 *********************************************************************/
154 void log_error(int loglevel, char *fmt, ...)
160 long this_thread = 1; /* was: pthread_t this_thread;*/
162 /* verify if loglevel applies to current settings and bail out if negative */
163 if(!(loglevel & debug))
168 /* FIXME get current thread id */
169 /* this_thread = (long)pthread_self(); */
173 case LOG_LEVEL_ERROR:
174 outc = sprintf(outbuf, "IJB(%d) Error: ", this_thread);
177 outc = sprintf(outbuf, "IJB(%d) Request: ", this_thread);
179 case LOG_LEVEL_CONNECT:
180 outc = sprintf(outbuf, "IJB(%d) Connect: ", this_thread);
182 case LOG_LEVEL_HEADER:
183 outc = sprintf(outbuf, "IJB(%d) Header: ", this_thread);
186 outc = sprintf(outbuf, "IJB(%d) Info: ", this_thread);
189 outc = sprintf(outbuf, "IJB(%d) Re-Filter: ", this_thread);
192 outc = sprintf(outbuf, "IJB(%d) Force: ", this_thread);
195 outc = sprintf(outbuf, "IJB(%d) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
199 /* get ready to scan var. args. */
202 /* build formatted message from fmt and var-args */
203 while ((*src) && (outc < BUFSIZ-2))
205 char tempbuf[BUFSIZ];
224 outbuf[outc++] = '%';
227 ival = va_arg( ap, int );
229 outc += sprintf(tempbuf, "%d", ival);
232 strcpy(outbuf + oldoutc, tempbuf);
236 outbuf[oldoutc] = '\0';
240 uval = va_arg( ap, unsigned );
242 outc += sprintf(tempbuf, "%u", uval);
245 strcpy(outbuf + oldoutc, tempbuf);
249 outbuf[oldoutc] = '\0';
253 /* this is a modifier that must be followed by u or d */
257 lval = va_arg( ap, long );
259 outc += sprintf(tempbuf, "%ld", lval);
263 ulval = va_arg( ap, unsigned long );
265 outc += sprintf(tempbuf, "%lu", ulval);
270 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
272 "Exiting.", this_thread, fmt);
273 /* FIXME RACE HAZARD: should start critical section error_log_use here */
278 fputs(outbuf, logfp);
279 /* FIXME RACE HAZARD: should end critical section error_log_use here */
280 #if defined(_WIN32) && ! defined (_WIN_CONSOLE)
281 MessageBox(NULL, outbuf, "Internet JunkBuster Error",
282 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
283 #endif /* defined(_WIN32) && ! defined (_WIN_CONSOLE) */
289 strcpy(outbuf + oldoutc, tempbuf);
293 outbuf[oldoutc] = '\0';
298 * Note that char paramaters are converted to int, so we need to
299 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
301 outbuf[outc++] = (char) va_arg( ap, int );
304 sval = va_arg( ap, char * );
306 outc += strlen(sval);
309 strcpy(outbuf + oldoutc, sval);
313 outbuf[oldoutc] = '\0';
317 /* Non-standard: Print error code from errno */
320 sval = strerror(ival);
321 #else /* def NOSTRERROR */
323 #endif /* def NOSTRERROR */
326 sprintf(tempbuf, "(errno = %d)", ival);
330 outc += strlen(sval);
333 strcpy(outbuf + oldoutc, sval);
337 outbuf[oldoutc] = '\0';
341 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
343 "Exiting.", this_thread, fmt);
344 /* FIXME RACE HAZARD: should start critical section error_log_use here */
349 fputs(outbuf, logfp);
350 /* FIXME RACE HAZARD: should end critical section error_log_use here */
351 #if defined(_WIN32) && ! defined (_WIN_CONSOLE)
352 MessageBox(NULL, outbuf, "Internet JunkBuster Error",
353 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
354 #endif /* defined(_WIN32) && ! defined (_WIN_CONSOLE) */
362 /* done with var. args */
365 if (outc >= BUFSIZ-2)
367 /* insufficient room for newline and trailing null. */
369 static const char warning[] = "... [too long, truncated]\n";
373 /* Need to add terminating null in this case. */
377 /* Truncate output */
378 outbuf[BUFSIZ - sizeof(warning)] = '\0';
381 strcat(outbuf, warning);
385 /* Add terminating newline and null */
386 outbuf[outc++] = '\n';
390 /* FIXME RACE HAZARD: should start critical section error_log_use here */
392 /* deal with glibc stupidity - it won't let you initialize logfp */
398 fputs(outbuf, logfp);
400 /* FIXME RACE HAZARD: should end critical section error_log_use here */
402 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
403 /* Write to display */
404 LogPutString(outbuf);
406 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */