Skip to content

Commit 866e838

Browse files
committed
update fluent auto CR comments
1 parent d26d5e8 commit 866e838

File tree

4 files changed

+57
-37
lines changed

4 files changed

+57
-37
lines changed

conf/fluent-bit-logrotate.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Path /tmp/logs
1616
File test.log
1717
Format json
18-
Max_Size 10
18+
Max_Size 10M
1919
Max_Files 5
2020
Gzip On
2121
Mkdir On
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(src
22
logrotate.c)
33

4-
FLB_PLUGIN(out_logrotate "${src}" "")
4+
find_package(ZLIB REQUIRED)
5+
FLB_PLUGIN(out_logrotate "${src}" "${ZLIB_LIBRARIES}")

plugins/out_logrotate/logrotate.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
#include <string.h>
3737
#include <time.h>
3838
#include <miniz/miniz.h>
39+
#include <limits.h> /* PATH_MAX */
40+
#include <inttypes.h> /* PRIu64 */
41+
#ifndef FLB_SYSTEM_WINDOWS
42+
#include <libgen.h> /* dirname */
43+
#endif
3944

4045
#ifdef FLB_SYSTEM_WINDOWS
4146
#include <Shlobj.h>
@@ -465,7 +470,7 @@ static int mkpath(struct flb_output_instance *ins, const char *dir)
465470

466471
return 0;
467472
#elif FLB_SYSTEM_MACOS
468-
dup_dir = strdup(dir);
473+
dup_dir = flb_strdup(dir);
469474
if (!dup_dir) {
470475
return -1;
471476
}
@@ -479,27 +484,27 @@ static int mkpath(struct flb_output_instance *ins, const char *dir)
479484
if (S_ISDIR (st.st_mode)) {
480485
flb_plg_debug(ins, "creating directory %s", dup_dir);
481486
ret = mkdir(dup_dir, 0755);
482-
free(dup_dir);
487+
flb_free(dup_dir);
483488
return ret;
484489
}
485490
}
486491

