Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcfe9b85

Browse files
committed
Logging to error-log-file works
1 parent9e8b171 commitcfe9b85

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

‎utils/logger.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ static void elog_internal(int elevel, const char *fmt, va_list args)
4444
pg_attribute_printf(2,0);
4545

4646
/* Functions to work with log files */
47-
staticvoidopen_logfile(void);
47+
staticvoidopen_logfile(FILE**file,constchar*filename_format);
4848
staticvoidrelease_logfile(void);
49-
staticchar*logfile_getname(time_ttimestamp);
49+
staticchar*logfile_getname(constchar*format,time_ttimestamp);
5050
staticFILE*logfile_open(constchar*filename,constchar*mode);
5151

5252
/* Static variables */
5353

5454
staticFILE*log_file=NULL;
55-
staticchar*last_file_name=NULL;
55+
staticFILE*error_log_file=NULL;
56+
5657
staticboolexit_hook_registered= false;
5758
/* Logging to file is in progress */
5859
staticboollogging_to_file= false;
@@ -113,7 +114,7 @@ elog_internal(int elevel, const char *fmt, va_list args)
113114
logging_to_file= true;
114115

115116
if (log_file==NULL)
116-
open_logfile();
117+
open_logfile(&log_file,log_filename);
117118

118119
write_elevel(log_file,elevel);
119120

@@ -125,6 +126,27 @@ elog_internal(int elevel, const char *fmt, va_list args)
125126
wrote_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
*/
251273
staticchar*
252-
logfile_getname(time_ttimestamp)
274+
logfile_getname(constchar*format,time_ttimestamp)
253275
{
254276
char*filename;
255277
size_tlen;
@@ -265,8 +287,8 @@ logfile_getname(time_t timestamp)
265287
len=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

271293
returnfilename;
272294
}
@@ -304,18 +326,13 @@ logfile_open(const char *filename, const char *mode)
304326
* Open the log file.
305327
*/
306328
staticvoid
307-
open_logfile(void)
329+
open_logfile(FILE**file,constchar*filename_format)
308330
{
309331
char*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)
338355
fclose(log_file);
339356
log_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
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp