Skip to content

Commit

Permalink
initial changes for limiting/rotating fluent-bit logs
Browse files Browse the repository at this point in the history
  • Loading branch information
phiremande committed Jan 24, 2020
1 parent 8e96609 commit 1c30efa
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/flb_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ static inline void log_file_rotate(struct flb_log *log)
{
char *backup_file_name = NULL;
char *src_file_name = NULL;
char dest_file_suffix[4];
char src_file_suffix[4];
int ret = -1;

if (log->max_history == 0) {
Expand All @@ -98,31 +96,26 @@ static inline void log_file_rotate(struct flb_log *log)
}

for (int l = log->max_history; l > 1; l--) {
sprintf(dest_file_suffix, ".%d", l);
sprintf(src_file_suffix, ".%d", l-1);
strcpy(backup_file_name, log->out);
strcat(backup_file_name, dest_file_suffix);
strcpy(src_file_name, log->out);
strcat(src_file_name, src_file_suffix);
sprintf(backup_file_name, "%s.%d", log->out, l);
sprintf(src_file_name, "%s.%d", log->out, l-1);
//since rename behaviour is implementation dependent when new file exists, remove the last file.
if (l == log->max_history) {
remove(backup_file_name);
}
ret = rename(src_file_name, backup_file_name);
if (ret != 0) {
//we could not rename delete the source file.
//we could not rename; delete the source file.
remove(src_file_name);
}
}
if (src_file_name) {
flb_free(src_file_name);
}

strcpy(backup_file_name, log->out);
strcat(backup_file_name, ".1");
sprintf(backup_file_name, "%s.1", log->out);
ret = rename(log->out, backup_file_name);
if (ret != 0) {
//we could not rename delete the source file.
//we could not rename; delete the source file.
remove(log->out);
}
if (backup_file_name) {
Expand Down Expand Up @@ -268,11 +261,13 @@ int flb_log_set_file(struct flb_config *config, char *out)
int flb_log_set_history(struct flb_config *config, uint8_t max_history)
{
config->log->max_history = max_history;
return 0;
}

int flb_log_set_size(struct flb_config *config, uint16_t max_sz_mb)
{
config->log->max_sz_mb = max_sz_mb;
return 0;
}

struct flb_log *flb_log_init(struct flb_config *config, int type,
Expand Down

0 comments on commit 1c30efa

Please sign in to comment.