From 8680c2f4f5639dbdabfa383b1d0819aabd7cc652 Mon Sep 17 00:00:00 2001 From: yangwei Date: Tue, 30 May 2023 18:35:20 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(MESA=5Fhandle=5Fruntime=5Flog?= =?UTF-8?q?=5Flevel=5Fenabled):=20log=5Fhandle=5Ft=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=89=8D=E7=BD=AE=E5=A3=B0=E6=98=8E=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=88=A4=E6=96=ADlevel=E6=98=AF=E5=90=A6=E5=90=AF=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/test_handle_logger.c | 15 +++++++++++---- inc/MESA_handle_logger.h | 10 +++++++--- src/MESA_handle_logger.c | 39 ++++++++++++++++++++++----------------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/demo/test_handle_logger.c b/demo/test_handle_logger.c index b4f9e67..bd36de3 100644 --- a/demo/test_handle_logger.c +++ b/demo/test_handle_logger.c @@ -16,6 +16,7 @@ void *test_handle = NULL; int g_mode = 0; int g_log_num = 0; int g_thread_num = 0; +int g_sleep_interval = 0; const char *g_zlog_conf = NULL; volatile long g_start_time = 0; volatile long g_end_time = 0; @@ -44,7 +45,11 @@ void call_logger(int log_num, int thread_num) //sleep(1); MESA_handle_runtime_log(test_handle, RLOG_LV_INFO, "test", "test_handle MESA_handle_runtime_log, i = %d, thread_num = %d", i, thread_num); //MESA_HANDLE_RUNTIME_LOG(sample_handle, RLOG_LV_FATAL, "sample", "sample_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num); - //sleep(1); + if(g_sleep_interval > 0) + { + printf("MESA_handle_runtime_log_level_enabled(%d) return %d\n", RLOG_LV_DEBUG, MESA_handle_runtime_log_level_enabled(test_handle, RLOG_LV_DEBUG)); + sleep(g_sleep_interval); + } //MESA_HANDLE_RUNTIME_LOG(test_handle, RLOG_LV_FATAL, "test", "test_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num); } clock_gettime(CLOCK_MONOTONIC, &end); @@ -60,8 +65,9 @@ void call_logger(int log_num, int thread_num) g_end_time = end_time; } } - time_cost = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000 ; + time_cost = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000 - g_sleep_interval * 1000000 ; printf("THREAD %d write %d log using %ld us, avg speed %f /s, %ld -> %ld\n", thread_num, log_num, time_cost, ((float)log_num/(float)time_cost)*1000000, start_time, end_time); + printf("MESA_handle_runtime_log_level_enabled(%d) return %d\n", RLOG_LV_DEBUG, MESA_handle_runtime_log_level_enabled(test_handle, RLOG_LV_DEBUG)); return; } @@ -101,15 +107,16 @@ int main(int argc, char ** args) pthread_t t[MAX_THREAD_NUM]; int i = 0; - if (argc != 5) + if (argc != 6) { - printf("Usage: ./($app) $mode[1 or 2] $zlog_conf_path $thread_num $log_num \n"); + printf("Usage: ./($app) $version_mode[1 or 2] $zlog_conf_path $thread_num $log_num $sleep_interval\n"); return -1; } g_mode = atoi(args[1]); g_zlog_conf = args[2]; g_thread_num = atoi(args[3]); g_log_num = atoi(args[4]); + g_sleep_interval = atoi(args[5]); if(g_thread_num <= 0 || g_log_num <= 0) { diff --git a/inc/MESA_handle_logger.h b/inc/MESA_handle_logger.h index ed46578..970e50d 100644 --- a/inc/MESA_handle_logger.h +++ b/inc/MESA_handle_logger.h @@ -19,6 +19,8 @@ extern "C" #define RLOG_LV_FATAL 30 +struct log_handle_t; + int MESA_handle_runtime_log_creation(const char *conf_path); int MESA_handle_runtime_log_reconstruction(const char *conf_path); void MESA_handle_runtime_log_destruction(); @@ -27,6 +29,8 @@ void MESA_handle_runtime_log_destruction(); MESA_handle_runtime_log((handle), (lv), (mod), "file %s, line %d, " fmt, \ __FILE__, __LINE__, ##args) + +int MESA_handle_runtime_log_level_enabled(struct log_handle_t *handle, const int level); /* * name: MESA_create_runtime_log_handle * functionality: get runtime_log handle; @@ -37,7 +41,7 @@ void MESA_handle_runtime_log_destruction(); * not NULL, if succeeded; * NULL, if file is not absolute path, or failed to create log file; */ - void *MESA_create_runtime_log_handle(const char *file_path, int level); +struct log_handle_t *MESA_create_runtime_log_handle(const char *file_path, int level); /* * name: MESA_handle_runtime_log @@ -51,7 +55,7 @@ void MESA_handle_runtime_log_destruction(); * returns: * none; */ -void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...); +void MESA_handle_runtime_log(struct log_handle_t *handle, int level, const char *module, const char *fmt, ...); /* * name: MESA_destroy_runtime_log_handle @@ -61,7 +65,7 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const * returns: * none; */ -void MESA_destroy_runtime_log_handle(void *handle); +void MESA_destroy_runtime_log_handle(struct log_handle_t *handle); #ifdef __cplusplus } diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c index 473361c..93da94c 100644 --- a/src/MESA_handle_logger.c +++ b/src/MESA_handle_logger.c @@ -14,13 +14,13 @@ static int g_zlog_conf_fp = -1; static char global_conf_filepath[MAX_HANDLE_LOG_PATH] = ""; static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = ""; -typedef struct log_handle_t +struct log_handle_t { int runtime_log_level; zlog_category_t *zc; const char *global_conf_path; char runtime_log_file[MAX_HANDLE_LOG_PATH]; -} log_handle_t; +}; @@ -163,7 +163,7 @@ static void snapshot_handle_info(const char *handle_name, const char *log_path, return; } -void *MESA_create_runtime_log_handle(const char *file_path, int level) +struct log_handle_t *MESA_create_runtime_log_handle(const char *file_path, int level) { if(file_path == NULL) return NULL; @@ -171,11 +171,11 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level) int rc = -1; zlog_category_t *zc = NULL; FILE *fp = NULL; - log_handle_t *p_handle = NULL; + struct log_handle_t *handle = NULL; char handle_name[MAX_HANDLE_LOG_PATH]; char *p_path_end = rindex(file_path, '/'); - char *p_name = p_path_end+1; + char *p_name; strcpy(handle_name, file_path); escape_for_zlog(handle_name, strlen(handle_name)); @@ -195,14 +195,14 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level) { fprintf(stderr,"[MESA_create_runtime_log_handle], get zlog category (%s) in global_conf_filepath(%s) fail\n", p_name, global_conf_filepath); } - p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1); - strncpy(p_handle->runtime_log_file, file_path, sizeof(p_handle->runtime_log_file) - 1); - p_handle->runtime_log_level = level; - p_handle->zc = zc; - return (void *)p_handle; + handle = (struct log_handle_t *)calloc(sizeof(struct log_handle_t), 1); + strncpy(handle->runtime_log_file, file_path, sizeof(handle->runtime_log_file) - 1); + handle->runtime_log_level = level; + handle->zc = zc; + return (void *)handle; } -void MESA_destroy_runtime_log_handle(void *handle) +void MESA_destroy_runtime_log_handle(struct log_handle_t *handle) { if(handle != NULL) { @@ -214,18 +214,23 @@ void MESA_destroy_runtime_log_handle(void *handle) } -void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...) +int MESA_handle_runtime_log_level_enabled(struct log_handle_t *handle, const int level) +{ + if(handle == NULL || handle->runtime_log_file == NULL)return; + if(handle->zc == NULL) return; + return zlog_level_enabled(handle->zc, level); +} + +void MESA_handle_runtime_log(struct log_handle_t *handle, int level, const char *module, const char *fmt, ...) { - log_handle_t *p_handle = (log_handle_t *)handle; + if(handle == NULL || handle->runtime_log_file == NULL)return; - if(p_handle == NULL || p_handle->runtime_log_file == NULL)return; - - if(p_handle->zc == NULL)return; + if(handle->zc == NULL)return; va_list ap; va_start(ap, fmt); - vzlog(p_handle->zc, p_handle->runtime_log_file, strlen(p_handle->runtime_log_file), module, strlen(module), __LINE__, level, fmt, ap); + vzlog(handle->zc, handle->runtime_log_file, strlen(handle->runtime_log_file), module, strlen(module), __LINE__, level, fmt, ap); va_end(ap); return ; }