2
2
*
3
3
* logger.c: - log events into log file or stderr.
4
4
*
5
- * Portions Copyright (c) 2017-2017, Postgres Professional
5
+ * Copyright (c) 2017-2017, Postgres Professional
6
6
*
7
7
*-------------------------------------------------------------------------
8
8
*/
@@ -103,12 +103,22 @@ write_elevel(FILE *stream, int elevel)
103
103
static void
104
104
elog_internal (int elevel , const char * fmt , va_list args )
105
105
{
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 ;
107
111
108
112
/* There is no need to lock if this is elog() from upper elog() */
109
113
if (!logging_to_file )
110
114
pthread_mutex_lock (& log_file_mutex );
111
115
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
+
112
122
/*
113
123
* Write message to log file.
114
124
* 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)
131
141
wrote_to_file = true;
132
142
}
133
143
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
+
134
153
/*
135
154
* Write error message to error log file.
136
155
* Do not write to file if this error was raised during write previous
137
156
* message.
138
157
*/
139
- if (elevel >= ERROR && error_log_filename && ! logging_to_file )
158
+ if (write_to_error_log )
140
159
{
141
160
logging_to_file = true;
142
161
@@ -145,25 +164,23 @@ elog_internal(int elevel, const char *fmt, va_list args)
145
164
146
165
write_elevel (error_log_file , elevel );
147
166
148
- vfprintf (error_log_file , fmt , args );
167
+ vfprintf (error_log_file , fmt , error_args );
149
168
fputc ('\n' , error_log_file );
150
169
fflush (error_log_file );
151
170
152
171
logging_to_file = false;
172
+ va_end (error_args );
153
173
}
154
174
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 )
161
176
{
162
177
write_elevel (stderr , elevel );
163
178
164
- vfprintf (stderr , fmt , args );
179
+ vfprintf (stderr , fmt , std_args );
165
180
fputc ('\n' , stderr );
166
181
fflush (stderr );
182
+
183
+ va_end (std_args );
167
184
}
168
185
169
186
if (!logging_to_file )
0 commit comments