@@ -44,15 +44,16 @@ static void elog_internal(int elevel, const char *fmt, va_list args)
44
44
pg_attribute_printf (2 , 0 );
45
45
46
46
/* Functions to work with log files */
47
- static void open_logfile (void );
47
+ static void open_logfile (FILE * * file , const char * filename_format );
48
48
static void release_logfile (void );
49
- static char * logfile_getname (time_t timestamp );
49
+ static char * logfile_getname (const char * format , time_t timestamp );
50
50
static FILE * logfile_open (const char * filename , const char * mode );
51
51
52
52
/* Static variables */
53
53
54
54
static FILE * log_file = NULL ;
55
- static char * last_file_name = NULL ;
55
+ static FILE * error_log_file = NULL ;
56
+
56
57
static bool exit_hook_registered = false;
57
58
/* Logging to file is in progress */
58
59
static bool logging_to_file = false;
@@ -113,7 +114,7 @@ elog_internal(int elevel, const char *fmt, va_list args)
113
114
logging_to_file = true;
114
115
115
116
if (log_file == NULL )
116
- open_logfile ();
117
+ open_logfile (& log_file , log_filename );
117
118
118
119
write_elevel (log_file , elevel );
119
120
@@ -125,6 +126,27 @@ elog_internal(int elevel, const char *fmt, va_list args)
125
126
wrote_to_file = true;
126
127
}
127
128
129
+ /*
130
+ * Write error message to error log file.
131
+ * Do not write to file if this error was raised during write previous
132
+ * message.
133
+ */
134
+ if (elevel >= ERROR && error_log_filename && !logging_to_file )
135
+ {
136
+ logging_to_file = true;
137
+
138
+ if (error_log_file == NULL )
139
+ open_logfile (& error_log_file , error_log_filename );
140
+
141
+ write_elevel (error_log_file , elevel );
142
+
143
+ vfprintf (error_log_file , fmt , args );
144
+ fputc ('\n' , error_log_file );
145
+ fflush (error_log_file );
146
+
147
+ logging_to_file = false;
148
+ }
149
+
128
150
/*
129
151
* Write to stderr if the message was not written to log file.
130
152
* Write to stderr if the message level is greater than WARNING anyway.
@@ -249,7 +271,7 @@ parse_log_level(const char *level)
249
271
* Result is palloc'd.
250
272
*/
251
273
static char *
252
- logfile_getname (time_t timestamp )
274
+ logfile_getname (const char * format , time_t timestamp )
253
275
{
254
276
char * filename ;
255
277
size_t len ;
@@ -265,8 +287,8 @@ logfile_getname(time_t timestamp)
265
287
len = strlen (filename );
266
288
267
289
/* Treat log_filename as a strftime pattern */
268
- if (strftime (filename + len , MAXPGPATH - len , log_filename , tm ) <= 0 )
269
- elog (ERROR , "strftime(%s) failed: %s" , log_filename , strerror (errno ));
290
+ if (strftime (filename + len , MAXPGPATH - len , format , tm ) <= 0 )
291
+ elog (ERROR , "strftime(%s) failed: %s" , format , strerror (errno ));
270
292
271
293
return filename ;
272
294
}
@@ -304,18 +326,13 @@ logfile_open(const char *filename, const char *mode)
304
326
* Open the log file.
305
327
*/
306
328
static void
307
- open_logfile (void )
329
+ open_logfile (FILE * * file , const char * filename_format )
308
330
{
309
331
char * filename ;
310
332
311
- filename = logfile_getname (time (NULL ));
312
-
313
- log_file = logfile_open (filename , "a" );
314
-
315
- if (last_file_name != NULL )
316
- pfree (last_file_name );
317
-
318
- last_file_name = filename ;
333
+ filename = logfile_getname (filename_format , time (NULL ));
334
+ * file = logfile_open (filename , "a" );
335
+ pfree (filename );
319
336
320
337
/*
321
338
* Arrange to close opened file at proc_exit.
@@ -338,6 +355,9 @@ release_logfile(void)
338
355
fclose (log_file );
339
356
log_file = NULL ;
340
357
}
341
- if (last_file_name != NULL )
342
- pfree (last_file_name );
358
+ if (error_log_file )
359
+ {
360
+ fclose (error_log_file );
361
+ error_log_file = NULL ;
362
+ }
343
363
}
0 commit comments