产品动态
公告
sudo yum groupinstall "Development Tools"sudo yum install glibc-devel cmake openssl-devel gitsudo yum install libcurl-devel
git clone https://github.com/TencentCloud/tencentcloud-cls-sdk-c.git
cd tencentcloud-cls-sdk-c/demo
cd ..# 生成构建文件cmake .# 编译 SDKmake
# 进入可执行文件目录cd build/bin# 运行程序./post_log_demo
变量 | 类型 | 是否必填 | 说明 |
Endpoint | String | 是 | |
AccessId | String | 是 | |
AccessKey | String | 是 | |
Topic | String | 是 | 日志主题的 ID 信息。 |
#include "log_producer_config.h"#include "log_producer_client.h"#include "log_error.h"#include <stdio.h>#include <stdlib.h>void callback(const char *config_name, int 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){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{if (message == NULL){message = "";}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);}}clslogproducer *ConstructorLogProducer(SendCallBackFunc notifyFunc){//调用malloch函数开辟内存,并初始化默认的配置ProducerConfig *config = ConstructLogConfig();// 填入域名信息,请参见链接中 API 上传日志 Tab 中的域名:https://www.tencentcloud.com/document/product/614/18940?from_cn_redirect=1#.E5.9F.9F.E5.90.8DSetEndpoint(config, "ap-xxxxxxxxx.cls.tencentcs.com");// 填入云API密钥信息。密钥信息获取请前往:https://console.tencentcloud.com/cam/capi// 并请确保密钥关联的账号具有相应的日志上传权限,权限配置指引:https://www.tencentcloud.com/document/product/614/68374?from_cn_redirect=1#.E4.BD.BF.E7.94.A8-api-.E4.B8.8A.E4.BC.A0.E6.95.B0.E6.8D.AE// 本示例从环境变量中获取,环境变量配置指引:https://www.tencentcloud.com/document/product/614/113851?from_cn_redirect=1SetAccessId(config, getenv("TENCENTCLOUD_SECRET_ID"));SetAccessKey(config, getenv("TENCENTCLOUD_SECRET_KEY"));// 设置要上传日志的主题 ID,替换为您的 Topic IDSetTopic(config, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");SetPackageLogBytes(config, 4 * 1024 * 1024);setPackageTimeout(config, 3000);SetMaxBufferLimit(config, 64 * 1024 * 1024);set_send_thread_count(config, 4);SetCompressType(config, 1);SetConnectTtimeoutSec(config, 10);SetSendTimeoutSec(config, 15);return ConstructorClsLogProducer(config, notifyFunc, NULL);}void post_logs(){if (ClsLogProducerInit(LOG_GLOBAL_ALL) != LOG_PRODUCER_OK){printf("ClsLogProducerInit init fail \\n");exit(1);}clslogproducer *producer = ConstructorLogProducer(callback);if (producer == NULL){printf("create log producer by config fail \\n");exit(1);}clslogproducerclient *client = GetClsLogProducer(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);int rst = PostClsLog(client, 20, "key1", "value_1","key2", "value_2","key3", "value_3","key4", "value_4","key5", "value_5","key6", "value_6","key7", "value_7","key8", "value_8","key9", "value_9","index", indexStr);if (rst != LOG_PRODUCER_OK){printf("add log error %d \\n", rst);}}DestructorClsLogProducer(producer);ClsLogProducerDestroy();}int main(int argc, char *argv[]){post_logs();return 0;}
文档反馈