sample/log_producer_sample.c (106 lines of code) (raw):
//
// Created by ZhangCheng on 26/12/2017.
//
#include "log_api.h"
#include "log_producer_config.h"
#include "log_producer_client.h"
void on_log_send_done(const char * config_name, log_producer_result result, size_t log_bytes, size_t compressed_bytes, const char * req_id, const char * message, const unsigned char * raw_buffer)
{
if (result == LOG_PRODUCER_OK)
{
// @note requst id may be NULL and printf %s NULL is undefined behavior
if (req_id == NULL)
{
req_id = "";
}
printf("send success, config : %s, result : %d, log bytes : %d, compressed bytes : %d, request id : %s \n",
config_name, (result),
(int)log_bytes, (int)compressed_bytes, req_id);
}
else
{
// @note message may be NULL and printf %s NULL is undefined behavior
if (message == NULL)
{
message = "";
}
// @note requst id may be NULL and printf %s NULL is undefined behavior
if (req_id == NULL)
{
req_id = "";
}
printf("send fail, config : %s, result : %d, log bytes : %d, compressed bytes : %d, request id : %s, error message : %s\n",
config_name, (result),
(int)log_bytes, (int)compressed_bytes, req_id, message);
}
}
log_producer * create_log_producer_wrapper(on_log_producer_send_done_function on_send_done)
{
log_producer_config * config = create_log_producer_config();
// endpoint list: https://help.aliyun.com/document_detail/29008.html
log_producer_config_set_endpoint(config, "${your_endpoint}");
log_producer_config_set_project(config, "${your_project}");
log_producer_config_set_logstore(config, "${your_logstore}");
log_producer_config_set_access_id(config, "${your_access_key_id}");
log_producer_config_set_access_key(config, "${your_access_key_secret}");
// @note, if using security token to send log, remember reset token before expired. this function is thread safe
//log_producer_config_reset_security_token(config, "${your_access_key_id}", "${your_access_key_secret}", "${your_access_security_token}");
// if you do not need topic or tag, comment it
log_producer_config_set_topic(config, "test_topic");
log_producer_config_add_tag(config, "tag_1", "val_1");
log_producer_config_add_tag(config, "tag_2", "val_2");
log_producer_config_add_tag(config, "tag_3", "val_3");
log_producer_config_add_tag(config, "tag_4", "val_4");
log_producer_config_add_tag(config, "tag_5", "val_5");
// set resource params
log_producer_config_set_packet_log_bytes(config, 4*1024*1024);
log_producer_config_set_packet_log_count(config, 4096);
log_producer_config_set_packet_timeout(config, 3000);
log_producer_config_set_max_buffer_limit(config, 64*1024*1024);
// set send thread count
log_producer_config_set_send_thread_count(config, 4);
// set compress type : lz4
log_producer_config_set_compress_type(config, 1);
// set timeout
log_producer_config_set_connect_timeout_sec(config, 10);
log_producer_config_set_send_timeout_sec(config, 15);
log_producer_config_set_destroy_flusher_wait_sec(config, 1);
log_producer_config_set_destroy_sender_wait_sec(config, 1);
// set interface
log_producer_config_set_net_interface(config, NULL);
return create_log_producer(config, on_send_done);
}
void log_producer_post_logs()
{
if (log_producer_env_init(LOG_GLOBAL_ALL) != LOG_PRODUCER_OK) {
exit(1);
}
log_producer * producer = create_log_producer_wrapper(on_log_send_done);
if (producer == NULL)
{
printf("create log producer by config fail \n");
exit(1);
}
log_producer_client * client = get_log_producer_client(producer, NULL);
if (client == NULL)
{
printf("create log producer client by config fail \n");
exit(1);
}
int i = 0;
for (; i < 10; ++i)
{
char indexStr[32];
sprintf(indexStr, "%d", i);
log_producer_client_add_log(client, 20, "content_key_1", "1abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_2", "2abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_3", "3abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_4", "4abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_5", "5abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_6", "6abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_7", "7abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_8", "8abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"content_key_9", "9abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+",
"index", indexStr);
log_producer_result rst = log_producer_client_add_log(client, 8, "LogHub", "Real-time log collection and consumption",
"Search/Analytics", "Query and real-time analysis",
"Visualized", "dashboard and report functions",
"Interconnection", "Grafana and JDBC/SQL92");
if (rst != LOG_PRODUCER_OK)
{
printf("add log error %d \n", rst);
}
}
destroy_log_producer(producer);
log_producer_env_destroy();
}
int main(int argc, char *argv[])
{
log_producer_post_logs();
return 0;
}