Skip to content

Commit

Permalink
[add] add post log option api
Browse files Browse the repository at this point in the history
[add] add bind interface to post log option
  • Loading branch information
shabicheng committed Jul 14, 2017
1 parent f270f08 commit bafac72
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 11 deletions.
123 changes: 121 additions & 2 deletions sample/log_post_logs_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@
#include "log_config.h"
#include <time.h>
#include <stdlib.h>

log_post_option g_log_option;


void post_logs_with_json_with_option()
{
aos_pool_t *p = NULL;
aos_status_t *s = NULL;
aos_pool_create(&p, NULL);

g_log_option.interface = "eth0";
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "__source__", "127.0.0.1");
cJSON_AddStringToObject(root, "__topic__", "topic");
cJSON *logs = cJSON_CreateArray();
cJSON_AddItemToObject(root, "__logs__", logs);
cJSON *log1 = cJSON_CreateObject();
cJSON_AddStringToObject(log1, "level", "error1");
cJSON_AddStringToObject(log1, "message", "c sdk test message1");
cJSON_AddNumberToObject(log1, "__time__", apr_time_now()/1000000);
cJSON *log2 = cJSON_CreateObject();
cJSON_AddStringToObject(log2, "level", "error2");
cJSON_AddStringToObject(log2, "message", "c sdk test message2");
cJSON_AddNumberToObject(log2, "__time__", apr_time_now()/1000000);
cJSON_AddItemToArray(logs, log1);
cJSON_AddItemToArray(logs, log2);
s = log_post_logs_with_option(p, LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET, PROJECT_NAME, LOGSTORE_NAME, root, &g_log_option);
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");
}
cJSON_Delete(root);
aos_pool_destroy(p);
}

void post_logs_with_json()
{
aos_pool_t *p = NULL;
Expand Down Expand Up @@ -109,7 +149,45 @@ double test_build_speed_second(const char* path){

}


void post_logs_with_http_cont_log_option(){

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"));

g_log_option.interface = "eth0";
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_with_option(cont, &g_log_option);
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_http_cont(){

if (aos_http_io_initialize("linux-x86_64", 0) != AOSE_OK) {
Expand Down Expand Up @@ -147,6 +225,46 @@ void post_logs_with_http_cont(){

aos_http_io_deinitialize();
}
void post_logs_with_proto_buffer_with_option(){

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));
}
}
g_log_option.interface = "eth0";
s = log_post_logs_from_proto_buf_with_option(LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET,NULL, PROJECT_NAME, LOGSTORE_NAME, bder, &g_log_option);

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_group_destroy(bder);

aos_http_io_deinitialize();
}


