Skip to content

Commit 385f979

Browse files
committed
Make compiler happy again
1 parent eead6b6 commit 385f979

File tree

1 file changed

+55
-43
lines changed

1 file changed

+55
-43
lines changed

src/utils/logger.c

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ typedef enum
4040

4141
void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
4242

43-
static void elog_internal(int elevel, bool file_only, const char *fmt, va_list args)
44-
pg_attribute_printf(3, 0);
43+
static void elog_internal(int elevel, bool file_only, const char *message);
4544
static void elog_stderr(int elevel, const char *fmt, ...)
4645
pg_attribute_printf(2, 3);
46+
static char *get_log_message(const char *fmt, va_list args) pg_attribute_printf(1, 0);
4747

4848
/* Functions to work with log files */
4949
static void open_logfile(FILE **file, const char *filename_format);
@@ -74,7 +74,7 @@ init_logger(const char *root_path, LoggerConfig *config)
7474
/* Set log path */
7575
if (config->log_directory == NULL)
7676
{
77-
config->log_directory = palloc(MAXPGPATH);
77+
config->log_directory = pgut_malloc(MAXPGPATH);
7878
join_path_components(config->log_directory,
7979
root_path, LOG_DIRECTORY_DEFAULT);
8080
}
@@ -148,13 +148,11 @@ exit_if_necessary(int elevel)
148148
* Actual implementation for elog() and pg_log().
149149
*/
150150
static void
151-
elog_internal(int elevel, bool file_only, const char *fmt, va_list args)
151+
elog_internal(int elevel, bool file_only, const char *message)
152152
{
153153
bool write_to_file,
154154
write_to_error_log,
155155
write_to_stderr;
156-
va_list error_args,
157-
std_args;
158156
time_t log_time = (time_t) time(NULL);
159157
char strfbuf[128];
160158

@@ -165,22 +163,8 @@ elog_internal(int elevel, bool file_only, const char *fmt, va_list args)
165163
write_to_stderr = elevel >= logger_config.log_level_console && !file_only;
166164

167165
pthread_lock(&log_file_mutex);
168-
#ifdef WIN32
169-
std_args = NULL;
170-
error_args = NULL;
171-
#endif
172166
loggin_in_progress = true;
173167

174-
/* We need copy args only if we need write to error log file */
175-
if (write_to_error_log)
176-
va_copy(error_args, args);
177-
/*
178-
* We need copy args only if we need write to stderr. But do not copy args
179-
* if we need to log only to stderr.
180-
*/
181-
if (write_to_stderr && write_to_file)
182-
va_copy(std_args, args);
183-
184168
if (write_to_file || write_to_error_log)
185169
strftime(strfbuf, sizeof(strfbuf), "%Y-%m-%d %H:%M:%S %Z",
186170
localtime(&log_time));
@@ -203,8 +187,7 @@ elog_internal(int elevel, bool file_only, const char *fmt, va_list args)
203187
fprintf(log_file, "%s: ", strfbuf);
204188
write_elevel(log_file, elevel);
205189

206-
vfprintf(log_file, fmt, args);
207-
fputc('\n', log_file);
190+
fprintf(log_file, "%s\n", message);
208191
fflush(log_file);
209192
}
210193

@@ -221,33 +204,19 @@ elog_internal(int elevel, bool file_only, const char *fmt, va_list args)
221204
fprintf(error_log_file, "%s: ", strfbuf);
222205
write_elevel(error_log_file, elevel);
223206

224-
vfprintf(error_log_file, fmt, error_args);
225-
fputc('\n', error_log_file);
207+
fprintf(error_log_file, "%s\n", message);
226208
fflush(error_log_file);
227-
228-
va_end(error_args);
229209
}
230210

231211
/*
232212
* Write to stderr if the message was not written to log file.
233213
* Write to stderr if the message level is greater than WARNING anyway.
234214
*/
235-
if (write_to_stderr && write_to_file)
215+
if (write_to_stderr)
236216
{
237217
write_elevel(stderr, elevel);
238218

239-
vfprintf(stderr, fmt, std_args);
240-
fputc('\n', stderr);
241-
fflush(stderr);
242-
243-
va_end(std_args);
244-
}
245-
else if (write_to_stderr)
246-
{
247-
write_elevel(stderr, elevel);
248-
249-
vfprintf(stderr, fmt, args);
250-
fputc('\n', stderr);
219+
fprintf(stderr, "%s\n", message);
251220
fflush(stderr);
252221
}
253222

@@ -285,12 +254,44 @@ elog_stderr(int elevel, const char *fmt, ...)
285254
exit_if_necessary(elevel);
286255
}
287256

257+
/*
258+
* Formats text data under the control of fmt and returns it in an allocated
259+
* buffer.
260+
*/
261+
static char *
262+
get_log_message(const char *fmt, va_list args)
263+
{
264+
size_t len = 256; /* initial assumption about buffer size */
265+
266+
for (;;)
267+
{
268+
char *result;
269+
size_t newlen;
270+
va_list copy_args;
271+
272+
result = (char *) pgut_malloc(len);
273+
274+
/* Try to format the data */
275+
va_copy(copy_args, args);
276+
newlen = pvsnprintf(result, len, fmt, copy_args);
277+
va_end(copy_args);
278+
279+
if (newlen < len)
280+
return result; /* success */
281+
282+
/* Release buffer and loop around to try again with larger len. */
283+
pfree(result);
284+
len = newlen;
285+
}
286+
}
287+
288288
/*
289289
* Logs to stderr or to log file and exit if ERROR.
290290
*/
291291
void
292292
elog(int elevel, const char *fmt, ...)
293293
{
294+
char *message;
294295
va_list args;
295296

296297
/*
@@ -302,8 +303,11 @@ elog(int elevel, const char *fmt, ...)
302303
return;
303304

304305
va_start(args, fmt);
305-
elog_internal(elevel, false, fmt, args);
306+
message = get_log_message(fmt, args);
306307
va_end(args);
308+
309+
elog_internal(elevel, false, message);
310+
pfree(message);
307311
}
308312

309313
/*
@@ -312,6 +316,7 @@ elog(int elevel, const char *fmt, ...)
312316
void
313317
elog_file(int elevel, const char *fmt, ...)
314318
{
319+
char *message;
315320
va_list args;
316321

317322
/*
@@ -322,8 +327,11 @@ elog_file(int elevel, const char *fmt, ...)
322327
return;
323328

324329
va_start(args, fmt);
325-
elog_internal(elevel, true, fmt, args);
330+
message = get_log_message(fmt, args);
326331
va_end(args);
332+
333+
elog_internal(elevel, true, message);
334+
pfree(message);
327335
}
328336

329337
/*
@@ -332,6 +340,7 @@ elog_file(int elevel, const char *fmt, ...)
332340
void
333341
pg_log(eLogType type, const char *fmt, ...)
334342
{
343+
char *message;
335344
va_list args;
336345
int elevel = INFO;
337346

@@ -364,8 +373,11 @@ pg_log(eLogType type, const char *fmt, ...)
364373
return;
365374

366375
va_start(args, fmt);
367-
elog_internal(elevel, false, fmt, args);
376+
message = get_log_message(fmt, args);
368377
va_end(args);
378+
379+
elog_internal(elevel, false, message);
380+
pfree(message);
369381
}
370382

371383
/*
@@ -450,7 +462,7 @@ logfile_getname(const char *format, time_t timestamp)
450462
logger_config.log_directory[0] == '\0')
451463
elog_stderr(ERROR, "logging path is not set");
452464

453-
filename = (char *) palloc(MAXPGPATH);
465+
filename = (char *) pgut_malloc(MAXPGPATH);
454466

455467
snprintf(filename, MAXPGPATH, "%s/", logger_config.log_directory);
456468

0 commit comments

Comments
 (0)