summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwangmenglan <[email protected]>2022-01-21 10:23:51 +0800
committerwangmenglan <[email protected]>2022-01-21 10:23:51 +0800
commit3ecbc885fb71a38821c1564d7446e32a6cda6303 (patch)
treebbbe665e35f459e8e4eaa02242adb3f62511c04d
parent4c9b58db5634f5255320c64150b513a8481e6e32 (diff)
调整限制日志输出逻辑,通过调用函数位置做单独的限制输出速率,不再通过handle做整体限制
-rw-r--r--demo/test_handle_logger.c7
-rw-r--r--inc/MESA_handle_logger.h22
-rw-r--r--src/MESA_handle_logger.c54
3 files changed, 52 insertions, 31 deletions
diff --git a/demo/test_handle_logger.c b/demo/test_handle_logger.c
index 401b49b..8de49d2 100644
--- a/demo/test_handle_logger.c
+++ b/demo/test_handle_logger.c
@@ -40,12 +40,15 @@ void call_logger(int log_num, int thread_num)
}
for(i = 0; i < log_num; i++)
{
- MESA_handle_runtime_log(sample_handle, RLOG_LV_DEBUG, "sample", "sample_handle MESA_handle_runtime_log, i = %d, thread_num = %d", i, thread_num);
+ // MESA_handle_runtime_log(sample_handle, RLOG_LV_DEBUG, "sample", "sample_handle MESA_handle_runtime_log, i = %d, thread_num = %d", i, 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(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);
//MESA_HANDLE_RUNTIME_LOG(test_handle, RLOG_LV_FATAL, "test", "test_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
+ MESA_HANDLE_RUNTIME_LOG_RATELIMIT(sample_handle, RLOG_LV_DEBUG, "sample", "sample_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
+ MESA_HANDLE_RUNTIME_LOG_RATELIMIT(test_handle, RLOG_LV_INFO, "test", "test_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
+ sleep(1);
}
clock_gettime(CLOCK_MONOTONIC, &end);
end_time = end.tv_sec*1000000 + end.tv_nsec/1000;
diff --git a/inc/MESA_handle_logger.h b/inc/MESA_handle_logger.h
index 60ba348..8589189 100644
--- a/inc/MESA_handle_logger.h
+++ b/inc/MESA_handle_logger.h
@@ -14,10 +14,21 @@ extern "C"
{
#endif
+#include <pthread.h>
+
#define RLOG_LV_DEBUG 10
#define RLOG_LV_INFO 20
#define RLOG_LV_FATAL 30
+typedef struct log_ratelimit_s
+{
+ int interval;
+ int burst;
+ int printed;
+ int missed;
+ long begin;
+ pthread_mutex_t mutex;
+}log_ratelimit_t;
int MESA_handle_runtime_log_creation(const char *conf_path);
int MESA_handle_runtime_log_reconstruction(const char *conf_path);
@@ -27,6 +38,15 @@ void MESA_handle_runtime_log_destruction();
MESA_handle_runtime_log((handle), (lv), (mod), "file %s, line %d, " fmt, \
__FILE__, __LINE__, ##args)
+#define MESA_HANDLE_RUNTIME_LOG_RATELIMIT(handle, lv, mod, fmt, args...) \
+({ \
+ static log_ratelimit_t _rs = { \
+ .mutex = PTHREAD_MUTEX_INITIALIZER, \
+ }; \
+ if (MESA_runtime_log_ratelimit((handle), (&_rs), (lv), (mod), (__func__))) \
+ MESA_handle_runtime_log((handle), (lv), (mod), fmt, ##args); \
+})
+
/*
* name: MESA_create_runtime_log_handle
* functionality: get runtime_log handle;
@@ -64,7 +84,7 @@ 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
+ * name: MESA_set_runtime_log_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;
diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c
index 877291d..efe69a4 100644
--- a/src/MESA_handle_logger.c
+++ b/src/MESA_handle_logger.c
@@ -18,13 +18,9 @@ static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
typedef struct log_handle_t
{
- int interval;
- int burst;
- int printed;
- int missed;
- long begin;
+ int interval;
+ int burst;
pthread_mutex_t mutex;
-
int runtime_log_level;
zlog_category_t *zc;
const char *global_conf_path;
@@ -171,52 +167,57 @@ 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)
+static void log_update_time(log_handle_t *p_handle, log_ratelimit_t *rs, int level, const char *module, const char *func)
{
struct timespec current = {0, 0};
clock_gettime(CLOCK_MONOTONIC, &current);
- if (((long)(p_handle->begin + p_handle->interval - current.tv_sec)) < 0)
+ if (((long)(rs->begin + rs->interval - current.tv_sec)) < 0)
{
- if (p_handle->missed)
+ if (rs->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);
+ 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", func, rs->missed);
+ rs->missed = 0;
}
- p_handle->begin = current.tv_sec;
- p_handle->printed = 0;
- p_handle->missed = 0;
+ rs->begin = current.tv_sec;
+ rs->printed = 0;
}
return ;
}
-static int log_ratelimit(void *handle, int level, const char *module)
+int MESA_runtime_log_ratelimit(void *handle, log_ratelimit_t *rs, int level, const char *module, const char *func)
{
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 (pthread_mutex_trylock(&rs->mutex) != 0)return 0;
- if (!p_handle->begin)
+ if (!rs->begin)
{
clock_gettime(CLOCK_MONOTONIC, &timer);
- p_handle->begin = timer.tv_sec;
+ rs->begin = timer.tv_sec;
+ }
+ rs->burst = p_handle->burst;
+ rs->interval = p_handle->interval;
+ if (!rs->interval)
+ {
+ pthread_mutex_unlock(&rs->mutex);
+ return 1;
}
- log_update_time(p_handle, level, module);
+ log_update_time(p_handle, rs, level, module, func);
- if (p_handle->burst && p_handle->burst > p_handle->printed)
+ if (rs->burst && rs->burst > rs->printed)
{
- p_handle->printed++;
+ rs->printed++;
ret = 1;
}
else
{
- p_handle->missed++;
+ rs->missed++;
ret = 0;
}
- pthread_mutex_unlock(&p_handle->mutex);
+ pthread_mutex_unlock(&rs->mutex);
return ret;
}
@@ -256,10 +257,9 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
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;
-
p_handle->burst = DEFAULT_BURST;
p_handle->interval = DEFAULT_INTERVAL;
- pthread_mutex_init(&p_handle->mutex,NULL);
+ pthread_mutex_init(&p_handle->mutex, NULL);
return (void *)p_handle;
}
@@ -286,8 +286,6 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
if(p_handle->zc == NULL)return;
- if (!log_ratelimit(handle, level, module))return;
-
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);