Skip to content

Commit 3b3180e

Browse files
committed
Fix logger, when an error is occured during parsing cmd options
1 parent 798c447 commit 3b3180e

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

utils/logger.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ bool log_level_defined = false;
2525
char *log_filename = NULL;
2626
char *error_log_filename = NULL;
2727
char *log_directory = NULL;
28+
/*
29+
* If log_path is empty logging is not initialized.
30+
* We will log only into stderr
31+
*/
2832
char log_path[MAXPGPATH] = "";
2933

3034
/* Maximum size of an individual log file in kilobytes */
@@ -104,30 +108,42 @@ write_elevel(FILE *stream, int elevel)
104108
static void
105109
elog_internal(int elevel, const char *fmt, va_list args)
106110
{
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;
109114
va_list error_args,
110115
std_args;
111116

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)
114125
pthread_mutex_lock(&log_file_mutex);
115126

116127
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+
118131
/* We need copy args only if we need write to error log file */
119132
if (write_to_error_log)
120133
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)
123139
va_copy(std_args, args);
124140

125141
/*
126142
* Write message to log file.
127143
* Do not write to file if this error was raised during write previous
128144
* message.
129145
*/
130-
if (log_filename && !logging_to_file)
146+
if (log_filename && write_to_file)
131147
{
132148
logging_to_file = true;
133149

@@ -141,7 +157,6 @@ elog_internal(int elevel, const char *fmt, va_list args)
141157
fflush(log_file);
142158

143159
logging_to_file = false;
144-
wrote_to_file = true;
145160
}
146161

147162
/*
@@ -170,18 +185,22 @@ elog_internal(int elevel, const char *fmt, va_list args)
170185
* Write to stderr if the message was not written to log file.
171186
* Write to stderr if the message level is greater than WARNING anyway.
172187
*/
173-
if (!wrote_to_file || elevel >= ERROR)
188+
if (write_to_stderr)
174189
{
175190
write_elevel(stderr, elevel);
176191

177-
vfprintf(stderr, fmt, std_args);
192+
if (write_to_file)
193+
vfprintf(stderr, fmt, std_args);
194+
else
195+
vfprintf(stderr, fmt, args);
178196
fputc('\n', stderr);
179197
fflush(stderr);
180198

181-
va_end(std_args);
199+
if (write_to_file)
200+
va_end(std_args);
182201
}
183202

184-
if (!logging_to_file)
203+
if (write_to_file)
185204
pthread_mutex_unlock(&log_file_mutex);
186205

187206
/* Exit with code if it is an error */

0 commit comments

Comments
 (0)