487492
ret = mkpath(ins, dirname(dup_dir));
488493
if (ret != 0) {
489-
free(dup_dir);
494+
flb_free(dup_dir);
490495
return ret;
491496
}
492497
flb_plg_debug(ins, "creating directory %s", dup_dir);
493498
ret = mkdir(dup_dir, 0755);
494-
free(dup_dir);
499+
flb_free(dup_dir);
495500
return ret;
496501
#else
497-
dup_dir = strdup(dir);
502+
dup_dir = flb_strdup(dir);
498503
if (!dup_dir) {
499504
return -1;
500505
}
501506
ret = mkpath(ins, dirname(dup_dir));
502-
free(dup_dir);
507+
flb_free(dup_dir);
503508
if (ret != 0) {
504509
return ret;
505510
}
@@ -517,18 +522,23 @@ static int should_rotate_file(struct flb_logrotate_conf *ctx)
517522
/* Function to update file size counter using current file position */
518523
static void update_file_size_counter(struct flb_logrotate_conf *ctx, FILE *fp)
519524
{
520-
long current_pos = ftell(fp);
521-
if (current_pos >= 0) {
522-
ctx->current_file_size = (size_t)current_pos;
525+
struct stat st;
526+
if (fstat(fileno(fp), &st) == 0 && st.st_size >= 0) {
527+
ctx->current_file_size = (size_t) st.st_size;
523528
}
524529
}
525530

526531
/* Function to generate timestamp for rotated file */
527532
static void generate_timestamp(char *timestamp, size_t size)
528533
{
529534
time_t now = time(NULL);
530-
struct tm *tm_info = localtime(&now);
531-
strftime(timestamp, size, "%Y%m%d_%H%M%S", tm_info);
535+
struct tm tm_info;
536+
#ifdef FLB_SYSTEM_WINDOWS
537+
localtime_s(&tm_info, &now);
538+
#else
539+
localtime_r(&now, &tm_info);
540+
#endif
541+
strftime(timestamp, size, "%Y%m%d_%H%M%S", &tm_info);
532542
}
533543

534544
/* Helper function to write gzip header (based on flb_gzip.c) */
@@ -619,7 +629,7 @@ static int gzip_compress_file(const char *input_filename, const char *output_fil
619629
goto cleanup;
620630
}
621631

622-
/* Process file in chunks */
632+
/* Process file in chunks (ensure Z_FINISH is always issued) */
623633
do {
624634
bytes_read = fread(input_buffer, 1, GZIP_CHUNK_SIZE, src_fp);
625635
if (bytes_read > 0) {
@@ -631,7 +641,7 @@ static int gzip_compress_file(const char *input_filename, const char *output_fil
631641
strm.next_in = (Bytef *)input_buffer;
632642
strm.avail_in = bytes_read;
633643

634-
/* Determine flush mode */
644+
/* Determine flush mode based on EOF after this read */
635645
flush = feof(src_fp) ? Z_FINISH : Z_NO_FLUSH;
636646

637647
/* Compress chunk */
@@ -869,7 +879,7 @@ static void cb_logrotate_flush(struct flb_event_chunk *event_chunk,
869879
/* Check if file needs rotation based on current size counter */
870880
if (should_rotate_file(ctx)) {
871881
/* Extract directory and base filename for cleanup */
872-
out_file_copy = strdup(out_file);
882+
out_file_copy = flb_strdup(out_file);
873883
if (out_file_copy) {
874884
#ifdef FLB_SYSTEM_WINDOWS
875885
PathRemoveFileSpecA(out_file_copy);
@@ -879,7 +889,7 @@ static void cb_logrotate_flush(struct flb_event_chunk *event_chunk,
879889
strncpy(directory, dirname(out_file_copy), PATH_MAX - 1);
880890
directory[PATH_MAX - 1] = '\0';
881891
#endif
882-
free(out_file_copy);
892+
flb_free(out_file_copy);
883893
}
884894

885895
/* Get base filename for cleanup */
@@ -903,15 +913,15 @@ static void cb_logrotate_flush(struct flb_event_chunk *event_chunk,
903913
/* Open output file with default name as the Tag */
904914
fp = fopen(out_file, "ab+");
905915
if (ctx->mkdir == FLB_TRUE && fp == NULL && errno == ENOENT) {
906-
out_file_copy = strdup(out_file);
916+
out_file_copy = flb_strdup(out_file);
907917
if (out_file_copy) {
908918
#ifdef FLB_SYSTEM_WINDOWS
909919
PathRemoveFileSpecA(out_file_copy);
910920
ret = mkpath(ctx->ins, out_file_copy);
911921
#else
912922
ret = mkpath(ctx->ins, dirname(out_file_copy));
913923
#endif
914-
free(out_file_copy);
924+
flb_free(out_file_copy);
915925
if (ret == 0) {
916926
fp = fopen(out_file, "ab+");
917927
}

tests/runtime/out_logrotate.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void flb_test_logrotate_basic_rotation(void)
115115
TEST_CHECK(out_ffd >= 0);
116116
flb_output_set(ctx, out_ffd, "match", "test", NULL);
117117
flb_output_set(ctx, out_ffd, "file", TEST_LOGFILE, NULL);
118-
flb_output_set(ctx, out_ffd, "max_size", "1", NULL); /* 1 MB */
118+
flb_output_set(ctx, out_ffd, "max_size", "1M", NULL); /* 1 MB */
119119
flb_output_set(ctx, out_ffd, "max_files", "3", NULL);
120120
flb_output_set(ctx, out_ffd, "gzip", "false", NULL);
121121

@@ -142,13 +142,8 @@ void flb_test_logrotate_basic_rotation(void)
142142
fclose(fp);
143143
}
144144

145-
/* Check that a rotated file was created */
146-
fp = fopen(rotated_file, "r");
147-
TEST_CHECK(fp != NULL);
148-
if (fp != NULL) {
149-
fclose(fp);
150-
remove(rotated_file);
151-
}
145+
/* Check that at least one rotated file exists: "flb_test_logrotate.log.*" */
146+
TEST_CHECK(count_files_in_directory(".", "flb_test_logrotate.log.") >= 1);
152147

153148
remove(TEST_LOGFILE);
154149
}
@@ -187,7 +182,7 @@ void flb_test_logrotate_gzip_compression(void)
187182
TEST_CHECK(out_ffd >= 0);
188183
flb_output_set(ctx, out_ffd, "match", "test", NULL);
189184
flb_output_set(ctx, out_ffd, "file", TEST_LOGFILE, NULL);
190-
flb_output_set(ctx, out_ffd, "max_size", "1", NULL); /* 1 MB */
185+
flb_output_set(ctx, out_ffd, "max_size", "1M", NULL); /* 1 MB */
191186
flb_output_set(ctx, out_ffd, "max_files", "3", NULL);
192187
flb_output_set(ctx, out_ffd, "gzip", "true", NULL);
193188

@@ -206,10 +201,23 @@ void flb_test_logrotate_gzip_compression(void)
206201
flb_stop(ctx);
207202
flb_destroy(ctx);
208203

209-
/* Check that a gzipped rotated file was created */
210-
snprintf(rotated_file, sizeof(rotated_file), "%s.%s", TEST_LOGFILE, timestamp);
211-
ret = check_gzip_file_exists(rotated_file);
212-
TEST_CHECK(ret == 1);
204+
/* Check that a gzipped rotated file exists: "flb_test_logrotate.log.*.gz" */
205+
ret = 0;
206+
{
207+
DIR *dir = opendir(".");
208+
struct dirent *entry;
209+
if (dir) {
210+
while ((entry = readdir(dir)) != NULL) {
211+
if (strstr(entry->d_name, "flb_test_logrotate.log.") == entry->d_name &&
212+
strstr(entry->d_name, ".gz") != NULL) {
213+
ret = 1;
214+
break;
215+
}
216+
}
217+
closedir(dir);
218+
}
219+
}
220+
TEST_CHECK(ret == 1);
213221

214222
/* Clean up */
215223
remove(rotated_file);
@@ -241,7 +249,7 @@ void flb_test_logrotate_max_files_cleanup(void)
241249
TEST_CHECK(out_ffd >= 0);
242250
flb_output_set(ctx, out_ffd, "match", "test", NULL);
243251
flb_output_set(ctx, out_ffd, "file", TEST_LOGFILE, NULL);
244-
flb_output_set(ctx, out_ffd, "max_size", "1", NULL); /* 1 MB */
252+
flb_output_set(ctx, out_ffd, "max_size", "1M", NULL); /* 1 MB */
245253
flb_output_set(ctx, out_ffd, "max_files", "2", NULL); /* Only keep 2 files */
246254
flb_output_set(ctx, out_ffd, "gzip", "false", NULL);
247255

@@ -293,7 +301,7 @@ void flb_test_logrotate_counter_based_size(void)
293301
TEST_CHECK(out_ffd >= 0);
294302
flb_output_set(ctx, out_ffd, "match", "test", NULL);
295303
flb_output_set(ctx, out_ffd, "file", TEST_LOGFILE, NULL);
296-
flb_output_set(ctx, out_ffd, "max_size", "1", NULL); /* 1 MB */
304+
flb_output_set(ctx, out_ffd, "max_size", "1M", NULL); /* 1 MB */
297305
flb_output_set(ctx, out_ffd, "max_files", "3", NULL);
298306
flb_output_set(ctx, out_ffd, "gzip", "false", NULL);
299307

@@ -352,7 +360,8 @@ void flb_test_logrotate_different_formats(void)
352360
flb_output_set(ctx, out_ffd, "match", "test", NULL);
353361
flb_output_set(ctx, out_ffd, "file", TEST_LOGFILE, NULL);
354362
flb_output_set(ctx, out_ffd, "format", "csv", NULL);
355-
flb_output_set(ctx, out_ffd, "max_size", "10", NULL); /* 10 MB */
363+
flb_output_set(ctx, out_ffd, "csv_column_names", "true", NULL);
364+
flb_output_set(ctx, out_ffd, "max_size", "10M", NULL); /* 10 MB */
356365
flb_output_set(ctx, out_ffd, "max_files", "3", NULL);
357366
flb_output_set(ctx, out_ffd, "gzip", "false", NULL);
358367

@@ -420,7 +429,7 @@ void flb_test_logrotate_mkdir_support(void)
420429
flb_output_set(ctx, out_ffd, "match", "test", NULL);
421430
flb_output_set(ctx, out_ffd, "path", TEST_LOGPATH, NULL);
422431
flb_output_set(ctx, out_ffd, "mkdir", "true", NULL);
423-
flb_output_set(ctx, out_ffd, "max_size", "10", NULL);
432+
flb_output_set(ctx, out_ffd, "max_size", "10M", NULL);
424433
flb_output_set(ctx, out_ffd, "max_files", "3", NULL);
425434
flb_output_set(ctx, out_ffd, "gzip", "false", NULL);
426435

@@ -474,7 +483,7 @@ void flb_test_logrotate_performance_test(void)
474483
TEST_CHECK(out_ffd >= 0);
475484
flb_output_set(ctx, out_ffd, "match", "test", NULL);
476485
flb_output_set(ctx, out_ffd, "file", TEST_LOGFILE, NULL);
477-
flb_output_set(ctx, out_ffd, "max_size", "10", NULL); /* 10 MB */
486+
flb_output_set(ctx, out_ffd, "max_size", "10M", NULL); /* 10 MB */
478487
flb_output_set(ctx, out_ffd, "max_files", "3", NULL);
479488
flb_output_set(ctx, out_ffd, "gzip", "false", NULL);
480489

0 commit comments

Comments
 (0)