@@ -25,6 +25,10 @@ bool log_level_defined = false;
25
25
char * log_filename = NULL ;
26
26
char * error_log_filename = NULL ;
27
27
char * log_directory = NULL ;
28
+ /*
29
+ * If log_path is empty logging is not initialized.
30
+ * We will log only into stderr
31
+ */
28
32
char log_path [MAXPGPATH ] = "" ;
29
33
30
34
/* Maximum size of an individual log file in kilobytes */
@@ -104,30 +108,42 @@ write_elevel(FILE *stream, int elevel)
104
108
static void
105
109
elog_internal (int elevel , const char * fmt , va_list args )
106
110
{
107
- bool wrote_to_file = false,
108
- write_to_error_log ;
111
+ bool write_to_file ,
112
+ write_to_error_log ,
113
+ write_to_stderr ;
109
114
va_list error_args ,
110
115
std_args ;
111
116
112
- /* There is no need to lock if this is elog() from upper elog() */
113
- if (!logging_to_file )
117
+ write_to_file = log_path [0 ] != '\0' && !logging_to_file &&
118
+ (log_filename || error_log_filename );
119
+
120
+ /*
121
+ * There is no need to lock if this is elog() from upper elog() and
122
+ * logging is not initialized.
123
+ */
124
+ if (write_to_file )
114
125
pthread_mutex_lock (& log_file_mutex );
115
126
116
127
write_to_error_log =
117
- elevel >= ERROR && error_log_filename && !logging_to_file ;
128
+ elevel >= ERROR && error_log_filename && write_to_file ;
129
+ write_to_stderr = elevel >= ERROR || !write_to_file ;
130
+
118
131
/* We need copy args only if we need write to error log file */
119
132
if (write_to_error_log )
120
133
va_copy (error_args , args );
121
- /* We need copy args only if we need write to stderr */
122
- if (elevel >= ERROR || !(log_filename && !logging_to_file ))
134
+ /*
135
+ * We need copy args only if we need write to stderr. But do not copy args
136
+ * if we need to log only to stderr.
137
+ */
138
+ if (write_to_stderr && write_to_file )
123
139
va_copy (std_args , args );
124
140
125
141
/*
126
142
* Write message to log file.
127
143
* Do not write to file if this error was raised during write previous
128
144
* message.
129
145
*/
130
- if (log_filename && ! logging_to_file )
146
+ if (log_filename && write_to_file )
131
147
{
132
148
logging_to_file = true;
133
149
@@ -141,7 +157,6 @@ elog_internal(int elevel, const char *fmt, va_list args)
141
157
fflush (log_file );
142
158
143
159
logging_to_file = false;
144
- wrote_to_file = true;
145
160
}
146
161
147
162
/*
@@ -170,18 +185,22 @@ elog_internal(int elevel, const char *fmt, va_list args)
170
185
* Write to stderr if the message was not written to log file.
171
186
* Write to stderr if the message level is greater than WARNING anyway.
172
187
*/
173
- if (! wrote_to_file || elevel >= ERROR )
188
+ if (write_to_stderr )
174
189
{
175
190
write_elevel (stderr , elevel );
176
191
177
- vfprintf (stderr , fmt , std_args );
192
+ if (write_to_file )
193
+ vfprintf (stderr , fmt , std_args );
194
+ else
195
+ vfprintf (stderr , fmt , args );
178
196
fputc ('\n' , stderr );
179
197
fflush (stderr );
180
198
181
- va_end (std_args );
199
+ if (write_to_file )
200
+ va_end (std_args );
182
201
}
183
202
184
- if (! logging_to_file )
203
+ if (write_to_file )
185
204
pthread_mutex_unlock (& log_file_mutex );
186
205
187
206
/* Exit with code if it is an error */
0 commit comments