diff --git a/sample/log_post_logs_sample.c b/sample/log_post_logs_sample.c index 4194158..f4cc6c3 100644 --- a/sample/log_post_logs_sample.c +++ b/sample/log_post_logs_sample.c @@ -8,7 +8,7 @@ #include "log_config.h" #include #include -void log_post_logs_sample() +void post_logs_with_json() { aos_pool_t *p = NULL; aos_status_t *s = NULL; @@ -17,10 +17,6 @@ void log_post_logs_sample() cJSON *root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "__source__", "127.0.0.1"); cJSON_AddStringToObject(root, "__topic__", "topic"); - //cJSON *tags = cJSON_CreateObject(); - //cJSON_AddItemToObject(root, "__tags__", tags); - //cJSON_AddStringToObject(tags, "tag1", "tag1"); - //cJSON_AddStringToObject(tags, "tag2", "tag2"); cJSON *logs = cJSON_CreateArray(); cJSON_AddItemToObject(root, "__logs__", logs); cJSON *log1 = cJSON_CreateObject(); @@ -46,9 +42,7 @@ void log_post_logs_sample() cJSON_Delete(root); aos_pool_destroy(p); } - -double log_build_http_cont_test(const char* path){ - +double test_build_speed_second(const char* path){ apr_initialize(); FILE* f = fopen(path, "r"); @@ -115,8 +109,46 @@ double log_build_http_cont_test(const char* path){ } -void log_post_logs_sample2(){ +void post_logs_with_http_cont(){ + + if (aos_http_io_initialize("linux-x86_64", 0) != AOSE_OK) { + exit(1); + } + + aos_status_t *s = NULL; + log_group_builder* bder = log_group_create(); + add_source(bder,"mSource",sizeof("mSource")); + add_topic(bder,"mTopic", sizeof("mTopic")); + + int i; + for(i=0;i<3;i++){ + add_log(bder); + int j; + for(j=0;j<5;j++){ + char k[] = {i*j + 'a'}; + char v[] = {2*j + 'a'} ; + add_log_key_value(bder, k, strlen(k), v, strlen(v)); + } + } + log_http_cont* cont = log_create_http_cont(LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET,NULL, PROJECT_NAME, LOGSTORE_NAME, bder); + s = log_post_logs_from_http_cont(cont); + if (aos_status_is_ok(s)) { + printf("post logs succeeded\n"); + } else { + printf("%d\n",s->code); + printf("%s\n",s->error_code); + printf("%s\n",s->error_msg); + printf("%s\n",s->req_id); + printf("put logs failed\n"); + } + + log_clean_http_cont(cont); + + aos_http_io_deinitialize(); +} +void post_logs_with_proto_buffer(){ + if (aos_http_io_initialize("linux-x86_64", 0) != AOSE_OK) { exit(1); } @@ -131,16 +163,14 @@ void log_post_logs_sample2(){ add_log(bder); int j; for(j=0;j<5;j++){ - char k[] = "_k_"; - char v[] = "_v_"; + char k[] = {i*j + 'a'}; + char v[] = {2*j + 'a'} ; add_log_key_value(bder, k, strlen(k), v, strlen(v)); } } - //s = log_post_logs_from_proto_buf(LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET,NULL, PROJECT_NAME, LOGSTORE_NAME, bder); + s = log_post_logs_from_proto_buf(LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET,NULL, PROJECT_NAME, LOGSTORE_NAME, bder); - log_http_cont* cont = log_create_http_cont(LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET,NULL, PROJECT_NAME, LOGSTORE_NAME, bder); - s = log_post_logs_from_http_cont(cont); if (aos_status_is_ok(s)) { printf("post logs succeeded\n"); } else { @@ -151,13 +181,14 @@ void log_post_logs_sample2(){ printf("put logs failed\n"); } - log_clean_http_cont(cont); + log_group_destroy(bder); aos_http_io_deinitialize(); } + int main(int argc, char *argv[]) { - log_post_logs_sample2(); + post_logs_with_http_cont(); return 0; } diff --git a/src/log_builder.c b/src/log_builder.c index 32fd40e..64ea638 100644 --- a/src/log_builder.c +++ b/src/log_builder.c @@ -36,7 +36,7 @@ void add_log(log_group_builder* bder) bder->grp->n_logs++; if((bder->grp->n_logs & (bder->grp->n_logs - 1)) == 0 &&bder->grp->n_logs>=INIT_LOG_NUMBER_IN_LOGGROUP){ - log_lg** tmp = apr_palloc(bder->root,sizeof(log_lg*)*bder->grp->n_logs<<1); + log_lg** tmp = apr_palloc(bder->root,sizeof(log_lg*)*(bder->grp->n_logs<<1)); memcpy(tmp, bder->grp->logs,sizeof(log_lg*)*bder->grp->n_logs); bder->grp->logs = tmp; //memory usage issue } @@ -63,24 +63,6 @@ void add_topic(log_group_builder* bder,char* tpc,size_t len) bder->grp->topic = topic; } -void lg_end(log_group_builder* bder) -{ - if(!bder->grp->logs){ - bder->grp->logs = (log_lg**)apr_palloc(bder->root,sizeof(log_lg*)*INIT_LOG_NUMBER_IN_LOGGROUP); - bder->grp->n_logs = 0; - } - bder->grp->logs[bder->grp->n_logs] = bder->lg; - bder->grp->n_logs++; - if((bder->grp->n_logs & (bder->grp->n_logs - 1)) == 0 - &&bder->grp->n_logs>=INIT_LOG_NUMBER_IN_LOGGROUP){ - log_lg** tmp = apr_palloc(bder->root,sizeof(log_lg*)*bder->grp->n_logs<<1); - memcpy(tmp, bder->grp->logs,sizeof(log_lg*)*bder->grp->n_logs); - bder->grp->logs = tmp; //memory usage issue - } - bder->lg = (log_lg*)apr_palloc(bder->root,sizeof(log_lg)); - *bder->lg = (log_lg)log_lg_init; -} - void add_log_key_value(log_group_builder* bder,char* k,size_t k_len,char* v,size_t v_len) { char* key = (char*)apr_palloc(bder->root, sizeof(char)*(k_len+1)); @@ -106,7 +88,7 @@ void add_log_key_value(log_group_builder* bder,char* k,size_t k_len,char* v,size bder->lg->n_contents++; if((bder->lg->n_contents & (bder->lg->n_contents - 1)) == 0 &&bder->lg->n_contents>=INIT_KVPAIR_NUMBER_IN_LOG){ - log_content** tmp = apr_palloc(bder->root,sizeof(log_content*)*bder->lg->n_contents<<1); + log_content** tmp = apr_palloc(bder->root,sizeof(log_content*)*(bder->lg->n_contents<<1)); memcpy(tmp, bder->lg->contents,sizeof(log_content*)*bder->lg->n_contents); bder->lg->contents = tmp; //memory usage issue