Use local variable to format timestamp in GTM log line prefix
authorTomas Vondra <[email protected]>
Sun, 12 Nov 2017 11:01:53 +0000 (12:01 +0100)
committerTomas Vondra <[email protected]>
Sun, 12 Nov 2017 11:01:53 +0000 (12:01 +0100)
When formatting log line prefix in GTM, we can't use global variable,
because multiple threads may scribble over the same value. This is
why the timestamp was missing in some log lines - one thread did the
strftime(), but before it used the value another thread truncated the
string (which is the first step in formatting a log line).

So instead use a local (not shared by threads) variable, and pass it
to setup_formatted_log_time() explicitly.

src/gtm/common/elog.c

index 9388130a4683e77991ea373e2da9f15b468f3425..4bda0c103c9642242fd9c303e080e725d37bfcf6 100644 (file)
@@ -66,15 +66,14 @@ char           *Log_line_prefix = "%l:%p:%m -";             /* format for extra log line info */
 
 #define FORMATTED_TS_LEN 128
 static char formatted_start_time[FORMATTED_TS_LEN];
-static char formatted_log_time[FORMATTED_TS_LEN];
 
 static void log_line_prefix(StringInfo buf);
-static void setup_formatted_log_time(void);
+static void setup_formatted_log_time(char formatted_log_time[FORMATTED_TS_LEN]);
 /*
  * setup formatted_log_time, for consistent times between CSV and regular logs
  */
 static void
-setup_formatted_log_time(void)
+setup_formatted_log_time(char formatted_log_time[FORMATTED_TS_LEN])
 {
        struct timeval tv;
        time_t  stamp_time;
@@ -101,6 +100,7 @@ log_line_prefix(StringInfo buf)
 {
        /* static counter for line numbers */
        static long log_line_number = 0;
+       static char formatted_log_time[FORMATTED_TS_LEN];
 
        /* has counter been reset in current process? */
        static int      log_my_pid = 0;
@@ -150,7 +150,8 @@ log_line_prefix(StringInfo buf)
                                appendStringInfo(buf, "%ld", log_line_number);
                                break;
                        case 'm':
-                               setup_formatted_log_time();
+                               formatted_log_time[0] = '\0';
+                               setup_formatted_log_time(formatted_log_time);
                                appendStringInfoString(buf, formatted_log_time);
                                break;
                        default:
@@ -791,8 +792,6 @@ send_message_to_server_log(ErrorData *edata)
 
        initStringInfo(&buf);
 
-       formatted_log_time[0] = '\0';
-
        log_line_prefix(&buf);
        appendStringInfo(&buf, "%s:  ", error_severity(edata->elevel));