void post_logs_with_proto_buffer(){

if (aos_http_io_initialize("linux-x86_64", 0) != AOSE_OK) {
Expand Down Expand Up @@ -189,6 +307,7 @@ void post_logs_with_proto_buffer(){

int main(int argc, char *argv[])
{
post_logs_with_http_cont();
post_logs_with_proto_buffer_with_option();
post_logs_with_http_cont_log_option();
return 0;
}
1 change: 1 addition & 0 deletions src/aos_http_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ int aos_http_send_request(aos_http_controller_t *ctl, aos_http_request_t *req, a
t->req = req;
t->resp = resp;
t->controller = (aos_http_controller_ex_t *)ctl;
t->interface = ctl->interface;

return aos_http_transport_perform(t);
}
Expand Down
5 changes: 5 additions & 0 deletions src/aos_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ int aos_curl_transport_setup(aos_curl_http_transport_t *t)
curl_easy_setopt_safe(CURLOPT_TCP_NODELAY, 1);
curl_easy_setopt_safe(CURLOPT_NETRC, CURL_NETRC_IGNORED);

// interface
if (t->interface != NULL){
curl_easy_setopt_safe(CURLOPT_INTERFACE, t->interface);
}

// transport options
curl_easy_setopt_safe(CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt_safe(CURLOPT_USERAGENT, t->options->user_agent);
Expand Down
6 changes: 4 additions & 2 deletions src/aos_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ struct aos_http_transport_options_s {
int64_t first_byte_time; \
int64_t finish_time; \
uint32_t owner:1; \
void *user_data;
void *user_data; \
char * interface;

struct aos_http_controller_s {
AOS_HTTP_BASE_CONTROLLER_DEFINE
Expand Down Expand Up @@ -118,7 +119,8 @@ typedef enum {
aos_transport_state_e state; \
aos_array_header_t *cleanup; \
aos_http_transport_options_t *options; \
aos_http_controller_ex_t *controller;
aos_http_controller_ex_t *controller; \
char * interface;

struct aos_http_transport_s {
AOS_HTTP_BASE_TRANSPORT_DEFINE
Expand Down
45 changes: 38 additions & 7 deletions src/log_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "log_util.h"
#include "log_api.h"

aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont){
aos_status_t* log_post_logs_from_http_cont_with_option(log_http_cont* cont, log_post_option * logPostOption){

aos_http_request_t *req = (aos_http_request_t*)apr_palloc(cont->root, sizeof(aos_http_request_t));
req->pool = cont->root;
Expand All @@ -22,7 +22,9 @@ aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont){

options->ctl = aos_http_controller_create(cont->root, 0);
options->ctl->pool = cont->root;

if (logPostOption != NULL){
options->ctl->interface = (char *)logPostOption->interface;
}
aos_list_t buffer;
aos_list_init(&buffer);

Expand All @@ -38,8 +40,7 @@ aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont){
return s;
}


aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder)
aos_status_t *log_post_logs_from_proto_buf_with_option(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder, log_post_option * logPostOption)
{
aos_pool_t *p = bder->root;
aos_string_t project_name, logstore_name;
Expand All @@ -64,6 +65,9 @@ aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * ac
aos_str_set(&(options->config->sts_token), stsToken);
}
options->ctl = aos_http_controller_create(options->pool, 0);
if (logPostOption != NULL){
options->ctl->interface = (char *)logPostOption->interface;
}
headers = aos_table_make(p, 5);
apr_table_set(headers, LOG_API_VERSION, "0.6.0");
apr_table_set(headers, LOG_COMPRESS_TYPE, "lz4");
Expand Down Expand Up @@ -120,8 +124,7 @@ aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * ac

}


aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root)
aos_status_t *log_post_logs_with_sts_token_with_option(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root, log_post_option * logPostOption)
{
aos_string_t project_name, logstore_name;
aos_table_t *headers = NULL;
Expand All @@ -145,6 +148,9 @@ aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint,
aos_str_set(&(options->config->sts_token), stsToken);
}
options->ctl = aos_http_controller_create(options->pool, 0);
if (logPostOption != NULL){
options->ctl->interface = (char *)logPostOption->interface;
}
headers = aos_table_make(p, 5);
apr_table_set(headers, LOG_API_VERSION, "0.6.0");
apr_table_set(headers, LOG_COMPRESS_TYPE, "lz4");
Expand Down Expand Up @@ -196,7 +202,32 @@ aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint,
free(body);
return s;
}

aos_status_t *log_post_logs_with_option(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *project, const char *logstore, cJSON *root, log_post_option * logPostOption)
{
return log_post_logs_with_sts_token_with_option(p, endpoint, accesskeyId, accessKey, NULL, project, logstore, root, logPostOption);
}




aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root)
{
return log_post_logs_with_sts_token_with_option(p, endpoint, accesskeyId, accessKey, stsToken, project, logstore, root, NULL);

}

aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder)
{
return log_post_logs_from_proto_buf_with_option(endpoint, accesskeyId, accessKey, stsToken, project, logstore, bder, NULL);
}

aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont)
{
return log_post_logs_from_http_cont_with_option(cont, NULL);
}

aos_status_t *log_post_logs(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *project, const char *logstore, cJSON *root)
{
return log_post_logs_with_sts_token(p, endpoint, accesskeyId, accessKey, NULL, project, logstore, root);
return log_post_logs_with_option(p, endpoint, accesskeyId, accessKey, project, logstore, root, NULL);
}
11 changes: 11 additions & 0 deletions src/log_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@
#include "log_http_cont.h"
LOG_CPP_START

struct _log_post_option
{
char * interface;
};
typedef struct _log_post_option log_post_option;

aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root);
aos_status_t *log_post_logs(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *project, const char *logstore, cJSON *root);
aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder);
aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont);

aos_status_t *log_post_logs_with_sts_token_with_option(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root, log_post_option * logPostOption);
aos_status_t *log_post_logs_with_option(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *project, const char *logstore, cJSON *root, log_post_option * logPostOption);
aos_status_t *log_post_logs_from_proto_buf_with_option(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder, log_post_option * logPostOption);
aos_status_t* log_post_logs_from_http_cont_with_option(log_http_cont* cont, log_post_option * logPostOption);

LOG_CPP_END
#endif

0 comments on commit bafac72

Please sign in to comment.