22 *
33 * logger.c: - log events into log file or stderr.
44 *
5- *Portions Copyright (c) 2017-2017, Postgres Professional
5+ * Copyright (c) 2017-2017, Postgres Professional
66 *
77 *-------------------------------------------------------------------------
88 */
@@ -103,12 +103,22 @@ write_elevel(FILE *stream, int elevel)
103103static void
104104elog_internal (int elevel ,const char * fmt ,va_list args )
105105{
106- bool wrote_to_file = false;
106+ bool wrote_to_file = false,
107+ write_to_error_log ,
108+ write_to_stderr ;
109+ va_list error_args ,
110+ std_args ;
107111
108112/* There is no need to lock if this is elog() from upper elog() */
109113if (!logging_to_file )
110114pthread_mutex_lock (& log_file_mutex );
111115
116+ write_to_error_log =
117+ elevel >=ERROR && error_log_filename && !logging_to_file ;
118+ /* We need copy args only if we need write to error log file */
119+ if (write_to_error_log )
120+ va_copy (error_args ,args );
121+
112122/*
113123 * Write message to log file.
114124 * Do not write to file if this error was raised during write previous
@@ -131,12 +141,21 @@ elog_internal(int elevel, const char *fmt, va_list args)
131141wrote_to_file = true;
132142}
133143
144+ /*
145+ * Write to stderr if the message was not written to log file.
146+ * Write to stderr if the message level is greater than WARNING anyway.
147+ */
148+ write_to_stderr = !wrote_to_file || elevel >=ERROR ;
149+ /* We need copy args only if we need write to stderr */
150+ if (write_to_stderr )
151+ va_copy (std_args ,error_args );
152+
134153/*
135154 * Write error message to error log file.
136155 * Do not write to file if this error was raised during write previous
137156 * message.
138157 */
139- if (elevel >= ERROR && error_log_filename && ! logging_to_file )
158+ if (write_to_error_log )
140159{
141160logging_to_file = true;
142161
@@ -145,25 +164,23 @@ elog_internal(int elevel, const char *fmt, va_list args)
145164
146165write_elevel (error_log_file ,elevel );
147166
148- vfprintf (error_log_file ,fmt ,args );
167+ vfprintf (error_log_file ,fmt ,error_args );
149168fputc ('\n' ,error_log_file );
150169fflush (error_log_file );
151170
152171logging_to_file = false;
172+ va_end (error_args );
153173}
154174
155- /*
156- * Write to stderr if the message was not written to log file.
157- * Write to stderr if the message level is greater than WARNING anyway.
158- */
159- if (!wrote_to_file ||
160- elevel >=ERROR )
175+ if (write_to_stderr )
161176{
162177write_elevel (stderr ,elevel );
163178
164- vfprintf (stderr ,fmt ,args );
179+ vfprintf (stderr ,fmt ,std_args );
165180fputc ('\n' ,stderr );
166181fflush (stderr );
182+
183+ va_end (std_args );
167184}
168185
169186if (!logging_to_file )