Skip to content

Commit 2388ace

Browse files
author
Alexander Zikulnig
committed
affile: add pending flag to lograte_options
The pending flag should signal if a rotation is currently ongoing. If all log files have been rotated but the main log file could not be reopened, then the rotation should not be performed again. Signed-off-by: Alexander Zikulnig <[email protected]>
1 parent 5dcf6bd commit 2388ace

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

modules/affile/affile-dest.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,20 @@ affile_dw_logrotate(AFFileDestWriter *self, gpointer user_data)
240240

241241
if (logrotate_is_enabled(logrotate_options) && logrotate_is_required(logrotate_options, self->cached_filesize))
242242
{
243-
LogRotateStatus status = logrotate_do_rotate(logrotate_options, filename);
244-
245-
if (status != LR_SUCCESS)
243+
// rotate only if there has not been an unfinished rotation before
244+
if (!logrotate_is_pending(logrotate_options))
246245
{
247-
msg_error("Error while rotating log files", evt_tag_str("filename", self->filename));
248-
return FALSE;
246+
LogRotateStatus status = logrotate_do_rotate(logrotate_options, filename);
247+
248+
if (status != LR_SUCCESS)
249+
{
250+
msg_error("Error while rotating log files", evt_tag_str("filename", self->filename));
251+
return FALSE;
252+
}
249253
}
250254

255+
logrotate_options->pending = TRUE;
256+
251257
// The reference pointed to by p is only updated if the reopen was successful,
252258
// e.g. open_result == FILE_OPENER_RESULT_SUCCESS
253259
FileOpenerResult open_result = _dw_reopen(self, p);
@@ -256,7 +262,7 @@ affile_dw_logrotate(AFFileDestWriter *self, gpointer user_data)
256262
/* best case --> continue with opened file */
257263
msg_info("Reopened log file after logrotate",
258264
evt_tag_str("filename", self->filename));
259-
265+
logrotate_options->pending = FALSE;
260266
}
261267
else
262268
{
@@ -265,6 +271,7 @@ affile_dw_logrotate(AFFileDestWriter *self, gpointer user_data)
265271
/* try again to reopen */
266272
msg_info("Trying again to re-open file after logrotate", evt_tag_str("filename", self->filename));
267273
gboolean success = (gboolean) main_loop_call((MainLoopTaskFunc) affile_dw_reopen, (gpointer) self, TRUE);
274+
logrotate_options->pending = !success;
268275
return success;
269276
}
270277
else

modules/affile/logrotate.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void logrotate_options_defaults(LogRotateOptions *logrotate_options)
4040
logrotate_options->max_rotations = LR_DEFAULT_ROTATIONS;
4141
logrotate_options->interval = NONE;
4242
logrotate_options->date_format = LR_DEFAULT_DATE_FORMAT;
43+
logrotate_options->pending = FALSE;
4344
}
4445

4546
gboolean logrotate_is_enabled(LogRotateOptions *logrotate_options)
@@ -64,6 +65,17 @@ gboolean logrotate_is_required(LogRotateOptions *logrotate_options, const gsize
6465
return (filesize >= logrotate_options->size);
6566
}
6667

68+
gboolean logrotate_is_pending(LogRotateOptions *logrotate_options)
69+
{
70+
71+
if (logrotate_options == NULL)
72+
{
73+
return FALSE;
74+
}
75+
76+
return logrotate_options->pending;
77+
}
78+
6779
gchar *get_log_file_name(const gchar *filename, gsize rotation_suffix)
6880
{
6981
return g_strdup_printf("%s.%ld", filename, rotation_suffix);

modules/affile/logrotate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ typedef struct _LogRotateOptions
4545
gsize max_rotations;
4646
interval_t interval;
4747
const gchar *date_format;
48+
gboolean pending;
4849
} LogRotateOptions;
4950

5051
gboolean logrotate_is_enabled(LogRotateOptions *logrotate_options);
5152
gboolean logrotate_is_required(LogRotateOptions *logrotate_options, const gsize filesize);
53+
gboolean logrotate_is_pending(LogRotateOptions *logrotate_options);
5254
void logrotate_options_defaults(LogRotateOptions *logrotate_options);
5355
LogRotateStatus logrotate_do_rotate(LogRotateOptions *logrotate_options, const gchar *filename);
5456

0 commit comments

Comments
 (0)