diff --git a/deps/log/log.c b/deps/log/log.c index 6e192d0..46d9c1f 100644 --- a/deps/log/log.c +++ b/deps/log/log.c @@ -41,6 +41,9 @@ typedef enum { struct log_handle { int level; int enable; + int split_file_by_size; + size_t max_file_size_mb; + int file_index; FILE *fp; va_list ap; char defined_log_fn[1024]; @@ -145,9 +148,29 @@ int log_create_log_file(struct log_handle *handle) if (NULL == (localtime_r(&t, &local_time))) { return 0; } - snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d", + snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d-%03d", handle->defined_log_fn, local_time.tm_year + 1900, - local_time.tm_mon + 1, local_time.tm_mday); + local_time.tm_mon + 1, local_time.tm_mday, handle->file_index); + + if (handle->split_file_by_size) { + struct stat file_stat; + + if (0 != memcmp(tmp_log_file_name, handle->runtime_log_fn, strlen(tmp_log_file_name))) {//new timestamp, reset file index + handle->file_index = 0; + snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d-%03d", + handle->defined_log_fn, local_time.tm_year + 1900, + local_time.tm_mon + 1, local_time.tm_mday, handle->file_index); + } + + if (stat(tmp_log_file_name, &file_stat) == 0) { + if (file_stat.st_size >= (handle->max_file_size_mb * 1024 * 1024)) { + handle->file_index++; + snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d-%03d", + handle->defined_log_fn, local_time.tm_year + 1900, + local_time.tm_mon + 1, local_time.tm_mday, handle->file_index); + } + } + } if (handle->fp == NULL) { if (0 != log_open_file(tmp_log_file_name, handle)) @@ -219,6 +242,14 @@ void log_handle_set_enable(struct log_handle * handle, int enable) } } +void log_handle_set_file_max_size(struct log_handle *handle, size_t max_file_size_mb) +{ + if (handle != NULL) { + handle->split_file_by_size = 1; + handle->max_file_size_mb = max_file_size_mb; + } +} + struct log_handle *log_handle_create(const char *file_path, int level) { struct log_handle *handle = ALLOC(struct log_handle, 1); diff --git a/deps/log/log.h b/deps/log/log.h index 681fb66..c125f77 100644 --- a/deps/log/log.h +++ b/deps/log/log.h @@ -41,6 +41,7 @@ enum { void log_print(struct log_handle *, int level, const char *module, const char *fmt, ...); void log_handle_set_enable(struct log_handle *, int enable); void log_handle_set_level(struct log_handle *, int level); +void log_handle_set_file_max_size(struct log_handle *, size_t max_file_size_mb); struct log_handle * log_handle_create(const char *file_path, int level); void log_handle_destroy(struct log_handle *); diff --git a/include/maat.h b/include/maat.h index fe2991d..bb9b97f 100644 --- a/include/maat.h +++ b/include/maat.h @@ -133,6 +133,8 @@ int maat_options_set_foreign_cont_dir(struct maat_options *opts, const char *dir int maat_options_set_logger(struct maat_options *opts, const char *log_path, enum log_level level); +int maat_options_set_log_file_max_size(struct maat_options *opts, size_t max_size_mb);//default is 500MB, 0 for unlimited + int maat_options_set_iris(struct maat_options *opts, const char *full_directory, const char *increment_directory); diff --git a/src/inc_internal/maat_rule.h b/src/inc_internal/maat_rule.h index ae84de8..a2c1404 100644 --- a/src/inc_internal/maat_rule.h +++ b/src/inc_internal/maat_rule.h @@ -113,6 +113,7 @@ struct maat_options { char decrypt_key[MAX_KEYWORDS_STR_LEN]; char log_path[PATH_MAX]; int log_level; + size_t log_file_max_size_mb; char stat_file[NAME_MAX]; size_t nr_worker_thread; char *accept_tags; diff --git a/src/maat_api.c b/src/maat_api.c index de8eb6b..b6b7ba0 100644 --- a/src/maat_api.c +++ b/src/maat_api.c @@ -77,6 +77,7 @@ struct maat_options* maat_options_new(void) options->input_mode = DATA_SOURCE_NONE; options->expr_engine = MAAT_EXPR_ENGINE_AUTO; options->log_level = 0; + options->log_file_max_size_mb = 500; return options; } @@ -311,6 +312,17 @@ int maat_options_set_logger(struct maat_options *opts, const char *log_path, return 0; } +int maat_options_set_log_file_max_size(struct maat_options *opts, size_t max_size_mb) +{ + if (NULL == opts || max_size_mb <= 0) { + return -1; + } + + opts->log_file_max_size_mb = max_size_mb; + + return 0; +} + static void _maat_free(struct maat *maat_inst) { if (NULL == maat_inst) { @@ -365,6 +377,9 @@ struct maat *maat_new(struct maat_options *opts, const char *table_info_path) } maat_inst->logger = log_handle_create(log_path, maat_inst->opts.log_level); } + if (maat_inst->opts.log_file_max_size_mb != 0) { + log_handle_set_file_max_size(maat_inst->logger, maat_inst->opts.log_file_max_size_mb); + } if (NULL == maat_inst->logger) { fprintf(stderr, "create log handle failed.\n"); diff --git a/src/maat_stat.c b/src/maat_stat.c index 83a6942..ee9e8db 100644 --- a/src/maat_stat.c +++ b/src/maat_stat.c @@ -243,9 +243,9 @@ static void fs_table_row_output(FILE *fp, struct maat_stat *stat, int perf_on) long long total_scan_times = 0, total_hit_times = 0, total_scan_cpu_time = 0; long long total_regv6_num = 0, total_hit_item_num = 0, total_hit_pattern_num = 0; long long g2c_not_clause_num = 0, g2g_excl_rule_num = 0; - struct fieldstat_tag cell_tag = { + struct field cell_tag = { .key = "TBL", - .type = TAG_CSTRING + .type = FIELD_VALUE_CSTRING }; size_t max_table_count = table_manager_table_size(stat->ref_tbl_mgr);