调整限制日志输出逻辑,通过调用函数位置做单独的限制输出速率,不再通过handle做整体限制

This commit is contained in:
wangmenglan
2022-01-21 10:23:51 +08:00
parent 4c9b58db56
commit 3ecbc885fb
3 changed files with 52 additions and 31 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -20,11 +20,7 @@ typedef struct log_handle_t
{
int interval;
int burst;
int printed;
int missed;
long begin;
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(&rs->mutex) != 0)return 0;
if (pthread_mutex_trylock(&p_handle->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,7 +257,6 @@ 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);
@@ -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);