前提条件
创建并获取云 API 密钥信息 accessKeyId 和 accessKey,密钥信息获取请前往 API 密钥管理。
并请确保密钥关联的账号具有相应的 SDK 上传日志权限。
准备开发环境
安装 C 语言开发环境
根据您的操作系统,安装 C 语言的编译环境和必要的开发工具。例如,在 CentOS 上,您可以使用以下命令安装开发工具和依赖库:
sudo yum groupinstall "Development Tools"sudo yum install glibc-devel cmake openssl-devel gitsudo yum install libcurl-devel
安装 C SDK
1. 下载 C SDK:克隆 C SDK 的仓库到本地。
git clone https://github.com/TencentCloud/tencentcloud-cls-sdk-c.git
2. 修改 demo 中代码。
在 SDK 的 demo 目录中,您会找到示例代码 log_post.c,代码解析见下方。请根据您的需求修改示例代码,并确保替换其中的 Endpoint、AccessId、AccessKey 和 Topic 参数为您的实际信息。
cd tencentcloud-cls-sdk-c/demo
3. 回到进入 SDK 目录,使用 cmake 和 make 工具编译并安装 SDK。
cd ..# 生成构建文件cmake .# 编译 SDKmake
编译并运行示例程序
执行编译好的程序,上传日志。
# 进入可执行文件目录cd build/bin# 运行程序./post_log_demo
请求参数
变量 | 类型 | 是否必填 | 说明 |
Endpoint | String | 是 | |
AccessId | String | 是 | |
AccessKey | String | 是 | |
Topic | String | 是 | 日志主题的 ID 信息。 |
日志上传示例代码
以下是一个简化的示例代码,展示了如何使用 C SDK 上传日志。
#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://cloud.tencent.com/document/product/614/18940#.E5.9F.9F.E5.90.8DSetEndpoint(config, "ap-xxxxxxxxx.cls.tencentcs.com");// 填入云API密钥信息。密钥信息获取请前往:https://console.cloud.tencent.com/cam/capi// 并请确保密钥关联的账号具有相应的日志上传权限,权限配置指引:https://cloud.tencent.com/document/product/614/68374#.E4.BD.BF.E7.94.A8-api-.E4.B8.8A.E4.BC.A0.E6.95.B0.E6.8D.AE// 本示例从环境变量中获取,环境变量配置指引:https://cloud.tencent.com/document/product/614/113851SetAccessId(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;}