Rename LOG_LEVEL_GPC to LOG_LEVEL_REQUEST
[privoxy.git] / errlog.c
index e094ddd..05a2c82 100644 (file)
--- a/errlog.c
+++ b/errlog.c
@@ -1,4 +1,3 @@
-const char errlog_rcs[] = "$Id: errlog.c,v 1.114 2012/03/09 17:55:50 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/errlog.c,v $
@@ -6,8 +5,8 @@ const char errlog_rcs[] = "$Id: errlog.c,v 1.114 2012/03/09 17:55:50 fabiankeil
  * Purpose     :  Log errors to a designated destination in an elegant,
  *                printf-like fashion.
  *
- * Copyright   :  Written by and Copyright (C) 2001-2010 the
- *                Privoxy team. http://www.privoxy.org/
+ * Copyright   :  Written by and Copyright (C) 2001-2014 the
+ *                Privoxy team. https://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
  *                by and Copyright (C) 1997 Anonymous Coders and
@@ -75,9 +74,9 @@ const char errlog_rcs[] = "$Id: errlog.c,v 1.114 2012/03/09 17:55:50 fabiankeil
 #include "errlog.h"
 #include "project.h"
 #include "jcc.h"
-
-const char errlog_h_rcs[] = ERRLOG_H_VERSION;
-
+#ifdef FEATURE_EXTERNAL_FILTERS
+#include "jbsockets.h"
+#endif
 
 /*
  * LOG_LEVEL_FATAL cannot be turned off.  (There are
@@ -146,25 +145,27 @@ static inline void unlock_loginit() {}
  *********************************************************************/
 static void fatal_error(const char *error_message)
 {
-#if defined(_WIN32) && !defined(_WIN_CONSOLE)
-   /* Skip timestamp and thread id for the message box. */
-   const char *box_message = strstr(error_message, "Fatal error");
-   if (NULL == box_message)
+   if (logfp != NULL)
    {
-      /* Shouldn't happen but ... */
-      box_message = error_message;
+      fputs(error_message, logfp);
    }
-   MessageBox(g_hwndLogFrame, box_message, "Privoxy Error",
-      MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
 
-   /* Cleanup - remove taskbar icon etc. */
-   TermLogWindow();
-#endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
-
-   if (logfp != NULL)
+#if defined(_WIN32) && !defined(_WIN_CONSOLE)
    {
-      fputs(error_message, logfp);
+      /* Skip timestamp and thread id for the message box. */
+      const char *box_message = strstr(error_message, "Fatal error");
+      if (NULL == box_message)
+      {
+         /* Shouldn't happen but ... */
+         box_message = error_message;
+      }
+      MessageBox(g_hwndLogFrame, box_message, "Privoxy Error",
+         MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
+
+      /* Cleanup - remove taskbar icon etc. */
+      TermLogWindow();
    }
+#endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
 
 #if defined(unix)
    if (pidfile)
@@ -240,6 +241,17 @@ void init_log_module(void)
  *********************************************************************/
 void set_debug_level(int debug_level)
 {
+#ifdef FUZZ
+   if (LOG_LEVEL_STFU == debug_level)
+   {
+      debug = LOG_LEVEL_STFU;
+   }
+   if (LOG_LEVEL_STFU == debug)
+   {
+      return;
+   }
+#endif
+
    debug = debug_level | LOG_LEVEL_MINIMUM;
 }
 
@@ -352,6 +364,10 @@ void init_error_log(const char *prog_name, const char *logfname)
       log_error(LOG_LEVEL_FATAL, "init_error_log(): can't open logfile: \'%s\'", logfname);
    }
 
+#ifdef FEATURE_EXTERNAL_FILTERS
+   mark_socket_for_close_on_execute(3);
+#endif
+
    /* set logging to be completely unbuffered */
    setbuf(fp, NULL);
 
@@ -400,7 +416,7 @@ void init_error_log(const char *prog_name, const char *logfname)
  *********************************************************************/
 static long get_thread_id(void)
 {
-   long this_thread = 1;  /* was: pthread_t this_thread;*/
+   long this_thread;
 
 #ifdef __OS2__
    PTIB     ptib;
@@ -412,12 +428,11 @@ static long get_thread_id(void)
    this_thread = (long)pthread_self();
 #ifdef __MACH__
    /*
-    * Mac OSX (and perhaps other Mach instances) doesn't have a debuggable
-    * value at the first 4 bytes of pthread_self()'s return value, a pthread_t.
-    * pthread_t is supposed to be opaque... but it's fairly random, though, so
-    * we make it mostly presentable.
+    * Mac OSX (and perhaps other Mach instances) doesn't have a unique
+    * value at the lowest order 4 bytes of pthread_self()'s return value, a pthread_t,
+    * so trim the three lowest-order bytes from the value (16^3).
     */
-   this_thread = abs(this_thread % 1000);
+   this_thread = this_thread / 4096;
 #endif /* def __MACH__ */
 #elif defined(_WIN32)
    this_thread = GetCurrentThreadId();
@@ -425,6 +440,9 @@ static long get_thread_id(void)
    ulrc = DosGetInfoBlocks(&ptib, NULL);
    if (ulrc == 0)
      this_thread = ptib -> tib_ptib2 -> tib2_ultid;
+#else
+   /* Forking instead of threading. */
+   this_thread = 1;
 #endif /* def FEATURE_PTHREAD */
 
    return this_thread;
@@ -583,7 +601,7 @@ static inline const char *get_log_level_string(int loglevel)
       case LOG_LEVEL_FATAL:
          log_level_string = "Fatal error";
          break;
-      case LOG_LEVEL_GPC:
+      case LOG_LEVEL_REQUEST:
          log_level_string = "Request";
          break;
       case LOG_LEVEL_CONNECT:
@@ -621,6 +639,9 @@ static inline const char *get_log_level_string(int loglevel)
       case LOG_LEVEL_CGI:
          log_level_string = "CGI";
          break;
+      case LOG_LEVEL_ACTIONS:
+         log_level_string = "Actions";
+         break;
       default:
          log_level_string = "Unknown log level";
          break;
@@ -631,6 +652,7 @@ static inline const char *get_log_level_string(int loglevel)
 }
 
 
+#define LOG_BUFFER_SIZE BUFFER_SIZE
 /*********************************************************************
  *
  * Function    :  log_error
@@ -648,19 +670,13 @@ static inline const char *get_log_level_string(int loglevel)
 void log_error(int loglevel, const char *fmt, ...)
 {
    va_list ap;
-   char *outbuf = NULL;
-   static char *outbuf_save = NULL;
-   char tempbuf[BUFFER_SIZE];
+   char outbuf[LOG_BUFFER_SIZE+1];
+   char tempbuf[LOG_BUFFER_SIZE];
    size_t length = 0;
    const char * src = fmt;
    long thread_id;
    char timestamp[30];
-   /*
-    * XXX: Make this a config option,
-    * why else do we allocate instead of using
-    * an array?
-    */
-   size_t log_buffer_size = BUFFER_SIZE;
+   const size_t log_buffer_size = LOG_BUFFER_SIZE;
 
 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
    /*
@@ -668,7 +684,7 @@ void log_error(int loglevel, const char *fmt, ...)
     * the taskbar icon animate.  (There is an option to disable
     * this but checking that is handled inside LogShowActivity()).
     */
-   if ((loglevel == LOG_LEVEL_GPC) || (loglevel == LOG_LEVEL_CRUNCH))
+   if ((loglevel == LOG_LEVEL_REQUEST) || (loglevel == LOG_LEVEL_CRUNCH))
    {
       LogShowActivity();
    }
@@ -685,6 +701,12 @@ void log_error(int loglevel, const char *fmt, ...)
 #endif
       )
    {
+#ifdef FUZZ
+      if (debug == LOG_LEVEL_STFU)
+      {
+         return;
+      }
+#endif
       if (loglevel == LOG_LEVEL_FATAL)
       {
          fatal_error("Fatal error. You're not supposed to"
@@ -696,28 +718,11 @@ void log_error(int loglevel, const char *fmt, ...)
    thread_id = get_thread_id();
    get_log_timestamp(timestamp, sizeof(timestamp));
 
-   /* protect the whole function because of the static buffer (outbuf) */
-   lock_logfile();
-
-   if (NULL == outbuf_save)
-   {
-      outbuf_save = (char*)zalloc(log_buffer_size + 1); /* +1 for paranoia */
-      if (NULL == outbuf_save)
-      {
-         snprintf(tempbuf, sizeof(tempbuf),
-            "%s %08lx Fatal error: Out of memory in log_error().",
-            timestamp, thread_id);
-         fatal_error(tempbuf); /* Exit */
-         return;
-      }
-   }
-   outbuf = outbuf_save;
-
    /*
     * Memsetting the whole buffer to zero (in theory)
     * makes things easier later on.
     */
-   memset(outbuf, 0, log_buffer_size);
+   memset(outbuf, 0, sizeof(outbuf));
 
    /* Add prefix for everything but Common Log Format messages */
    if (loglevel != LOG_LEVEL_CLF)
@@ -794,7 +799,7 @@ void log_error(int loglevel, const char *fmt, ...)
             break;
          case 'c':
             /*
-             * Note that char paramaters are converted to int, so we need to
+             * Note that char parameters are converted to int, so we need to
              * pass "int" to va_arg.  (See K&R, 2nd ed, section A7.3.2, page 202)
              */
             tempbuf[0] = (char) va_arg(ap, int);
@@ -931,19 +936,21 @@ void log_error(int loglevel, const char *fmt, ...)
    assert(NULL != logfp);
 #endif
 
+   lock_logfile();
+
    if (loglevel == LOG_LEVEL_FATAL)
    {
-      fatal_error(outbuf_save);
+      fatal_error(outbuf);
       /* Never get here */
    }
    if (logfp != NULL)
    {
-      fputs(outbuf_save, logfp);
+      fputs(outbuf, logfp);
    }
 
 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
    /* Write to display */
-   LogPutString(outbuf_save);
+   LogPutString(outbuf);
 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
 
    unlock_logfile();
@@ -957,17 +964,13 @@ void log_error(int loglevel, const char *fmt, ...)
  *
  * Description :  Translates JB_ERR_FOO codes into strings.
  *
- *                XXX: the type of error codes is jb_err
- *                but the typedef'inition is currently not
- *                visible to all files that include errlog.h.
- *
  * Parameters  :
  *          1  :  jb_error = a valid jb_err code
  *
  * Returns     :  A string with the jb_err translation
  *
  *********************************************************************/
-const char *jb_err_to_string(int jb_error)
+const char *jb_err_to_string(jb_err jb_error)
 {
    switch (jb_error)
    {
@@ -985,9 +988,6 @@ const char *jb_err_to_string(int jb_error)
          return "File has been modified outside of the CGI actions editor.";
       case JB_ERR_COMPRESS:
          return "(De)compression failure";
-      default:
-         assert(0);
-         return "Unknown error";
    }
    assert(0);
    return "Internal error";