tencent cloud

C SDK Log Upload
Last updated: 2025-11-07 17:40:37
C SDK Log Upload
Last updated: 2025-11-07 17:40:37
This article introduces how to quickly get started with the C SDK of Cloud Log Service (CLS) to implement log upload. For more details on SDK usage, visit the code repository tencentcloud-cls-sdk-c.

Prerequisites

Create and obtain TencentCloud API key information accessKeyId and accessKey. For key information acquisition, please visit API Key Management.
Please ensure the associated account has appropriate SDK log upload permission.

Preparing the Development Environment

Installing C Language Development Environment

Install the C language compiling environment and necessary development tools based on your operating system. For example, on CentOS, use the following commands to install development tools and dependency libraries:
sudo yum groupinstall "Development Tools"
sudo yum install glibc-devel cmake openssl-devel git
sudo yum install libcurl-devel

Installing the C SDK

1. Download the C SDK: Clone the C SDK warehouse locally.
git clone https://github.com/TencentCloud/tencentcloud-cls-sdk-c.git
2. Modify the code in the demo.
In the demo directory of the SDK, you will find the example code log_post.c. The code analysis is shown below. Modify the example code according to your needs and ensure you replace the Endpoint, AccessId, AccessKey, and Topic parameters with your actual information.
cd tencentcloud-cls-sdk-c/demo
3. Enter the SDK directory, use the cmake and make tools to compile and install the SDK.
cd ..
# Generate build files
cmake .
# Compile the SDK
make

Compile and Run the Sample Program

Execute compiled program. Upload log.
# Enter the executable file directory
cd build/bin
Run program
./post_log_demo

Request Parameters

Variable
Type
Required
Description
Endpoint
String
Yes
Domain information, fill in please refer to available region for the domain name in the Log upload via API Tab.
AccessId
String
Yes
TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has appropriate SDK log upload permission.
AccessKey
String
Yes
TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has appropriate SDK log upload permission.
Topic
String
Yes
Log topic ID info.

Upload Example Code for Logs

Here is a simplified example code that shows how to use the C SDK for log upload.
It is not recommended to store TencentCloud API key information in plaintext in project code. You can dynamically obtain the API key information through environment variables. For detailed operations, please refer to configure environment variables.
#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)
{
//Call the malloc function to allocate memory and initialize the default configuration
ProducerConfig *config = ConstructLogConfig();
// Fill in domain information, see the domain name in the Log upload via API Tab: https://www.tencentcloud.com/document/product/614/18940?from_cn_redirect=1#.E5.9F.9F.E5.90.8D
SetEndpoint(config, "ap-xxxxxxxxx.cls.tencentcs.com");
// Fill in the cloud API key information. For key information acquisition, please visit: https://console.tencentcloud.com/cam/capi
// Please ensure the associated account has appropriate log upload permissions. For the permission configuration guide, see: 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
// This example retrieves from environmental variables. For environment variable configuration, see https://www.tencentcloud.com/document/product/614/113851?from_cn_redirect=1
SetAccessId(config, getenv("TENCENTCLOUD_SECRET_ID"));
SetAccessKey(config, getenv("TENCENTCLOUD_SECRET_KEY"));
// Set the topic ID for log upload, replace with your 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;
}

Summary

Through these steps, you can quickly get started with the C SDK to upload logs to the cloud service. Ensure you have correctly set all required configurations and adjust the example code as needed. If you encounter any issues, please contact us for help.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback