From: Fabian Keil <fk@fabiankeil.de>
Date: Mon, 29 May 2017 10:06:57 +0000 (+0000)
Subject: log_error(): Reduce the mutex-protected area
X-Git-Tag: v_3_0_27~148
X-Git-Url: http://www.privoxy.org/gitweb/@default-cgi@edit-actions-section-add?a=commitdiff_plain;h=af31785cde42288c236ca4d0ba850d02e466c907;p=privoxy.git

log_error(): Reduce the mutex-protected area

... by not using a heap-allocated buffer that is
shared between all threads.

This increases performance and reduces the latency
with verbose debug settings and multiple concurrent
connections.

Sponsored by: Robert Klemme
---

diff --git a/errlog.c b/errlog.c
index 048a3b78..f2ec3ff3 100644
--- a/errlog.c
+++ b/errlog.c
@@ -1,4 +1,4 @@
-const char errlog_rcs[] = "$Id: errlog.c,v 1.127 2016/12/24 16:00:49 fabiankeil Exp $";
+const char errlog_rcs[] = "$Id: errlog.c,v 1.128 2017/05/29 10:06:09 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/errlog.c,v $
@@ -674,18 +674,12 @@ 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 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?
-    */
    const size_t log_buffer_size = LOG_BUFFER_SIZE;
 
 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
@@ -728,20 +722,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 = zalloc_or_die(log_buffer_size + 1); /* +1 for paranoia */
-   }
-   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)
@@ -955,14 +940,16 @@ 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)