Skip to content

Commit

Permalink
Modify sample
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjwchn committed Sep 7, 2016
1 parent 8eab23a commit 9062101
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
63 changes: 47 additions & 16 deletions sample/log_post_logs_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "log_config.h"
#include <time.h>
#include <stdlib.h>
void log_post_logs_sample()
void post_logs_with_json()
{
aos_pool_t *p = NULL;
aos_status_t *s = NULL;
Expand All @@ -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();
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 {
Expand All @@ -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;
}
22 changes: 2 additions & 20 deletions src/log_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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));
Expand All @@ -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

Expand Down

0 comments on commit 9062101

Please sign in to comment.