Skip to content

Commit e56ae30

Browse files
committed
Working on log_rotation_age
1 parent 1a6ee7a commit e56ae30

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

utils/logger.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,29 +329,59 @@ static void
329329
open_logfile(FILE **file, const char *filename_format)
330330
{
331331
char *filename;
332-
struct stat st;
332+
struct stat st;
333333
bool rotation_requested = false;
334334

335335
filename = logfile_getname(filename_format, time(NULL));
336336

337-
/* First check for rotation by size */
338-
if (log_rotation_size > 0)
337+
/* First check for rotation */
338+
if (log_rotation_size > 0 || log_rotation_age > 0)
339339
{
340340
if (stat(filename, &st) == -1)
341341
{
342342
if (errno == ENOENT)
343343
{
344344
/* There is no file "filename" and rotation does not need */
345+
goto logfile_open;
345346
}
346347
else
347348
elog(ERROR, "cannot stat log file \"%s\": %s",
348349
filename, strerror(errno));
349350
}
350351
/* Found log file "filename" */
351-
else
352+
353+
/* Check for rotation by age */
354+
if (log_rotation_age > 0)
355+
{
356+
char control[MAXPGPATH];
357+
struct stat control_st;
358+
FILE *control_file;
359+
360+
snprintf(control, MAXPGPATH, "%s.rotation", filename);
361+
if (stat(control, &control_st) == -1)
362+
{
363+
if (errno == ENOENT)
364+
{
365+
/* There is no control file for rotation */
366+
goto logfile_open;
367+
}
368+
else
369+
elog(ERROR, "cannot stat rotation file \"%s\": %s",
370+
control, strerror(errno));
371+
}
372+
373+
/* Found control file for rotation */
374+
375+
control_file = fopen(control, "r");
376+
fclose(control_file);
377+
}
378+
379+
/* Check for rotation by size */
380+
if (!rotation_requested && log_rotation_size > 0)
352381
rotation_requested = (st.st_size >= log_rotation_size * 1024L);
353382
}
354383

384+
logfile_open:
355385
if (rotation_requested)
356386
*file = logfile_open(filename, "w");
357387
else

0 commit comments

Comments
 (0)