1 const char errlog_rcs[] = "$Id: errlog.c,v 1.1 2001/05/13 21:57:06 administrator Exp $";
2 /*********************************************************************
4 * File : $Source: /home/administrator/cvs/ijb/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.
37 *********************************************************************/
49 #endif /* ndef _WIN32 */
52 /* #include <pthread.h> */
57 #endif /* def _WIN32 */
62 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
64 /* LOG_LEVEL_ERROR and LOG_LEVEL_INFO cannot be turned off */
65 #define LOG_LEVEL_MINIMUM (LOG_LEVEL_ERROR | LOG_LEVEL_INFO)
67 /* where to log (default: stderr) */
68 static FILE *logfp = NULL;
70 /* where to log (NULL == stderr) */
71 static char * logfilename = NULL;
73 /* logging detail level. */
74 static int debug = LOG_LEVEL_MINIMUM;
77 /*********************************************************************
79 * Function : init_errlog
81 * Description : Initializes the logging module. Must call before
85 * 1 : prog_name = The program name.
86 * 2 : logfname = The logfile name, or NULL for stderr.
87 * 3 : debuglevel = The debugging level.
91 *********************************************************************/
92 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
96 /* FIXME RACE HAZARD: should start critical section error_log_use here */
98 /* set the logging detail level */
99 debug = debuglevel | LOG_LEVEL_MINIMUM;
101 if ((logfp != NULL) && (logfp != stderr))
107 /* set the designated log file */
110 if( !(fp = fopen(logfname, "a")) )
112 log_error(LOG_LEVEL_ERROR, "init_errlog(): can't open logfile: %s", logfname);
113 #if defined(_WIN32) && ! defined (_WIN_CONSOLE)
114 MessageBox(NULL, "init_errlog(): can't open logfile", "Internet JunkBuster Error",
115 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
116 #endif /* defined(_WIN32) && ! defined (_WIN_CONSOLE) */
120 /* set logging to be completely unbuffered */
126 log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
127 if (prog_name != NULL)
129 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
132 /* FIXME RACE HAZARD: should end critical section error_log_use here */
134 } /* init_error_log */
137 /*********************************************************************
139 * Function : log_error
141 * Description : This is the error-reporting and logging function.
144 * 1 : loglevel = the type of message to be logged
145 * 2 : fmt = the main string we want logged, printf-like
146 * 3 : ... = arguments to be inserted in fmt (printf-like).
150 *********************************************************************/
151 void log_error(int loglevel, char *fmt, ...)
157 long this_thread = 1; /* was: pthread_t this_thread;*/
159 /* verify if loglevel applies to current settings and bail out if negative */
160 if(!(loglevel & debug))
165 /* FIXME get current thread id */
166 /* this_thread = (long)pthread_self(); */
170 case LOG_LEVEL_ERROR:
171 outc = sprintf(outbuf, "IJB(%d) Error: ", this_thread);
174 outc = sprintf(outbuf, "IJB(%d) Request: ", this_thread);
176 case LOG_LEVEL_CONNECT:
177 outc = sprintf(outbuf, "IJB(%d) Connect: ", this_thread);
179 case LOG_LEVEL_HEADER:
180 outc = sprintf(outbuf, "IJB(%d) Header: ", this_thread);
183 outc = sprintf(outbuf, "IJB(%d) Info: ", this_thread);
186 outc = sprintf(outbuf, "IJB(%d) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
190 /* get ready to scan var. args. */
193 /* build formatted message from fmt and var-args */
194 while ((*src) && (outc < BUFSIZ-2))
196 char tempbuf[BUFSIZ];
215 outbuf[outc++] = '%';
218 ival = va_arg( ap, int );
220 outc += sprintf(tempbuf, "%d", ival);
223 strcpy(outbuf + oldoutc, tempbuf);
227 outbuf[oldoutc] = '\0';
231 uval = va_arg( ap, unsigned );
233 outc += sprintf(tempbuf, "%u", uval);
236 strcpy(outbuf + oldoutc, tempbuf);
240 outbuf[oldoutc] = '\0';
244 /* this is a modifier that must be followed by u or d */
248 lval = va_arg( ap, long );
250 outc += sprintf(tempbuf, "%ld", lval);
254 ulval = va_arg( ap, unsigned long );
256 outc += sprintf(tempbuf, "%lu", ulval);
261 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
263 "Exiting.", this_thread, fmt);
264 /* FIXME RACE HAZARD: should start critical section error_log_use here */
269 fputs(outbuf, logfp);
270 /* FIXME RACE HAZARD: should end critical section error_log_use here */
271 #if defined(_WIN32) && ! defined (_WIN_CONSOLE)
272 MessageBox(NULL, outbuf, "Internet JunkBuster Error",
273 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
274 #endif /* defined(_WIN32) && ! defined (_WIN_CONSOLE) */
280 strcpy(outbuf + oldoutc, tempbuf);
284 outbuf[oldoutc] = '\0';
289 * Note that char paramaters are converted to int, so we need to
290 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
292 outbuf[outc++] = (char) va_arg( ap, int );
295 sval = va_arg( ap, char * );
297 outc += strlen(sval);
300 strcpy(outbuf + oldoutc, sval);
304 outbuf[oldoutc] = '\0';
308 /* Non-standard: Print error code from errno */
311 sval = strerror(ival);
312 #else /* def NOSTRERROR */
314 #endif /* def NOSTRERROR */
317 sprintf(tempbuf, "(errno = %d)", ival);
321 outc += strlen(sval);
324 strcpy(outbuf + oldoutc, sval);
328 outbuf[oldoutc] = '\0';
332 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
334 "Exiting.", this_thread, fmt);
335 /* FIXME RACE HAZARD: should start critical section error_log_use here */
340 fputs(outbuf, logfp);
341 /* FIXME RACE HAZARD: should end critical section error_log_use here */
342 #if defined(_WIN32) && ! defined (_WIN_CONSOLE)
343 MessageBox(NULL, outbuf, "Internet JunkBuster Error",
344 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
345 #endif /* defined(_WIN32) && ! defined (_WIN_CONSOLE) */
353 /* done with var. args */
356 if (outc >= BUFSIZ-2)
358 /* insufficient room for newline and trailing null. */
360 static const char warning[] = "... [too long, truncated]\n";
364 /* Need to add terminating null in this case. */
368 /* Truncate output */
369 outbuf[BUFSIZ - sizeof(warning)] = '\0';
372 strcat(outbuf, warning);
376 /* Add terminating newline and null */
377 outbuf[outc++] = '\n';
381 /* FIXME RACE HAZARD: should start critical section error_log_use here */
383 /* deal with glibc stupidity - it won't let you initialize logfp */
389 fputs(outbuf, logfp);
391 /* FIXME RACE HAZARD: should end critical section error_log_use here */
393 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
394 /* Write to display */
395 LogPutString(outbuf);
397 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */