@@ -40,10 +40,10 @@ typedef enum
40
40
41
41
void pg_log (eLogType type , const char * fmt ,...) pg_attribute_printf (2 , 3 );
42
42
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 );
45
44
static void elog_stderr (int elevel , const char * fmt , ...)
46
45
pg_attribute_printf (2 , 3 );
46
+ static char * get_log_message (const char * fmt , va_list args ) pg_attribute_printf (1 , 0 );
47
47
48
48
/* Functions to work with log files */
49
49
static void open_logfile (FILE * * file , const char * filename_format );
@@ -74,7 +74,7 @@ init_logger(const char *root_path, LoggerConfig *config)
74
74
/* Set log path */
75
75
if (config -> log_directory == NULL )
76
76
{
77
- config -> log_directory = palloc (MAXPGPATH );
77
+ config -> log_directory = pgut_malloc (MAXPGPATH );
78
78
join_path_components (config -> log_directory ,
79
79
root_path , LOG_DIRECTORY_DEFAULT );
80
80
}
@@ -148,13 +148,11 @@ exit_if_necessary(int elevel)
148
148
* Actual implementation for elog() and pg_log().
149
149
*/
150
150
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 )
152
152
{
153
153
bool write_to_file ,
154
154
write_to_error_log ,
155
155
write_to_stderr ;
156
- va_list error_args ,
157
- std_args ;
158
156
time_t log_time = (time_t ) time (NULL );
159
157
char strfbuf [128 ];
160
158
@@ -165,22 +163,8 @@ elog_internal(int elevel, bool file_only, const char *fmt, va_list args)
165
163
write_to_stderr = elevel >= logger_config .log_level_console && !file_only ;
166
164
167
165
pthread_lock (& log_file_mutex );
168
- #ifdef WIN32
169
- std_args = NULL ;
170
- error_args = NULL ;
171
- #endif
172
166
loggin_in_progress = true;
173
167
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
-
184
168
if (write_to_file || write_to_error_log )
185
169
strftime (strfbuf , sizeof (strfbuf ), "%Y-%m-%d %H:%M:%S %Z" ,
186
170
localtime (& log_time ));
@@ -203,8 +187,7 @@ elog_internal(int elevel, bool file_only, const char *fmt, va_list args)
203
187
fprintf (log_file , "%s: " , strfbuf );
204
188
write_elevel (log_file , elevel );
205
189
206
- vfprintf (log_file , fmt , args );
207
- fputc ('\n' , log_file );
190
+ fprintf (log_file , "%s\n" , message );
208
191
fflush (log_file );
209
192
}
210
193
@@ -221,33 +204,19 @@ elog_internal(int elevel, bool file_only, const char *fmt, va_list args)
221
204
fprintf (error_log_file , "%s: " , strfbuf );
222
205
write_elevel (error_log_file , elevel );
223
206
224
- vfprintf (error_log_file , fmt , error_args );
225
- fputc ('\n' , error_log_file );
207
+ fprintf (error_log_file , "%s\n" , message );
226
208
fflush (error_log_file );
227
-
228
- va_end (error_args );
229
209
}
230
210
231
211
/*
232
212
* Write to stderr if the message was not written to log file.
233
213
* Write to stderr if the message level is greater than WARNING anyway.
234
214
*/
235
- if (write_to_stderr && write_to_file )
215
+ if (write_to_stderr )
236
216
{
237
217
write_elevel (stderr , elevel );
238
218
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 );
251
220
fflush (stderr );
252
221
}
253
222
@@ -285,12 +254,44 @@ elog_stderr(int elevel, const char *fmt, ...)
285
254
exit_if_necessary (elevel );
286
255
}
287
256
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
+
288
288
/*
289
289
* Logs to stderr or to log file and exit if ERROR.
290
290
*/
291
291
void
292
292
elog (int elevel , const char * fmt , ...)
293
293
{
294
+ char * message ;
294
295
va_list args ;
295
296
296
297
/*
@@ -302,8 +303,11 @@ elog(int elevel, const char *fmt, ...)
302
303
return ;
303
304
304
305
va_start (args , fmt );
305
- elog_internal ( elevel , false, fmt , args );
306
+ message = get_log_message ( fmt , args );
306
307
va_end (args );
308
+
309
+ elog_internal (elevel , false, message );
310
+ pfree (message );
307
311
}
308
312
309
313
/*
@@ -312,6 +316,7 @@ elog(int elevel, const char *fmt, ...)
312
316
void
313
317
elog_file (int elevel , const char * fmt , ...)
314
318
{
319
+ char * message ;
315
320
va_list args ;
316
321
317
322
/*
@@ -322,8 +327,11 @@ elog_file(int elevel, const char *fmt, ...)
322
327
return ;
323
328
324
329
va_start (args , fmt );
325
- elog_internal ( elevel , true, fmt , args );
330
+ message = get_log_message ( fmt , args );
326
331
va_end (args );
332
+
333
+ elog_internal (elevel , true, message );
334
+ pfree (message );
327
335
}
328
336
329
337
/*
@@ -332,6 +340,7 @@ elog_file(int elevel, const char *fmt, ...)
332
340
void
333
341
pg_log (eLogType type , const char * fmt , ...)
334
342
{
343
+ char * message ;
335
344
va_list args ;
336
345
int elevel = INFO ;
337
346
@@ -364,8 +373,11 @@ pg_log(eLogType type, const char *fmt, ...)
364
373
return ;
365
374
366
375
va_start (args , fmt );
367
- elog_internal ( elevel , false, fmt , args );
376
+ message = get_log_message ( fmt , args );
368
377
va_end (args );
378
+
379
+ elog_internal (elevel , false, message );
380
+ pfree (message );
369
381
}
370
382
371
383
/*
@@ -450,7 +462,7 @@ logfile_getname(const char *format, time_t timestamp)
450
462
logger_config .log_directory [0 ] == '\0' )
451
463
elog_stderr (ERROR , "logging path is not set" );
452
464
453
- filename = (char * ) palloc (MAXPGPATH );
465
+ filename = (char * ) pgut_malloc (MAXPGPATH );
454
466
455
467
snprintf (filename , MAXPGPATH , "%s/" , logger_config .log_directory );
456
468
0 commit comments