C SDK 上传日志

最近更新时间:2025-07-07 20:02:32

我的收藏
本文介绍如何快速使用日志服务的 C SDK 实现日志上传的操作。更多 SDK 使用的详细内容,可见代码仓库 tencentcloud-cls-sdk-c

前提条件

创建并获取云 API 密钥信息 accessKeyId 和 accessKey,密钥信息获取请前往 API 密钥管理
并请确保密钥关联的账号具有相应的 SDK 上传日志权限

准备开发环境

安装 C 语言开发环境

根据您的操作系统,安装 C 语言的编译环境和必要的开发工具。例如,在 CentOS 上,您可以使用以下命令安装开发工具和依赖库:
sudo yum groupinstall "Development Tools"
sudo yum install glibc-devel cmake openssl-devel git
sudo 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 .
# 编译 SDK
make

编译并运行示例程序

执行编译好的程序,上传日志。
# 进入可执行文件目录
cd build/bin
# 运行程序
./post_log_demo

请求参数

变量
类型
是否必填
说明
Endpoint
String
域名信息,填写请参见 可用地域 中 API 上传日志 Tab 中的域名。
AccessId
String
云 API 密钥信息,密钥信息获取请前往 API 密钥管理。并请确保密钥关联的账号具有相应的 SDK 上传日志权限
AccessKey
String
云 API 密钥信息,密钥信息获取请前往 API 密钥管理。并请确保密钥关联的账号具有相应的 SDK 上传日志权限
Topic
String
日志主题的 ID 信息。

日志上传示例代码

以下是一个简化的示例代码,展示了如何使用 C SDK 上传日志。
不建议将云 API 密钥信息明文存储至工程代码中,可通过环境变量动态获取云 API 密钥信息,具体操作,请参见 配置环境变量
#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.8D
SetEndpoint(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/113851
SetAccessId(config, getenv("TENCENTCLOUD_SECRET_ID"));
SetAccessKey(config, getenv("TENCENTCLOUD_SECRET_KEY"));
// 设置要上传日志的主题 ID,替换为您的 Topic ID
SetTopic(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;
}

总结

通过以上步骤,您可以快速开始使用 C SDK 上传日志到云服务。确保您已正确设置所有必要的配置,并根据需要调整示例代码。如遇到任何问题,请 联系我们 获取帮助。