@@ -44,15 +44,16 @@ static void elog_internal(int elevel, const char *fmt, va_list args)
4444pg_attribute_printf (2 ,0 );
4545
4646/* Functions to work with log files */
47- static void open_logfile (void );
47+ static void open_logfile (FILE * * file , const char * filename_format );
4848static void release_logfile (void );
49- static char * logfile_getname (time_t timestamp );
49+ static char * logfile_getname (const char * format , time_t timestamp );
5050static FILE * logfile_open (const char * filename ,const char * mode );
5151
5252/* Static variables */
5353
5454static FILE * log_file = NULL ;
55- static char * last_file_name = NULL ;
55+ static FILE * error_log_file = NULL ;
56+
5657static bool exit_hook_registered = false;
5758/* Logging to file is in progress */
5859static bool logging_to_file = false;
@@ -113,7 +114,7 @@ elog_internal(int elevel, const char *fmt, va_list args)
113114logging_to_file = true;
114115
115116if (log_file == NULL )
116- open_logfile ();
117+ open_logfile (& log_file , log_filename );
117118
118119write_elevel (log_file ,elevel );
119120
@@ -125,6 +126,27 @@ elog_internal(int elevel, const char *fmt, va_list args)
125126wrote_to_file = true;
126127}
127128
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+
128150/*
129151 * Write to stderr if the message was not written to log file.
130152 * Write to stderr if the message level is greater than WARNING anyway.
@@ -249,7 +271,7 @@ parse_log_level(const char *level)
249271 * Result is palloc'd.
250272 */
251273static char *
252- logfile_getname (time_t timestamp )
274+ logfile_getname (const char * format , time_t timestamp )
253275{
254276char * filename ;
255277size_t len ;
@@ -265,8 +287,8 @@ logfile_getname(time_t timestamp)
265287len = strlen (filename );
266288
267289/* 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 ));
270292
271293return filename ;
272294}
@@ -304,18 +326,13 @@ logfile_open(const char *filename, const char *mode)
304326 * Open the log file.
305327 */
306328static void
307- open_logfile (void )
329+ open_logfile (FILE * * file , const char * filename_format )
308330{
309331char * filename ;
310332
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 );
319336
320337/*
321338 * Arrange to close opened file at proc_exit.
@@ -338,6 +355,9 @@ release_logfile(void)
338355fclose (log_file );
339356log_file = NULL ;
340357}
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+ }
343363}