summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwangmenglan <[email protected]>2022-01-24 17:10:49 +0800
committerwangmenglan <[email protected]>2022-01-24 17:10:49 +0800
commitc2c9a8a704d70d653724d847edd90a61a64e5b12 (patch)
treefb599fc04e9d1752f55c6184e71e8b88d2abc2f2
parent3ecbc885fb71a38821c1564d7446e32a6cda6303 (diff)
1. 将trylock替换为lock 2. 修改测试程序log_ratelimit
-rw-r--r--demo/test_handle_logger.c34
-rw-r--r--src/MESA_handle_logger.c3
2 files changed, 25 insertions, 12 deletions
diff --git a/demo/test_handle_logger.c b/demo/test_handle_logger.c
index 8de49d2..f8bc57c 100644
--- a/demo/test_handle_logger.c
+++ b/demo/test_handle_logger.c
@@ -16,11 +16,12 @@ void *test_handle = NULL;
int g_mode = 0;
int g_log_num = 0;
int g_thread_num = 0;
+int g_log_print_way = 0;
const char *g_zlog_conf = NULL;
volatile long g_start_time = 0;
volatile long g_end_time = 0;
-void call_logger(int log_num, int thread_num)
+void call_logger(int log_num, int thread_num, int way)
{
int i = 0;
struct timespec start, end;
@@ -40,15 +41,25 @@ 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);
- //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(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);
+ switch (way)
+ {
+ case 1:
+ MESA_handle_runtime_log(sample_handle, RLOG_LV_DEBUG, "sample", "sample_handle RUNTIEM_LOG test, 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);
+ break;
+ case 2:
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);
+ break;
+ case 3:
+ MESA_HANDLE_RUNTIME_LOG(sample_handle, RLOG_LV_DEBUG, "sample", "sample_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
+ MESA_HANDLE_RUNTIME_LOG(test_handle, RLOG_LV_INFO, "test", "test_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
+ break;
+ default:
+ break;
+ }
+ // if (i % 10000 == 0)
+ // sleep(1);
}
clock_gettime(CLOCK_MONOTONIC, &end);
end_time = end.tv_sec*1000000 + end.tv_nsec/1000;
@@ -73,7 +84,7 @@ void *thread_logger(void *arg)
{
int thread_num = (int)(unsigned long long)arg;
printf("thread %d created! \n", thread_num);
- call_logger(g_log_num, thread_num);
+ call_logger(g_log_num, thread_num, g_log_print_way);
printf("thread %d finished! \n", thread_num);
return NULL;
}
@@ -122,15 +133,16 @@ int main(int argc, char ** args)
pthread_t t[MAX_THREAD_NUM];
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) $mode[1 or 2] $zlog_conf_path $thread_num $log_num $log_print_way[1 2 3]\n");
return -1;
}
g_mode = atoi(args[1]);
g_zlog_conf = args[2];
g_thread_num = atoi(args[3]);
g_log_num = atoi(args[4]);
+ g_log_print_way = atoi(args[5]);
if(g_thread_num <= 0 || g_log_num <= 0)
{
diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c
index efe69a4..d1939cf 100644
--- a/src/MESA_handle_logger.c
+++ b/src/MESA_handle_logger.c
@@ -11,6 +11,7 @@
#define MAX_HANDLE_LOG_PATH 4096
#define DEFAULT_BURST 10
#define DEFAULT_INTERVAL 5
+
static int g_zlog_inited = 0;
static int g_zlog_conf_fp = -1;
static char global_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
@@ -190,7 +191,7 @@ int MESA_runtime_log_ratelimit(void *handle, log_ratelimit_t *rs, int level, con
struct timespec timer = {0, 0};
log_handle_t *p_handle = (log_handle_t *)handle;
- if (pthread_mutex_trylock(&rs->mutex) != 0)return 0;
+ if (pthread_mutex_lock(&rs->mutex) != 0)return 0;
if (!rs->begin)
{