修改日志逻辑,增加时间周期和周期内打印条数设置
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user