+void log_error(int loglevel, const char *fmt, ...)
+{
+ va_list ap;
+ char *outbuf = NULL;
+ static char *outbuf_save = NULL;
+ char tempbuf[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;
+
+#if defined(_WIN32) && !defined(_WIN_CONSOLE)
+ /*
+ * Irrespective of debug setting, a GET/POST/CONNECT makes
+ * the taskbar icon animate. (There is an option to disable
+ * this but checking that is handled inside LogShowActivity()).
+ */
+ if (loglevel == LOG_LEVEL_GPC)
+ {
+ LogShowActivity();
+ }
+#endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
+
+ /* verify if loglevel applies to current settings and bail out if negative */
+ if ((loglevel & debug) == 0)
+ {
+ return;
+ }
+
+ 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 Privoxy(%08lx) Fatal error: log_error() failed to allocate buffer memory.\n"
+ "\nExiting.", timestamp, thread_id);
+ if( !logfp )
+ {
+ logfp = stderr;
+ }
+ fputs(tempbuf, logfp);
+ unlock_logfile();
+ fatal_error(tempbuf); /* Exit */
+ }
+ }
+ outbuf = outbuf_save;
+
+ /*
+ * Memsetting the whole buffer to zero (in theory)
+ * makes things easier later on.
+ */
+ memset(outbuf, 0, log_buffer_size);
+
+ /* Add prefix for everything but Common Log Format messages */
+ if (loglevel != LOG_LEVEL_CLF)
+ {
+ length = (size_t)snprintf(outbuf, log_buffer_size, "%s Privoxy(%08lx) %s: ",
+ timestamp, thread_id, get_log_level_string(loglevel));
+ }
+
+ /* get ready to scan var. args. */
+ va_start(ap, fmt);
+
+ /* build formatted message from fmt and var-args */
+ while ((*src) && (length < log_buffer_size-2))
+ {
+ const char *sval = NULL; /* %N string */
+ int ival; /* %N string length or an error code */
+ unsigned uval; /* %u value */
+ long lval; /* %l value */
+ unsigned long ulval; /* %ul value */
+ char ch;
+ const char *format_string = tempbuf;
+
+ ch = *src++;
+ if (ch != '%')
+ {
+ outbuf[length++] = ch;
+ /*
+ * XXX: Only necessary on platforms which don't use pthread
+ * mutexes (mingw32 for example), where multiple threads can
+ * write to the buffer at the same time.
+ */
+ outbuf[length] = '\0';
+ continue;
+ }
+ outbuf[length] = '\0';
+ ch = *src++;
+ switch (ch) {
+ case '%':
+ tempbuf[0] = '%';
+ tempbuf[1] = '\0';
+ break;
+ case 'd':
+ ival = va_arg( ap, int );
+ snprintf(tempbuf, sizeof(tempbuf), "%d", ival);
+ break;
+ case 'u':
+ uval = va_arg( ap, unsigned );
+ snprintf(tempbuf, sizeof(tempbuf), "%u", uval);
+ break;
+ case 'l':
+ /* this is a modifier that must be followed by u or d */
+ ch = *src++;
+ if (ch == 'd')
+ {
+ lval = va_arg( ap, long );
+ snprintf(tempbuf, sizeof(tempbuf), "%ld", lval);
+ }
+ else if (ch == 'u')
+ {
+ ulval = va_arg( ap, unsigned long );
+ snprintf(tempbuf, sizeof(tempbuf), "%lu", ulval);
+ }
+ else
+ {
+ snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
+ loglevel = LOG_LEVEL_FATAL;
+ }
+ break;
+ case 'c':
+ /*
+ * Note that char paramaters 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);
+ tempbuf[1] = '\0';
+ break;
+ case 's':
+ format_string = va_arg(ap, char *);
+ if (format_string == NULL)
+ {
+ format_string = "[null]";
+ }
+ break;
+ case 'N':
+ /*
+ * Non-standard: Print a counted unterminated string.
+ * Takes 2 parameters: int length, const char * string.
+ */
+ ival = va_arg(ap, int);
+ sval = va_arg(ap, char *);
+ if (sval == NULL)
+ {
+ format_string = "[null]";
+ }
+ else if (ival <= 0)
+ {
+ if (0 == ival)
+ {
+ /* That's ok (but stupid) */
+ tempbuf[0] = '\0';
+ }
+ else
+ {
+ /*
+ * That's not ok (and even more stupid)
+ */
+ assert(ival >= 0);
+ format_string = "[counted string lenght < 0]";
+ }
+ }
+ else if (ival >= sizeof(tempbuf))
+ {
+ /*
+ * String is too long, copy as much as possible.
+ * It will be further truncated later.
+ */
+ memcpy(tempbuf, sval, sizeof(tempbuf)-1);
+ tempbuf[sizeof(tempbuf)-1] = '\0';
+ }
+ else
+ {
+ memcpy(tempbuf, sval, (size_t) ival);
+ tempbuf[ival] = '\0';
+ }
+ break;
+ case 'E':
+ /* Non-standard: Print error code from errno */
+#ifdef _WIN32
+ ival = WSAGetLastError();
+ format_string = w32_socket_strerr(ival, tempbuf);
+#elif __OS2__
+ ival = sock_errno();
+ if (ival != 0)
+ {
+ format_string = os2_socket_strerr(ival, tempbuf);
+ }
+ else
+ {
+ ival = errno;
+ format_string = strerror(ival);
+ }
+#else /* ifndef _WIN32 */
+ ival = errno;
+#ifdef HAVE_STRERROR
+ format_string = strerror(ival);
+#else /* ifndef HAVE_STRERROR */
+ format_string = NULL;
+#endif /* ndef HAVE_STRERROR */
+ if (sval == NULL)
+ {
+ snprintf(tempbuf, sizeof(tempbuf), "(errno = %d)", ival);
+ }
+#endif /* ndef _WIN32 */
+ break;
+ case 'T':
+ /* Non-standard: Print a Common Log File timestamp */
+ get_clf_timestamp(tempbuf, sizeof(tempbuf));
+ break;
+ default:
+ snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
+ loglevel = LOG_LEVEL_FATAL;
+ break;
+ } /* switch( p ) */
+
+ assert(length < log_buffer_size);
+ length += strlcpy(outbuf + length, format_string, log_buffer_size - length);
+
+ if (length >= log_buffer_size-2)
+ {
+ static char warning[] = "... [too long, truncated]";
+
+ length = log_buffer_size - sizeof(warning) - 1;
+ length += strlcpy(outbuf + length, warning, log_buffer_size - length);
+ assert(length < log_buffer_size);
+
+ break;
+ }
+ } /* for( p ... ) */
+
+ /* done with var. args */
+ va_end(ap);
+
+ assert(length < log_buffer_size);
+ length += strlcpy(outbuf + length, "\n", log_buffer_size - length);
+
+ /* Some sanity checks */
+ if (!(length < log_buffer_size)
+ || !(outbuf[log_buffer_size-1] == '\0')
+ || !(outbuf[log_buffer_size] == '\0')
+ )
+ {
+ /* Repeat as assertions */
+ assert(length < log_buffer_size);
+ assert(outbuf[log_buffer_size-1] == '\0');
+ /*
+ * outbuf's real size is log_buffer_size+1,
+ * so while this looks like an off-by-one,
+ * we're only checking our paranoia byte.
+ */
+ assert(outbuf[log_buffer_size] == '\0');
+
+ snprintf(outbuf, log_buffer_size,
+ "%s Privoxy(%08lx) Fatal error: log_error()'s sanity checks failed. length: %d\n"
+ "Exiting.", timestamp, thread_id, (int)length);
+ loglevel = LOG_LEVEL_FATAL;
+ }
+
+ /* deal with glibc stupidity - it won't let you initialize logfp */
+ /* XXX: Still necessary? */
+ if(NULL == logfp)
+ {
+ logfp = stderr;
+ }
+
+ fputs(outbuf_save, logfp);
+
+ if (loglevel == LOG_LEVEL_FATAL)
+ {
+ fatal_error(outbuf_save);
+ /* Never get here */
+ }
+
+ unlock_logfile();
+
+#if defined(_WIN32) && !defined(_WIN_CONSOLE)
+ /* Write to display */
+ LogPutString(outbuf_save);
+#endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
+
+}
+
+#else /* use traditional log_error version */
+
+/*********************************************************************
+ *
+ * Function : log_error
+ *
+ * Description : This is the error-reporting and logging function.
+ *
+ * Parameters :
+ * 1 : loglevel = the type of message to be logged
+ * 2 : fmt = the main string we want logged, printf-like
+ * 3 : ... = arguments to be inserted in fmt (printf-like).
+ *
+ * Returns : N/A
+ *
+ *********************************************************************/
+void log_error(int loglevel, const char *fmt, ...)