-
Notifications
You must be signed in to change notification settings - Fork 74
/
log_post_logs_sample.c
132 lines (116 loc) · 4.33 KB
/
log_post_logs_sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "log_util.h"
#include "log_api.h"
#include "log_config.h"
#include <string.h>
#include <stdio.h>
// @note this sample is just for SDK's developer, 99% of cases you should use log_producer_sample
#define POST_LOG_COUNT 10
#define POST_LOG_CONTENT_PAIRS 15
void post_logs_with_http_cont_lz4_log_option()
{
sls_log_init(LOG_GLOBAL_ALL);
int i = 0;
for (i = 0; i < 10; ++i)
{
log_group_builder *bder = log_group_create();
add_source(bder, "10.230.201.117", sizeof("10.230.201.117"));
add_topic(bder, "post_logs_with_http_cont_lz4_log_option",
sizeof("post_logs_with_http_cont_lz4_log_option"));
add_tag(bder, "taga_key", strlen("taga_key"), "taga_value",
strlen("taga_value"));
add_tag(bder, "tagb_key", strlen("tagb_key"), "tagb_value",
strlen("tagb_value"));
add_pack_id(bder, "123456789ABC", strlen("123456789ABC"), 0);
int x;
for (x = 0; x < POST_LOG_COUNT; x++)
{
char * keys[POST_LOG_CONTENT_PAIRS];
char * values[POST_LOG_CONTENT_PAIRS];
size_t key_lens[POST_LOG_CONTENT_PAIRS];
size_t val_lens[POST_LOG_CONTENT_PAIRS];
int j = 0;
for (j = 0; j < POST_LOG_CONTENT_PAIRS; j++)
{
keys[j] = (char *)malloc(64);
sprintf(keys[j], "key_%d", x * j);
values[j] = (char *)malloc(64);
sprintf(values[j], "value_%d", 2 * j);
key_lens[j] = strlen(keys[j]);
val_lens[j] = strlen(values[j]);
}
add_log_full(bder, time(NULL), POST_LOG_CONTENT_PAIRS, keys, key_lens, values, val_lens);
for (j = 0; j < POST_LOG_CONTENT_PAIRS; j++)
{
free(keys[j]);
free(values[j]);
}
}
// if you want to use this functions, add `-DADD_LOG_KEY_VALUE_FUN=ON` for cmake. eg `cmake . -DADD_LOG_KEY_VALUE_FUN=ON`
#ifdef LOG_KEY_VALUE_FLAG
for (x = 0; x < POST_LOG_COUNT; x++)
{
int j = 0;
add_log_begin(bder);
for (j = 0; j < POST_LOG_CONTENT_PAIRS; j++)
{
char key[64];
sprintf(key, "add_key_%d", x * j);
char value[64];
sprintf(value, "add_value_%d", 2 * j);
add_log_key_value(bder, key, strlen(key), value, strlen(value));
if (j == POST_LOG_CONTENT_PAIRS / 2)
{
add_log_time(bder, time(NULL));
}
}
add_log_end(bder);
}
#endif
log_post_option option;
memset(&option, 0, sizeof(log_post_option));
option.interface = NULL;
option.connect_timeout = 15;
option.operation_timeout = 15;
option.compress_type = i % 2 == 0;
lz4_log_buf *pLZ4Buf = NULL;
if (option.compress_type == 1)
{
printf("post log with lz4 \n");
pLZ4Buf = serialize_to_proto_buf_with_malloc_lz4(bder);
}
else
{
printf("post log with no compress \n");
pLZ4Buf = serialize_to_proto_buf_with_malloc_no_compress(bder);
}
log_group_destroy(bder);
if (pLZ4Buf == NULL)
{
printf("serialize_to_proto_buf_with_malloc_lz4 failed\n");
exit(1);
}
post_log_result * rst = post_logs_from_lz4buf(LOG_ENDPOINT, ACCESS_KEY_ID,
ACCESS_KEY_SECRET, NULL,
PROJECT_NAME, LOGSTORE_NAME,
pLZ4Buf, &option,
AUTH_VERSION_1, NULL);
printf("result %d %d \n", i, rst->statusCode);
if (rst->errorMessage != NULL)
{
printf("error message %s \n", rst->errorMessage);
}
if (rst->requestID != NULL)
{
printf("requestID %s \n", rst->requestID);
}
post_log_result_destroy(rst);
free_lz4_log_buf(pLZ4Buf);
}
sls_log_destroy();
}
// @note this sample is just for SDK's developer, 99% of cases you should use log_producer_sample
int main(int argc, char *argv[])
{
post_logs_with_http_cont_lz4_log_option();
return 0;
}