- Added AC_CHECK_FUNC tests for the availability of
[privoxy.git] / errlog.c
1 const char errlog_rcs[] = "$Id: errlog.c,v 1.23 2001/11/07 00:02:13 steudten Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/errlog.c,v $
5  *
6  * Purpose     :  Log errors to a designated destination in an elegant,
7  *                printf-like fashion.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
10  *                IJBSWA team.  http://ijbswa.sourceforge.net
11  *
12  *                Based on the Internet Junkbuster originally written
13  *                by and Copyright (C) 1997 Anonymous Coders and 
14  *                Junkbusters Corporation.  http://www.junkbusters.com
15  *
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.
21  *
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.
27  *
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.
33  *
34  * Revisions   :
35  *    $Log: errlog.c,v $
36  *    Revision 1.23  2001/11/07 00:02:13  steudten
37  *    Add line number in error output for lineparsing for
38  *    actionsfile and configfile.
39  *    Special handling for CLF added.
40  *
41  *    Revision 1.22  2001/11/05 23:43:05  steudten
42  *    Add time+date to log files.
43  *
44  *    Revision 1.21  2001/10/25 03:40:47  david__schmidt
45  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
46  *    threads to call select() simultaneously.  So, it's time to do a real, live,
47  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
48  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
49  *
50  *    Revision 1.20  2001/09/16 23:04:34  jongfoster
51  *    Fixing a warning
52  *
53  *    Revision 1.19  2001/09/13 20:08:06  jongfoster
54  *    Adding support for LOG_LEVEL_CGI
55  *
56  *    Revision 1.18  2001/09/10 11:27:24  oes
57  *    Declaration of w32_socket_strerr now conditional
58  *
59  *    Revision 1.17  2001/09/10 10:17:13  oes
60  *    Removed unused variable; Fixed sprintf format
61  *
62  *    Revision 1.16  2001/07/30 22:08:36  jongfoster
63  *    Tidying up #defines:
64  *    - All feature #defines are now of the form FEATURE_xxx
65  *    - Permanently turned off WIN_GUI_EDIT
66  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
67  *
68  *    Revision 1.15  2001/07/29 17:41:10  jongfoster
69  *    Now prints thread ID for each message (pthreads only)
70  *
71  *    Revision 1.14  2001/07/19 19:03:48  haroon
72  *    - Added case for LOG_LEVEL_POPUPS
73  *
74  *    Revision 1.13  2001/07/13 13:58:58  oes
75  *     - Added case for LOG_LEVEL_DEANIMATE
76  *     - Removed all #ifdef PCRS
77  *
78  *    Revision 1.12  2001/06/09 10:55:28  jongfoster
79  *    Changing BUFSIZ ==> BUFFER_SIZE
80  *
81  *    Revision 1.11  2001/06/01 18:14:49  jongfoster
82  *    Changing the calls to strerr() to check HAVE_STRERR (which is defined
83  *    in config.h if appropriate) rather than the NO_STRERR macro.
84  *
85  *    Revision 1.10  2001/05/29 11:52:21  oes
86  *    Conditional compilation of w32_socket_error
87  *
88  *    Revision 1.9  2001/05/28 16:15:17  jongfoster
89  *    Improved reporting of errors under Win32.
90  *
91  *    Revision 1.8  2001/05/26 17:25:14  jongfoster
92  *    Added support for CLF (Common Log Format) and fixed LOG_LEVEL_LOG
93  *
94  *    Revision 1.7  2001/05/26 15:21:28  jongfoster
95  *    Activity animation in Win32 GUI now works even if debug==0
96  *
97  *    Revision 1.6  2001/05/25 21:55:08  jongfoster
98  *    Now cleans up properly on FATAL (removes taskbar icon etc)
99  *
100  *    Revision 1.5  2001/05/22 18:46:04  oes
101  *
102  *    - Enabled filtering banners by size rather than URL
103  *      by adding patterns that replace all standard banner
104  *      sizes with the "Junkbuster" gif to the re_filterfile
105  *
106  *    - Enabled filtering WebBugs by providing a pattern
107  *      which kills all 1x1 images
108  *
109  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
110  *      which is selected by the (nonstandard and therefore
111  *      capital) letter 'U' in the option string.
112  *      It causes the quantifiers to be ungreedy by default.
113  *      Appending a ? turns back to greedy (!).
114  *
115  *    - Added a new interceptor ijb-send-banner, which
116  *      sends back the "Junkbuster" gif. Without imagelist or
117  *      MSIE detection support, or if tinygif = 1, or the
118  *      URL isn't recognized as an imageurl, a lame HTML
119  *      explanation is sent instead.
120  *
121  *    - Added new feature, which permits blocking remote
122  *      script redirects and firing back a local redirect
123  *      to the browser.
124  *      The feature is conditionally compiled, i.e. it
125  *      can be disabled with --disable-fast-redirects,
126  *      plus it must be activated by a "fast-redirects"
127  *      line in the config file, has its own log level
128  *      and of course wants to be displayed by show-proxy-args
129  *      Note: Boy, all the #ifdefs in 1001 locations and
130  *      all the fumbling with configure.in and acconfig.h
131  *      were *way* more work than the feature itself :-(
132  *
133  *    - Because a generic redirect template was needed for
134  *      this, tinygif = 3 now uses the same.
135  *
136  *    - Moved GIFs, and other static HTTP response templates
137  *      to project.h
138  *
139  *    - Some minor fixes
140  *
141  *    - Removed some >400 CRs again (Jon, you really worked
142  *      a lot! ;-)
143  *
144  *    Revision 1.4  2001/05/21 19:32:54  jongfoster
145  *    Added another #ifdef _WIN_CONSOLE
146  *
147  *    Revision 1.3  2001/05/20 01:11:40  jongfoster
148  *    Added support for LOG_LEVEL_FATAL
149  *    Renamed LOG_LEVEL_FRC to LOG_LEVEL_FORCE,
150  *    and LOG_LEVEL_REF to LOG_LEVEL_RE_FILTER
151  *
152  *    Revision 1.2  2001/05/17 22:42:01  oes
153  *     - Cleaned CRLF's from the sources and related files
154  *     - Repaired logging for REF and FRC
155  *
156  *    Revision 1.1.1.1  2001/05/15 13:58:51  oes
157  *    Initial import of version 2.9.3 source tree
158  *
159  *
160  *********************************************************************/
161 \f
162
163 #include "config.h"
164 #include "miscutil.h"
165
166 #include <stdlib.h>
167 #include <stdio.h>
168 #include <stdarg.h>
169 #include <string.h>
170
171 #if !defined(_WIN32) && !defined(__OS2__)
172 #include <unistd.h>
173 #endif /* !defined(_WIN32) && !defined(__OS2__) */
174
175 #include <errno.h>
176 #include <assert.h>
177 #ifdef FEATURE_PTHREAD
178 #include <pthread.h>
179 #endif /* def FEATURE_PTHREAD */
180
181 #ifdef _WIN32
182 #include <windows.h>
183 #ifndef _WIN_CONSOLE
184 #include "w32log.h"
185 #endif /* ndef _WIN_CONSOLE */
186 #endif /* def _WIN32 */
187
188 #ifdef __OS2__
189 #define INCL_DOS
190 #include <os2.h>
191 #endif
192
193 #include "errlog.h"
194 #include "project.h"
195
196 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
197
198
199 /*
200  * LOG_LEVEL_FATAL cannot be turned off.  (There are
201  * some exceptional situations where we need to get a
202  * message to the user).
203  */
204 #define LOG_LEVEL_MINIMUM  LOG_LEVEL_FATAL
205
206 /* where to log (default: stderr) */
207 static FILE *logfp = NULL;
208
209 /* logging detail level.  */
210 static int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO);  
211
212 /* static functions */
213 static void fatal_error(const char * error_message);
214 #ifdef _WIN32
215 static char *w32_socket_strerr(int errcode, char *tmp_buf);
216 #endif
217
218 /*********************************************************************
219  *
220  * Function    :  fatal_error
221  *
222  * Description :  Displays a fatal error to standard error (or, on 
223  *                a WIN32 GUI, to a dialog box), and exits
224  *                JunkBuster with status code 1.
225  *
226  * Parameters  :
227  *          1  :  error_message = The error message to display.
228  *
229  * Returns     :  Does not return.
230  *
231  *********************************************************************/
232 static void fatal_error(const char * error_message)
233 {
234 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
235    MessageBox(g_hwndLogFrame, error_message, "Internet JunkBuster Error", 
236       MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);  
237
238    /* Cleanup - remove taskbar icon etc. */
239    TermLogWindow();
240
241 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
242    fputs(error_message, stderr);
243 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
244
245 #if defined(unix)
246    deletePidFile();
247 #endif /* unix */
248
249    exit(1);
250 }
251
252
253 /*********************************************************************
254  *
255  * Function    :  init_errlog
256  *
257  * Description :  Initializes the logging module.  Must call before
258  *                calling log_error.
259  *
260  * Parameters  :
261  *          1  :  prog_name  = The program name.
262  *          2  :  logfname   = The logfile name, or NULL for stderr.
263  *          3  :  debuglevel = The debugging level.
264  *
265  * Returns     :  N/A
266  *
267  *********************************************************************/
268 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
269 {
270    FILE *fp;
271
272    /* FIXME RACE HAZARD: should start critical section error_log_use here */
273
274    /* set the logging detail level */
275    debug = debuglevel | LOG_LEVEL_MINIMUM;
276
277    if ((logfp != NULL) && (logfp != stderr))
278    {
279       log_error(LOG_LEVEL_INFO, "(Re-)Open logfile %s", logfname ? logfname : "none");
280       fclose(logfp);
281    }
282    logfp = stderr;
283
284    /* set the designated log file */
285    if( logfname )
286    {
287       if( !(fp = fopen(logfname, "a")) )
288       {
289          log_error(LOG_LEVEL_FATAL, "init_errlog(): can't open logfile: %s", logfname);
290       }
291
292       /* set logging to be completely unbuffered */
293       setbuf(fp, NULL);
294
295       logfp = fp;
296    }
297
298    log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
299    if (prog_name != NULL)
300    {
301       log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
302    }
303
304    /* FIXME RACE HAZARD: should end critical section error_log_use here */
305
306 } /* init_error_log */
307
308
309 /*********************************************************************
310  *
311  * Function    :  log_error
312  *
313  * Description :  This is the error-reporting and logging function.
314  *
315  * Parameters  :
316  *          1  :  loglevel  = the type of message to be logged
317  *          2  :  fmt       = the main string we want logged, printf-like
318  *          3  :  ...       = arguments to be inserted in fmt (printf-like).
319  *
320  * Returns     :  N/A
321  *
322  *********************************************************************/
323 void log_error(int loglevel, char *fmt, ...)
324 {
325    va_list ap;
326    char *outbuf= NULL;
327    char *outbuf_save = NULL;
328    char * src = fmt;
329    int outc = 0;
330    long this_thread = 1;  /* was: pthread_t this_thread;*/
331 #ifdef __OS2__
332    PTIB     ptib;
333    APIRET   ulrc;
334 #endif /* __OS2__ */
335
336 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
337    /*
338     * Irrespective of debug setting, a GET/POST/CONNECT makes
339     * the taskbar icon animate.  (There is an option to disable
340     * this but checking that is handled inside LogShowActivity()).
341     */
342    if (loglevel == LOG_LEVEL_GPC)
343    {
344       LogShowActivity();
345    }
346 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
347
348    /* verify if loglevel applies to current settings and bail out if negative */
349    if ((loglevel & debug) == 0)
350    {
351       return;
352    }
353
354    /* FIXME get current thread id */
355 #ifdef FEATURE_PTHREAD
356    this_thread = (long)pthread_self();
357 #elif __OS2__
358   
359    ulrc = DosGetInfoBlocks(&ptib, NULL);
360    if (ulrc == 0)
361      this_thread = ptib -> tib_ptib2 -> tib2_ultid;
362 #endif /* def FEATURE_PTHREAD */
363
364    outbuf_save = outbuf = (char*)malloc(BUFFER_SIZE);
365    assert(outbuf);
366
367     {
368        /*
369         * Write timestamp into tempbuf.
370         *
371         * Complex because not all OSs have tm_gmtoff or
372         * the %z field in strftime()
373         */
374        time_t now; 
375        struct tm *tm_now; 
376        time (&now); 
377        tm_now = localtime (&now); 
378        strftime (outbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", tm_now); 
379        outbuf += strlen( outbuf );
380     }
381    switch (loglevel)
382    {
383       case LOG_LEVEL_ERROR:
384          outc = sprintf(outbuf, "IJB(%ld) Error: ", this_thread);
385          break;
386       case LOG_LEVEL_FATAL:
387          outc = sprintf(outbuf, "IJB(%ld) Fatal error: ", this_thread);
388          break;
389       case LOG_LEVEL_GPC:
390          outc = sprintf(outbuf, "IJB(%ld) Request: ", this_thread);
391          break;
392       case LOG_LEVEL_CONNECT:
393          outc = sprintf(outbuf, "IJB(%ld) Connect: ", this_thread);
394          break;
395       case LOG_LEVEL_LOG:
396          outc = sprintf(outbuf, "IJB(%ld) Writing: ", this_thread);
397          break;
398       case LOG_LEVEL_HEADER:
399          outc = sprintf(outbuf, "IJB(%ld) Header: ", this_thread);
400          break;
401       case LOG_LEVEL_INFO:
402          outc = sprintf(outbuf, "IJB(%ld) Info: ", this_thread);
403          break;
404       case LOG_LEVEL_RE_FILTER:
405          outc = sprintf(outbuf, "IJB(%ld) Re-Filter: ", this_thread);
406          break;
407 #ifdef FEATURE_FORCE_LOAD
408       case LOG_LEVEL_FORCE:
409          outc = sprintf(outbuf, "IJB(%ld) Force: ", this_thread);
410          break;
411 #endif /* def FEATURE_FORCE_LOAD */
412 #ifdef FEATURE_FAST_REDIRECTS
413       case LOG_LEVEL_REDIRECTS:
414          outc = sprintf(outbuf, "IJB(%ld) Redirect: ", this_thread);
415          break;
416 #endif /* def FEATURE_FAST_REDIRECTS */
417       case LOG_LEVEL_DEANIMATE:
418          outc = sprintf(outbuf, "IJB(%ld) Gif-Deanimate: ", this_thread);
419          break;
420       case LOG_LEVEL_CLF:
421          outbuf = outbuf_save;
422          outc = 0;
423          outbuf[0] = '\0';
424          break;
425 #ifdef FEATURE_KILL_POPUPS
426       case LOG_LEVEL_POPUPS:
427          outc = sprintf(outbuf, "IJB(%ld) Kill-Popups: ", this_thread);
428          break;
429 #endif /* def FEATURE_KILL_POPUPS */
430       case LOG_LEVEL_CGI:
431          outc = sprintf(outbuf, "IJB(%ld) CGI: ", this_thread);
432          break;
433       default:
434          outc = sprintf(outbuf, "IJB(%ld) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
435          break;
436    }
437    
438    /* get ready to scan var. args. */
439    va_start( ap, fmt );
440
441    /* build formatted message from fmt and var-args */
442    while ((*src) && (outc < BUFFER_SIZE-2))
443    {
444       char tempbuf[BUFFER_SIZE];
445       char *sval;
446       int ival;
447       unsigned uval;
448       long lval;
449       unsigned long ulval;
450       int oldoutc;
451       char ch;
452       
453       ch = *src++;
454       if( ch != '%' )
455       {
456          outbuf[outc++] = ch;
457          continue;
458       }
459
460       ch = *src++;
461       switch (ch) {
462          case '%':
463             outbuf[outc++] = '%';
464             break;
465          case 'd':
466             ival = va_arg( ap, int );
467             oldoutc = outc;
468             outc += sprintf(tempbuf, "%d", ival);
469             if (outc < BUFFER_SIZE-1) 
470             {
471                strcpy(outbuf + oldoutc, tempbuf);
472             }
473             else
474             {
475                outbuf[oldoutc] = '\0';
476             }
477             break;
478          case 'u':
479             uval = va_arg( ap, unsigned );
480             oldoutc = outc;
481             outc += sprintf(tempbuf, "%u", uval);
482             if (outc < BUFFER_SIZE-1) 
483             {
484                strcpy(outbuf + oldoutc, tempbuf);
485             }
486             else
487             {
488                outbuf[oldoutc] = '\0';
489             }
490             break;
491          case 'l':
492             /* this is a modifier that must be followed by u or d */
493             ch = *src++;
494             if (ch == 'd')
495             {
496                lval = va_arg( ap, long );
497                oldoutc = outc;
498                outc += sprintf(tempbuf, "%ld", lval);
499             }
500             else if (ch == 'u')
501             {
502                ulval = va_arg( ap, unsigned long );
503                oldoutc = outc;
504                outc += sprintf(tempbuf, "%lu", ulval);
505             }
506             else
507             {
508                /* Error */
509                sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
510                                "Format = \"%s\"\n"
511                                "Exiting.", this_thread, fmt);
512                /* FIXME RACE HAZARD: should start critical section error_log_use here */
513                if( !logfp )
514                {
515                   logfp = stderr;
516                }
517                fputs(outbuf, logfp);
518                /* FIXME RACE HAZARD: should end critical section error_log_use here */
519                fatal_error(outbuf);
520                /* Never get here */
521                break;
522             }
523             if (outc < BUFFER_SIZE-1) 
524             {
525                strcpy(outbuf + oldoutc, tempbuf);
526             }
527             else
528             {
529                outbuf[oldoutc] = '\0';
530             }
531             break;
532          case 'c':
533             /*
534              * Note that char paramaters are converted to int, so we need to
535              * pass "int" to va_arg.  (See K&R, 2nd ed, section A7.3.2, page 202)
536              */
537             outbuf[outc++] = (char) va_arg( ap, int );
538             break;
539          case 's':
540             sval = va_arg( ap, char * );
541             oldoutc = outc;
542             outc += strlen(sval);
543             if (outc < BUFFER_SIZE-1) 
544             {
545                strcpy(outbuf + oldoutc, sval);
546             }
547             else
548             {
549                outbuf[oldoutc] = '\0';
550             }
551             break;
552          case 'N':
553             /* Non-standard: Print a counted string.  Takes 2 parameters:
554              * int length, const char * string
555              */
556             ival = va_arg( ap, int );
557             sval = va_arg( ap, char * );
558             if (ival < 0)
559             {
560                ival = 0;
561             }
562             oldoutc = outc;
563             outc += ival;
564             if (outc < BUFFER_SIZE-1)
565             {
566                memcpy(outbuf + oldoutc, sval, ival);
567             }
568             else
569             {
570                outbuf[oldoutc] = '\0';
571             }
572             break;
573          case 'E':
574             /* Non-standard: Print error code from errno */
575 #ifdef _WIN32
576             ival = WSAGetLastError();
577             sval = w32_socket_strerr(ival, tempbuf);
578 #else /* ifndef _WIN32 */
579             ival = errno; 
580 #ifdef HAVE_STRERROR
581             sval = strerror(ival);
582 #else /* ifndef HAVE_STRERROR */
583             sval = NULL;
584 #endif /* ndef HAVE_STRERROR */
585             if (sval == NULL)
586             {
587                sprintf(tempbuf, "(errno = %d)", ival);
588                sval = tempbuf;
589             }
590 #endif /* ndef _WIN32 */
591             oldoutc = outc;
592             outc += strlen(sval);
593             if (outc < BUFFER_SIZE-1) 
594             {
595                strcpy(outbuf + oldoutc, sval);
596             }
597             else
598             {
599                outbuf[oldoutc] = '\0';
600             }
601             break;
602          case 'T':
603             /* Non-standard: Print a Common Log File timestamp */
604             {
605                /*
606                 * Write timestamp into tempbuf.
607                 *
608                 * Complex because not all OSs have tm_gmtoff or
609                 * the %z field in strftime()
610                 */
611                time_t now; 
612                struct tm *tm_now; 
613                struct tm gmt; 
614                int days, hrs, mins; 
615                time (&now); 
616                gmt = *gmtime (&now); 
617                tm_now = localtime (&now); 
618                days = tm_now->tm_yday - gmt.tm_yday; 
619                hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour); 
620                mins = hrs * 60 + tm_now->tm_min - gmt.tm_min; 
621                strftime (tempbuf, BUFFER_SIZE-6, "%d/%b/%Y:%H:%M:%S ", tm_now); 
622                sprintf (tempbuf + strlen(tempbuf), "%+03d%02d", mins / 60, abs(mins) % 60); 
623             }
624             oldoutc = outc;
625             outc += strlen(tempbuf);
626             if (outc < BUFFER_SIZE-1) 
627             {
628                strcpy(outbuf + oldoutc, tempbuf);
629             }
630             else
631             {
632                outbuf[oldoutc] = '\0';
633             }
634             break;
635          default:
636             sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
637                             "Format = \"%s\"\n"
638                             "Exiting.", this_thread, fmt);
639             /* FIXME RACE HAZARD: should start critical section error_log_use here */
640             if( !logfp )
641             {
642                logfp = stderr;
643             }
644             fputs(outbuf_save, logfp);
645             /* FIXME RACE HAZARD: should end critical section error_log_use here */
646             fatal_error(outbuf_save);
647             /* Never get here */
648             break;
649
650       } /* switch( p ) */
651
652    } /* for( p ... ) */
653    
654    /* done with var. args */
655    va_end( ap );
656    
657    if (outc >= BUFFER_SIZE-2)
658    {
659       /* insufficient room for newline and trailing null. */
660
661       static const char warning[] = "... [too long, truncated]\n";
662
663       if (outc < BUFFER_SIZE)
664       {
665          /* Need to add terminating null in this case. */
666          outbuf[outc] = '\0';
667       }
668
669       /* Truncate output */
670       outbuf[BUFFER_SIZE - sizeof(warning)] = '\0';
671
672       /* Append warning */
673       strcat(outbuf, warning);
674    }
675    else
676    {
677       /* Add terminating newline and null */
678       outbuf[outc++] = '\n';
679       outbuf[outc] = '\0';
680    }
681
682    /* FIXME RACE HAZARD: should start critical section error_log_use here */
683
684    /* deal with glibc stupidity - it won't let you initialize logfp */
685    if( !logfp )
686    {
687       logfp = stderr;
688    }
689
690    fputs(outbuf_save, logfp);
691
692    if (loglevel == LOG_LEVEL_FATAL)
693    {
694       fatal_error(outbuf_save);
695       /* Never get here */
696    }
697
698    /* FIXME RACE HAZARD: should end critical section error_log_use here */
699
700 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
701    /* Write to display */
702    LogPutString(outbuf_save);
703 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
704
705 }
706
707
708 #ifdef _WIN32
709 /*********************************************************************
710  *
711  * Function    :  w32_socket_strerr
712  *
713  * Description :  Translate the return value from WSAGetLastError()
714  *                into a string.
715  *
716  * Parameters  :
717  *          1  :  errcode = The return value from WSAGetLastError().
718  *          2  :  tmp_buf = A temporary buffer that might be used to
719  *                          store the string.
720  *
721  * Returns     :  String representing the error code.  This may be
722  *                a global string constant or a string stored in
723  *                tmp_buf.
724  *
725  *********************************************************************/
726 static char *w32_socket_strerr(int errcode, char *tmp_buf)
727 {
728 #define TEXT_FOR_ERROR(code,text) \
729    if (errcode == code)           \
730    {                              \
731       return #code " - " text;    \
732    }
733
734    TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
735    TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
736    TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
737    TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
738    TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
739    TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
740    TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
741    TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
742    TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
743    TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
744    TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
745    TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
746    TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
747    TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
748    TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
749    TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
750    TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
751    TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
752    TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
753    TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
754    TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
755    TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
756    TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
757    TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
758    TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
759    TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
760    TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
761    TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
762    TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
763    TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
764    TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
765    TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
766    TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
767    TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
768    TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
769    TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
770    TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
771    TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
772    TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
773    TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
774    TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
775    TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
776    /*
777     * The following error codes are documented in the Microsoft WinSock
778     * reference guide, but don't actually exist.
779     *
780     * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
781     * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
782     * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
783     * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
784     * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
785     * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
786     * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
787     * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
788     * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
789     * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
790     */
791
792    sprintf(tmp_buf, "(error number %d)", errcode);
793    return tmp_buf;
794 }
795 #endif /* def _WIN32 */
796
797
798 /*
799   Local Variables:
800   tab-width: 3
801   end:
802 */