snprintf支持格式化结构体
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
void *sample_handle = NULL;
|
||||
void *test_handle = NULL;
|
||||
@@ -19,50 +20,67 @@ int g_thread_num = 0;
|
||||
const char *g_zlog_conf = NULL;
|
||||
volatile long g_start_time = 0;
|
||||
volatile long g_end_time = 0;
|
||||
struct test_A{
|
||||
int a;
|
||||
char *str;
|
||||
long long b;
|
||||
};
|
||||
int MESA_fmt_handler_A(void *info, char *buf, int maxlen)
|
||||
{
|
||||
struct test_A *obj = (struct test_A *)info;
|
||||
int n = snprintf(buf, maxlen, "a=%d,s=%s,b=%lld", obj->a, obj->str, obj->b);
|
||||
return n < maxlen?n:maxlen - 1;
|
||||
}
|
||||
|
||||
void call_logger(int log_num, int thread_num)
|
||||
{
|
||||
int i = 0;
|
||||
struct timespec start, end;
|
||||
long start_time, end_time, time_cost;
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
start_time = start.tv_sec*1000000 + start.tv_nsec/1000;
|
||||
if(g_start_time == 0)
|
||||
|
||||
struct test_A a;
|
||||
a.a = 10;
|
||||
a.str = "hello";
|
||||
a.b = 123456;
|
||||
int i = 0;
|
||||
struct timespec start, end;
|
||||
long start_time, end_time, time_cost;
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
start_time = start.tv_sec*1000000 + start.tv_nsec/1000;
|
||||
if(g_start_time == 0)
|
||||
{
|
||||
g_start_time = start_time;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(g_start_time > start_time)
|
||||
{
|
||||
g_start_time = start_time;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(g_start_time > start_time)
|
||||
{
|
||||
g_start_time = start_time;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
end_time = end.tv_sec*1000000 + end.tv_nsec/1000;
|
||||
if(g_end_time == 0)
|
||||
}
|
||||
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);
|
||||
MESA_HANDLE_RUNTIME_LOG(sample_handle, RLOG_LV_FATAL, "sample", "sample_handle RUNTIEM_LOG test, i = %d, thread_num = %d,struct a =%A", i, thread_num, &a);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
end_time = end.tv_sec*1000000 + end.tv_nsec/1000;
|
||||
if(g_end_time == 0)
|
||||
{
|
||||
g_end_time = end_time;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(g_end_time < end_time)
|
||||
{
|
||||
g_end_time = end_time;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(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 ;
|
||||
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);
|
||||
return;
|
||||
}
|
||||
}
|
||||
time_cost = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000 ;
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -129,26 +147,30 @@ int main(int argc, char ** args)
|
||||
{
|
||||
printf("get log sample_handle error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
test_handle = MESA_create_runtime_log_handle("./log/test_log", RLOG_LV_DEBUG);
|
||||
if(test_handle == NULL)
|
||||
{
|
||||
printf("get log test_handle error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(i = 0; i < g_thread_num; i++)
|
||||
{
|
||||
pthread_create(&t[i], NULL, thread_logger, (void *)(unsigned long)(i));
|
||||
}
|
||||
signal(SIGINT, sig_int_handler);
|
||||
signal(SIGHUP, sig_hup_handler);
|
||||
while(1)
|
||||
;
|
||||
//MESA_destroy_runtime_log_handle(sample_handle);
|
||||
//MESA_destroy_runtime_log_handle(test_handle);
|
||||
//sample_handle = NULL;
|
||||
//test_handle = NULL;
|
||||
}
|
||||
if(MESA_handle_fmt_rule_register(sample_handle, 'A', MESA_fmt_handler_A, 512) < 0){
|
||||
printf("struct a register error\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
test_handle = MESA_create_runtime_log_handle("./log/test_log", RLOG_LV_DEBUG);
|
||||
if(test_handle == NULL)
|
||||
{
|
||||
printf("get log test_handle error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(i = 0; i < g_thread_num; i++)
|
||||
{
|
||||
pthread_create(&t[i], NULL, thread_logger, (void *)(unsigned long)(i));
|
||||
}
|
||||
signal(SIGINT, sig_int_handler);
|
||||
signal(SIGHUP, sig_hup_handler);
|
||||
while(1)
|
||||
;
|
||||
//MESA_destroy_runtime_log_handle(sample_handle);
|
||||
//MESA_destroy_runtime_log_handle(test_handle);
|
||||
//sample_handle = NULL;
|
||||
//test_handle = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user