feat(MESA_handle_runtime_log_level_enabled): log_handle_t改为前置声明,增加判断level是否启用的接口

This commit is contained in:
yangwei
2023-05-30 18:35:20 +08:00
parent c0620c8d25
commit 8680c2f4f5
3 changed files with 40 additions and 24 deletions

View File

@@ -16,6 +16,7 @@ void *test_handle = NULL;
int g_mode = 0; int g_mode = 0;
int g_log_num = 0; int g_log_num = 0;
int g_thread_num = 0; int g_thread_num = 0;
int g_sleep_interval = 0;
const char *g_zlog_conf = NULL; const char *g_zlog_conf = NULL;
volatile long g_start_time = 0; volatile long g_start_time = 0;
volatile long g_end_time = 0; volatile long g_end_time = 0;
@@ -44,7 +45,11 @@ void call_logger(int log_num, int thread_num)
//sleep(1); //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); //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); //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); clock_gettime(CLOCK_MONOTONIC, &end);
@@ -60,8 +65,9 @@ void call_logger(int log_num, int thread_num)
g_end_time = end_time; 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("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; return;
} }
@@ -101,15 +107,16 @@ int main(int argc, char ** args)
pthread_t t[MAX_THREAD_NUM]; pthread_t t[MAX_THREAD_NUM];
int i = 0; 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; return -1;
} }
g_mode = atoi(args[1]); g_mode = atoi(args[1]);
g_zlog_conf = args[2]; g_zlog_conf = args[2];
g_thread_num = atoi(args[3]); g_thread_num = atoi(args[3]);
g_log_num = atoi(args[4]); g_log_num = atoi(args[4]);
g_sleep_interval = atoi(args[5]);
if(g_thread_num <= 0 || g_log_num <= 0) if(g_thread_num <= 0 || g_log_num <= 0)
{ {

View File

@@ -19,6 +19,8 @@ extern "C"
#define RLOG_LV_FATAL 30 #define RLOG_LV_FATAL 30
struct log_handle_t;
int MESA_handle_runtime_log_creation(const char *conf_path); int MESA_handle_runtime_log_creation(const char *conf_path);
int MESA_handle_runtime_log_reconstruction(const char *conf_path); int MESA_handle_runtime_log_reconstruction(const char *conf_path);
void MESA_handle_runtime_log_destruction(); 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, \ MESA_handle_runtime_log((handle), (lv), (mod), "file %s, line %d, " fmt, \
__FILE__, __LINE__, ##args) __FILE__, __LINE__, ##args)
int MESA_handle_runtime_log_level_enabled(struct log_handle_t *handle, const int level);
/* /*
* name: MESA_create_runtime_log_handle * name: MESA_create_runtime_log_handle
* functionality: get runtime_log handle; * functionality: get runtime_log handle;
@@ -37,7 +41,7 @@ void MESA_handle_runtime_log_destruction();
* not NULL, if succeeded; * not NULL, if succeeded;
* NULL, if file is not absolute path, or failed to create log file; * 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 * name: MESA_handle_runtime_log
@@ -51,7 +55,7 @@ void MESA_handle_runtime_log_destruction();
* returns: * returns:
* none; * 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 * name: MESA_destroy_runtime_log_handle
@@ -61,7 +65,7 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
* returns: * returns:
* none; * none;
*/ */
void MESA_destroy_runtime_log_handle(void *handle); void MESA_destroy_runtime_log_handle(struct log_handle_t *handle);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -14,13 +14,13 @@ static int g_zlog_conf_fp = -1;
static char global_conf_filepath[MAX_HANDLE_LOG_PATH] = ""; static char global_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
static char tmp_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; int runtime_log_level;
zlog_category_t *zc; zlog_category_t *zc;
const char *global_conf_path; const char *global_conf_path;
char runtime_log_file[MAX_HANDLE_LOG_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; 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) if(file_path == NULL)
return NULL; return NULL;
@@ -171,11 +171,11 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
int rc = -1; int rc = -1;
zlog_category_t *zc = NULL; zlog_category_t *zc = NULL;
FILE *fp = NULL; FILE *fp = NULL;
log_handle_t *p_handle = NULL; struct log_handle_t *handle = NULL;
char handle_name[MAX_HANDLE_LOG_PATH]; char handle_name[MAX_HANDLE_LOG_PATH];
char *p_path_end = rindex(file_path, '/'); char *p_path_end = rindex(file_path, '/');
char *p_name = p_path_end+1; char *p_name;
strcpy(handle_name, file_path); strcpy(handle_name, file_path);
escape_for_zlog(handle_name, strlen(handle_name)); 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); 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); handle = (struct log_handle_t *)calloc(sizeof(struct log_handle_t), 1);
strncpy(p_handle->runtime_log_file, file_path, sizeof(p_handle->runtime_log_file) - 1); strncpy(handle->runtime_log_file, file_path, sizeof(handle->runtime_log_file) - 1);
p_handle->runtime_log_level = level; handle->runtime_log_level = level;
p_handle->zc = zc; handle->zc = zc;
return (void *)p_handle; 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) 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(handle->zc == NULL)return;
if(p_handle->zc == NULL)return;
va_list ap; va_list ap;
va_start(ap, fmt); 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); va_end(ap);
return ; return ;
} }