[FEATURE]maat support dynamic reload log_level

This commit is contained in:
liuwentan
2023-12-05 16:31:18 +08:00
parent f48afd54f6
commit 59ee9364c9
9 changed files with 98 additions and 122 deletions

123
deps/log/log.c vendored
View File

@@ -38,8 +38,7 @@ typedef enum {
RT_LOG_OP_IFACE_MAX, RT_LOG_OP_IFACE_MAX,
}log_op_iface; }log_op_iface;
struct log_handle struct log_handle {
{
int level; int level;
int enable; int enable;
FILE *fp; FILE *fp;
@@ -56,49 +55,51 @@ static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr","May", "Jun"
static int log_create_dir(const char *dir_path, int path_len) static int log_create_dir(const char *dir_path, int path_len)
{ {
if(dir_path == NULL) if (dir_path == NULL) {
return -1; return -1;
}
char *buf = (char *)calloc(path_len+1, 1); char *buf = (char *)calloc(path_len + 1, 1);
int ret = -1; int ret = -1;
memcpy(buf, dir_path, path_len); memcpy(buf, dir_path, path_len);
if(access(buf, R_OK) != 0) if (access(buf, R_OK) != 0) {
{ if (mkdir(buf, 0755) != 0) {
if(mkdir(buf, 0755)!= 0)
ret = -1; ret = -1;
else } else {
ret = 0; ret = 0;
} }
else } else {
ret = 1; ret = 1;
}
free(buf); free(buf);
buf = NULL; buf = NULL;
return ret; return ret;
} }
static void log_close_file(struct log_handle *handle) static void log_close_file(struct log_handle *handle)
{ {
pthread_mutex_lock(&handle->mutex); pthread_mutex_lock(&handle->mutex);
if(handle->fp != NULL) if (handle->fp != NULL) {
{
fclose(handle->fp); fclose(handle->fp);
handle->fp = NULL; handle->fp = NULL;
} }
pthread_mutex_unlock(&handle->mutex); pthread_mutex_unlock(&handle->mutex);
return;
} }
int log_open_file(char *file_name, struct log_handle *handle) int log_open_file(char *file_name, struct log_handle *handle)
{ {
FILE *fp = NULL; FILE *fp = NULL;
log_close_file(handle); log_close_file(handle);
if(NULL == (fp = fopen(file_name, "a"))) if (NULL == (fp = fopen(file_name, "a"))) {
{
return -1; return -1;
} }
memcpy(handle->runtime_log_fn, file_name, strlen(file_name)); memcpy(handle->runtime_log_fn, file_name, strlen(file_name));
handle->fp = fp; handle->fp = fp;
return 0; return 0;
} }
@@ -106,12 +107,11 @@ static int log_create_path(const char *file_path)
{ {
FILE *fp = NULL; FILE *fp = NULL;
if(file_path == NULL) if (file_path == NULL)
return 0; return 0;
char *p_path = rindex(file_path, '/'); char *p_path = rindex(file_path, '/');
if(p_path==0) if (p_path == 0) {
{
return 0; return 0;
} }
@@ -119,22 +119,22 @@ static int log_create_path(const char *file_path)
int path_len = p_path - file_path; int path_len = p_path - file_path;
int i = 0; int i = 0;
if(log_create_dir(file_path, path_len) >= 0) if (log_create_dir(file_path, path_len) >= 0)
return 0; return 0;
for(;i<=path_len;i++,p_cur++) for (; i <= path_len; i++, p_cur++) {
{ if (*p_cur == '/') {
if(*p_cur == '/') if (log_create_dir(file_path, i + 1) < 0)
{
if(log_create_dir(file_path, i+1) < 0)
return -1; return -1;
} }
} }
if(NULL == (fp = fopen(file_path, "w")))
{ if (NULL == (fp = fopen(file_path, "w"))) {
return 0; return 0;
} }
fclose(fp); fclose(fp);
return 1; return 1;
} }
@@ -142,31 +142,32 @@ int log_create_log_file(struct log_handle *handle)
{ {
time_t t; time_t t;
struct tm local_time; struct tm local_time;
char tmp_log_file_name[1024+128]; char tmp_log_file_name[1024 + 128];
time(&t); time(&t);
if(NULL == (localtime_r(&t, &local_time))) if (NULL == (localtime_r(&t, &local_time))) {
{
return 0; return 0;
} }
snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d", handle->defined_log_fn, local_time.tm_year + 1900, local_time.tm_mon + 1, local_time.tm_mday); snprintf(tmp_log_file_name, sizeof(tmp_log_file_name), "%s.%04d-%02d-%02d",
handle->defined_log_fn, local_time.tm_year + 1900,
local_time.tm_mon + 1, local_time.tm_mday);
if(handle->fp == NULL) if (handle->fp == NULL) {
{ if (0 != log_open_file(tmp_log_file_name, handle))
if(0 != log_open_file(tmp_log_file_name, handle)) return 0; return 0;
} } else {
else if (0 != memcmp(tmp_log_file_name, handle->runtime_log_fn,
{ strlen(tmp_log_file_name))) {
if (0 != memcmp(tmp_log_file_name, handle->runtime_log_fn, strlen(tmp_log_file_name))) if (0 != log_open_file(tmp_log_file_name, handle))
{ return 0;
if(0 != log_open_file(tmp_log_file_name, handle))return 0;
} }
} }
return 1; return 1;
} }
static void log_print_file(struct log_handle *handle, int level, const char *module, va_list ap, const char *fmt) static void log_print_file(struct log_handle *handle, int level, const char *module,
va_list ap, const char *fmt)
{ {
char buf[64]={0}; char buf[64]={0};
time_t t; time_t t;
@@ -176,7 +177,8 @@ static void log_print_file(struct log_handle *handle, int level, const char *mod
time(&t); time(&t);
if(NULL == (localtime_r(&t, &local_time))) return; if(NULL == (localtime_r(&t, &local_time))) return;
snprintf(buf, sizeof(buf), "%s %s %d %02d:%02d:%02d %d", weekday_str[local_time.tm_wday], snprintf(buf, sizeof(buf), "%s %s %d %02d:%02d:%02d %d", weekday_str[local_time.tm_wday],
month_str[local_time.tm_mon], local_time.tm_mday, local_time.tm_hour, local_time.tm_min, local_time.tm_sec, local_time.tm_year+1900); month_str[local_time.tm_mon], local_time.tm_mday, local_time.tm_hour,
local_time.tm_min, local_time.tm_sec, local_time.tm_year+1900);
log_create_log_file(handle); log_create_log_file(handle);
fprintf(handle->fp, "%s, %s, %s, ", buf, level_str_map[level], module); fprintf(handle->fp, "%s, %s, %s, ", buf, level_str_map[level], module);
@@ -186,7 +188,8 @@ static void log_print_file(struct log_handle *handle, int level, const char *mod
fflush(handle->fp); fflush(handle->fp);
} }
static void log_print_console(struct log_handle *handle, int level, const char *module, va_list ap, const char *fmt) static void log_print_console(struct log_handle *handle, int level, const char *module,
va_list ap, const char *fmt)
{ {
char buf[64]={0}; char buf[64]={0};
time_t t; time_t t;
@@ -196,7 +199,8 @@ static void log_print_console(struct log_handle *handle, int level, const char *
time(&t); time(&t);
if(NULL == (localtime_r(&t, &local_time))) return; if(NULL == (localtime_r(&t, &local_time))) return;
snprintf(buf, sizeof(buf), "%s %s %d %02d:%02d:%02d %d", weekday_str[local_time.tm_wday], snprintf(buf, sizeof(buf), "%s %s %d %02d:%02d:%02d %d", weekday_str[local_time.tm_wday],
month_str[local_time.tm_mon], local_time.tm_mday, local_time.tm_hour, local_time.tm_min, local_time.tm_sec, local_time.tm_year+1900); month_str[local_time.tm_mon], local_time.tm_mday, local_time.tm_hour,
local_time.tm_min, local_time.tm_sec, local_time.tm_year+1900);
fprintf(handle->fp, "%s, %s, %s, ", buf, level_str_map[level], module); fprintf(handle->fp, "%s, %s, %s, ", buf, level_str_map[level], module);
vfprintf(handle->fp, fmt, ap); vfprintf(handle->fp, fmt, ap);
@@ -204,18 +208,16 @@ static void log_print_console(struct log_handle *handle, int level, const char *
fflush(handle->fp); fflush(handle->fp);
} }
void log_options_set_level(struct log_handle * handle, int level) void log_handle_set_level(struct log_handle * handle, int level)
{ {
if(handle != NULL) if (handle != NULL) {
{
handle->level = level; handle->level = level;
} }
} }
void log_options_set_enable(struct log_handle * handle, int enable) void log_handle_set_enable(struct log_handle * handle, int enable)
{ {
if(handle != NULL) if (handle != NULL) {
{
handle->enable = enable; handle->enable = enable;
} }
} }
@@ -223,17 +225,15 @@ void log_options_set_enable(struct log_handle * handle, int enable)
struct log_handle *log_handle_create(const char *file_path, int level) struct log_handle *log_handle_create(const char *file_path, int level)
{ {
struct log_handle *handle = ALLOC(struct log_handle, 1); struct log_handle *handle = ALLOC(struct log_handle, 1);
if(!handle) if (!handle) {
{
return NULL; return NULL;
} }
handle->enable=1; handle->enable = 1;
handle->level = level; handle->level = level;
strncpy(handle->defined_log_fn, file_path, 1024); strncpy(handle->defined_log_fn, file_path, 1024);
pthread_mutex_init(&handle->mutex,NULL); pthread_mutex_init(&handle->mutex, NULL);
if(handle->enable) if (handle->enable) {
{
log_create_path(handle->defined_log_fn); log_create_path(handle->defined_log_fn);
} }
@@ -242,37 +242,32 @@ struct log_handle *log_handle_create(const char *file_path, int level)
void log_handle_destroy(struct log_handle * handle) void log_handle_destroy(struct log_handle * handle)
{ {
if(!handle) if (!handle) {
{
return; return;
} }
if(handle->iface == LOG_OP_IFACE_FILE && handle->fp != NULL) if (handle->iface == LOG_OP_IFACE_FILE && handle->fp != NULL) {
{
fclose(handle->fp); fclose(handle->fp);
handle->fp=NULL; handle->fp = NULL;
} }
pthread_mutex_destroy(&(handle->mutex)); pthread_mutex_destroy(&(handle->mutex));
free(handle); free(handle);
handle = NULL; handle = NULL;
return;
} }
void log_print(struct log_handle *handle, int level, const char *module, const char *fmt, ...) void log_print(struct log_handle *handle, int level, const char *module, const char *fmt, ...)
{ {
va_list ap; va_list ap;
if(handle->enable != 1 && level >= handle->level) if (handle->enable != 1 && level >= handle->level) {
{
handle->fp = stdout; handle->fp = stdout;
handle->iface = LOG_OP_IFACE_CONSOLE; handle->iface = LOG_OP_IFACE_CONSOLE;
va_start(handle->ap, fmt); va_start(handle->ap, fmt);
log_print_console(handle, level, module, ap, fmt); log_print_console(handle, level, module, ap, fmt);
va_end(handle->ap); va_end(handle->ap);
} }
if (handle->enable == 1 && level >= handle->level) if (handle->enable == 1 && level >= handle->level) {
{
handle->iface = LOG_OP_IFACE_FILE; handle->iface = LOG_OP_IFACE_FILE;
va_start(ap, fmt); va_start(ap, fmt);
log_print_file(handle, level, module, ap, fmt); log_print_file(handle, level, module, ap, fmt);

4
deps/log/log.h vendored
View File

@@ -39,8 +39,8 @@ enum {
#define log_fatal(handle, module, fmt, ...) log_print(handle, LOG_FATAL, module, fmt, ##__VA_ARGS__) #define log_fatal(handle, module, fmt, ...) log_print(handle, LOG_FATAL, module, fmt, ##__VA_ARGS__)
void log_print(struct log_handle *, int level, const char *module, const char *fmt, ...); void log_print(struct log_handle *, int level, const char *module, const char *fmt, ...);
void log_options_set_enable(struct log_handle *, int enable); void log_handle_set_enable(struct log_handle *, int enable);
void log_options_set_level(struct log_handle *, int level); void log_handle_set_level(struct log_handle *, int level);
struct log_handle * log_handle_create(const char *file_path, int level); struct log_handle * log_handle_create(const char *file_path, int level);
void log_handle_destroy(struct log_handle *); void log_handle_destroy(struct log_handle *);

View File

@@ -163,10 +163,13 @@ int maat_options_set_hit_group_enabled(struct maat_options *opts);
struct maat *maat_new(struct maat_options *opts, const char *table_info_path); struct maat *maat_new(struct maat_options *opts, const char *table_info_path);
void maat_free(struct maat *instance); void maat_free(struct maat *instance);
void maat_reload_log_level(struct maat *instance, enum log_level level);
/** /**
* Each thread can call this function initially, maat will maintain the thread_id internally, * Each thread can call this function initially, maat will maintain the
* So it's no need to pass thread_id by maat_scan_xx and xx_plugin_get_ex_data API * thread_id internally, So it's no need to pass thread_id by maat_scan_xx and
*/ * xx_plugin_get_ex_data API
*/
void maat_register_thread(struct maat *instance); void maat_register_thread(struct maat *instance);
/* maat helper API */ /* maat helper API */

View File

@@ -443,6 +443,12 @@ void maat_free(struct maat *maat_inst)
pthread_join(maat_inst->cfg_mon_thread, &ret); pthread_join(maat_inst->cfg_mon_thread, &ret);
} }
void maat_reload_log_level(struct maat *maat_inst, enum log_level level)
{
maat_inst->opts.log_level = level;
log_handle_set_level(maat_inst->logger, level);
}
__thread int _thread_local_tid = -1; __thread int _thread_local_tid = -1;
void maat_register_thread(struct maat *maat_inst) void maat_register_thread(struct maat *maat_inst)
{ {
@@ -542,7 +548,8 @@ int maat_table_callback_register(struct maat *maat_inst, int table_id,
if (table_type != TABLE_TYPE_PLUGIN) { if (table_type != TABLE_TYPE_PLUGIN) {
pthread_mutex_unlock(&(maat_inst->background_update_mutex)); pthread_mutex_unlock(&(maat_inst->background_update_mutex));
log_fatal(maat_inst->logger, MODULE_MAAT_API, log_fatal(maat_inst->logger, MODULE_MAAT_API,
"[%s:%d] table type:%d illegal", __FUNCTION__, __LINE__, table_type); "[%s:%d] table type:%d illegal",
__FUNCTION__, __LINE__, table_type);
return -1; return -1;
} }

View File

@@ -1370,7 +1370,7 @@ static int maat_add_group_to_compile(struct rcu_hash_table *hash_tbl,
} }
} }
return ret; return 0;
} }
static int maat_remove_group_from_compile(struct rcu_hash_table *hash_tbl, static int maat_remove_group_from_compile(struct rcu_hash_table *hash_tbl,
@@ -1450,7 +1450,7 @@ static int maat_remove_group_from_compile(struct rcu_hash_table *hash_tbl,
} }
} }
return ret; return 0;
} }
struct compile_state *compile_state_new(void) struct compile_state *compile_state_new(void)

View File

@@ -181,10 +181,7 @@ static int cm_read_table_file(struct cm_table_info_t *index,
continue; continue;
} }
ret = update_fn(index->table_name, line, u_param); update_fn(index->table_name, line, u_param);
if (ret < 0) {
break;
}
} }
FREE(file_buff); FREE(file_buff);

View File

@@ -6,6 +6,7 @@ global:
maat_new; maat_new;
maat_free; maat_free;
maat_get_table_id; maat_get_table_id;
maat_reload_log_level;
maat_table*; maat_table*;
maat_compile_table*; maat_compile_table*;
maat_plugin_table*; maat_plugin_table*;

View File

@@ -6999,7 +6999,7 @@ protected:
struct maat_options *opts = maat_options_new(); struct maat_options *opts = maat_options_new();
maat_options_set_redis(opts, redis_ip, redis_port, redis_db); maat_options_set_redis(opts, redis_ip, redis_port, redis_db);
maat_options_set_stat_file(opts, "./stat.log"); maat_options_set_stat_file(opts, "./stat.log");
maat_options_set_logger(opts, "./maat_framework_gtest.log", LOG_LEVEL_INFO); maat_options_set_logger(opts, "./maat_framework_gtest.log", LOG_LEVEL_FATAL);
maat_options_set_hit_path_enabled(opts); maat_options_set_hit_path_enabled(opts);
maat_options_set_hit_group_enabled(opts); maat_options_set_hit_group_enabled(opts);
@@ -7038,6 +7038,7 @@ TEST_F(MaatCmdTest, SetIP) {
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT"; const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
struct maat *maat_inst = MaatCmdTest::_shared_maat_inst; struct maat *maat_inst = MaatCmdTest::_shared_maat_inst;
struct maat_state *state = maat_state_new(maat_inst, thread_id); struct maat_state *state = maat_state_new(maat_inst, thread_id);
maat_reload_log_level(maat_inst, LOG_LEVEL_INFO);
/* compile table add line */ /* compile table add line */
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1); long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);

View File

@@ -2267,34 +2267,6 @@
} }
] ]
}, },
{
"compile_id": 183,
"service": 1,
"action": 1,
"do_blacklist": 1,
"do_log": 1,
"user_region": "StringScan.RegexWithNotContains",
"is_valid": "yes",
"groups": [
{
"virtual_table": "CORNER_CASE_TABLE",
"group_name": "183_expr_group",
"group_id": 168,
"regions": [
{
"table_name": "CORNER_CASE_TABLE",
"table_type": "expr",
"table_content": {
"keywords": "^(?=.*/rain/a/TWF2021042600418000)(?!new.qq.com).*",
"expr_type": "regex",
"match_method": "sub",
"format": "uncase plain"
}
}
]
}
]
},
{ {
"compile_id": 184, "compile_id": 184,
"user_region": "APP_ID=6006740;Liumengyan-Bugreport-20210515", "user_region": "APP_ID=6006740;Liumengyan-Bugreport-20210515",