From d5de7fa5454779851cfa66a5eba88e3c0231fbf5 Mon Sep 17 00:00:00 2001 From: wangmenglan Date: Thu, 20 Jan 2022 11:06:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=97=B6=E9=97=B4=E5=91=A8?= =?UTF-8?q?=E6=9C=9F=E5=92=8C=E5=91=A8=E6=9C=9F=E5=86=85=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=9D=A1=E6=95=B0=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/test_handle_logger.c | 21 +++++++ inc/MESA_handle_logger.h | 12 ++++ src/MESA_handle_logger.c | 119 +++++++++++++++++++++++--------------- 3 files changed, 106 insertions(+), 46 deletions(-) diff --git a/demo/test_handle_logger.c b/demo/test_handle_logger.c index b4f9e67..401b49b 100644 --- a/demo/test_handle_logger.c +++ b/demo/test_handle_logger.c @@ -96,6 +96,24 @@ void sig_hup_handler(int sig) MESA_handle_runtime_log_reconstruction(g_zlog_conf); } +void sig_user1_handler(int sig) +{ + printf("SIGUSR1 recviced! set interval:3 burst:5\n"); + MESA_set_runtime_log_handle_ratelimit(sample_handle, 3, 5); +} + +void sig_user2_handler(int sig) +{ + printf("SIGUSR2 recviced! set interval:5 burst:3\n"); + MESA_set_runtime_log_handle_ratelimit(sample_handle, 5, 3); +} + +void sig_min_handler(int sig) +{ + printf("SIGRTMIN recviced! unlimit\n"); + MESA_set_runtime_log_handle_ratelimit(sample_handle, 0, 3); +} + int main(int argc, char ** args) { pthread_t t[MAX_THREAD_NUM]; @@ -144,6 +162,9 @@ int main(int argc, char ** args) } signal(SIGINT, sig_int_handler); signal(SIGHUP, sig_hup_handler); + signal(SIGUSR1, sig_user1_handler); + signal(SIGUSR2, sig_user2_handler); + signal(SIGRTMIN, sig_min_handler); while(1) ; //MESA_destroy_runtime_log_handle(sample_handle); diff --git a/inc/MESA_handle_logger.h b/inc/MESA_handle_logger.h index ed46578..60ba348 100644 --- a/inc/MESA_handle_logger.h +++ b/inc/MESA_handle_logger.h @@ -63,6 +63,18 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const */ void MESA_destroy_runtime_log_handle(void *handle); +/* + * name: MESA_set_handle_ratelimit + * functionality: set not more than @burst in every @interval when appends log message + * params: + * handle: runtime log handle which is going to be released; + * interval: interval time(second). if set '0', it means unlimit. + * burst: in interval time max count + * returns: + * none; + */ +void MESA_set_runtime_log_handle_ratelimit(void *handle, int interval, int burst); + #ifdef __cplusplus } #endif diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c index a80c3a7..3e61a70 100644 --- a/src/MESA_handle_logger.c +++ b/src/MESA_handle_logger.c @@ -50,11 +50,13 @@ static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL; #undef GIT_VERSION_CATTER #undef GIT_VERSION_EXPEND +#define DEFAULT_BURST 10 +#define DEFAULT_INTERVAL 5 + #ifdef __cplusplus } #endif - static int create_dir(const char *dir_path, int path_len) { if(dir_path == NULL) @@ -170,6 +172,55 @@ static void snapshot_handle_info(const char *handle_name, const char *log_path, return; } +static void log_update_time(log_handle_t *p_handle, int level, const char *module) +{ + struct timespec current = {0, 0}; + clock_gettime(CLOCK_MONOTONIC, ¤t); + if (((long)(p_handle->begin + p_handle->interval - current.tv_sec)) < 0) + { + if (p_handle->missed) + { + zlog(p_handle->zc, p_handle->runtime_log_file, strlen(p_handle->runtime_log_file), module, strlen(module), __LINE__, level, "%s: %d callbacks suppressed\n", module, p_handle->missed); + } + p_handle->begin = current.tv_sec; + p_handle->printed = 0; + p_handle->missed = 0; + } + return ; +} + +static int log_ratelimit(void *handle, int level, const char *module) +{ + int ret = 0; + struct timespec timer = {0, 0}; + log_handle_t *p_handle = (log_handle_t *)handle; + + if (!p_handle->interval)return 1; + + if (pthread_mutex_trylock(&p_handle->mutex) != 0)return 0; + + if (!p_handle->begin) + { + clock_gettime(CLOCK_MONOTONIC, &timer); + p_handle->begin = timer.tv_sec; + } + + log_update_time(p_handle, level, module); + + if (p_handle->burst && p_handle->burst > p_handle->printed) + { + p_handle->printed++; + ret = 1; + } + else + { + p_handle->missed++; + ret = 0; + } + pthread_mutex_unlock(&p_handle->mutex); + return ret; +} + void *MESA_create_runtime_log_handle(const char *file_path, int level) { if(file_path == NULL) @@ -207,8 +258,8 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level) p_handle->runtime_log_level = level; p_handle->zc = zc; - p_handle->burst = 10; - p_handle->interval = 5; + p_handle->burst = DEFAULT_BURST; + p_handle->interval = DEFAULT_INTERVAL; pthread_mutex_init(&p_handle->mutex,NULL); return (void *)p_handle; @@ -216,8 +267,10 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level) void MESA_destroy_runtime_log_handle(void *handle) { + log_handle_t *p_handle = (log_handle_t *)handle; if(handle != NULL) { + pthread_mutex_destroy(&p_handle->mutex); free(handle); handle = NULL; } @@ -225,62 +278,36 @@ void MESA_destroy_runtime_log_handle(void *handle) return; } -void time_update(long time) -{ - long ret = 0; - struct timespec current = {0, 0}; - clock_gettime(CLOCK_MONOTONIC, ¤t); - if (!(ret = (time-current.tv_sec))) - { - p_handle->begin = timer.tv_sec; - p_handle->printed = 0; - p_handle->missed = 0; - } - return ; -} - void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...) { - int ret = 0; - struct timespec timer = {0, 0}; + va_list ap; log_handle_t *p_handle = (log_handle_t *)handle; if(p_handle == NULL || p_handle->runtime_log_file == NULL)return; if(p_handle->zc == NULL)return; - if (pthread_mutex_trylock(&p_handle->mutex) != 0)return; + if (!log_ratelimit(handle, level, module))return; - if (!p_handle->begin) - { - clock_gettime(CLOCK_MONOTONIC, &timer); - p_handle->begin = timer.tv_sec; - } - - time_update(p_handle->begin + p_handle->interval); - - if (p_handle->burst && p_handle->burst > p_handle->printed) - { - p_handle->printed++; - ret = 1; - } - else - { - p_handle->missed++; - } - pthread_mutex_unlock(&p_handle->mutex); - - if (ret) - { - 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); - va_end(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); + va_end(ap); return ; } +void MESA_set_runtime_log_handle_ratelimit(void *handle, int interval, int burst) +{ + log_handle_t *p_handle = (log_handle_t *)handle; + + if ((p_handle == NULL) || (interval < 0) || (burst < 0))return; + + pthread_mutex_lock(&p_handle->mutex); + p_handle->interval = interval; + p_handle->burst = burst; + pthread_mutex_unlock(&p_handle->mutex); + return; +} int MESA_handle_runtime_log_creation(const char *conf_path) {