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 detailed information about SDK usage, visit the code repository tencentcloud-cls-sdk-c++.

Prerequisites

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

Preparing the Development Environment

Before installing the C++ SDK, you need to prepare a C++ development environment. See the following command to install the development environment (Centos is used as an example).
# Install the protoc compiler and dependencies (if not installed, C++ can be used)
sudo yum install gcc gcc-c++ automake autoconf libtool make
sudo yum install cmake

Installing the C++ SDK

Installing Dependencies

The C++ SDK relies on protobuf. Run the following command to install protobuf.
# Manual download protobuf
wget https://github.com/protocolbuffers/protobuf/archive/v2.6.1.tar.gz
# Decompress the source code
tar -xzf v2.6.1.tar.gz
# Access the decompressed directory
cd protobuf-2.6.1
# Generate configuration file. If the process hangs or a timed out error occurs, refer to the following [Common errors fix].
./autogen.sh
# Configure installation
./configure
# Compilation, this step may take a while, please wait patiently
make
sudo make install
sudo ldconfig

Download and Install SDK

Download and install the Tencent Cloud Log Service C++ SDK.
# Download the CLS SDK
git clone https://github.com/TencentCloud/tencentcloud-cls-sdk-cpp.git
# Enter directory
cd tencentcloud-cls-sdk-cpp
# Install dependency.
sudo yum install boost-devel
sudo yum install openssl-devel
sudo yum install libcurl-devel
# Generate build files
cmake .
# Compile SDK
make
sudo make install

Running the Demo

Take the log upload code sample.cpp under the cls folder in the Log Service SDK Demo as an example. The code analysis is shown below. In actual use, replace the key and other information in the code to run.
1. Compile example code.
# Compile example code
g++ -o sample ./cls/sample.cpp -std=c++11 -O2 -L/root/tencentcloud-cls-sdk-cpp-main -lclssdk -lcurl -lprotobuf -lssl -lcrypto -lboost_thread
2. Execute the following compiled sample file and upload logs. If the execution process reports error while....., see common errors.
./sample
If you see output similar to the following, it means the log upload was successful:
statusCode:200 requestId: content: bodyBytes:49 header:key:Content-Length value:0 header:key:Date value:Wed, 20 Nov 2024 03:37:25 GMT header:key:X-Cls-Requestid value:e7329d6d-9a48-4091-bb0c-5cea8d1c6f48 header:key:X-Cls-Trace-Id value: header:key:x-cls-requestid value:

Request Parameters

Variable
Type
Required
Description
endpoint
String
Yes
Domain information. For domain details, see the Log upload via API Tab in available region.
accesskeyid
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.
accessKeysecret
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

The following code takes sample.cpp in the SDK demo file as an example. By calling the SDK, it completes the log upload operation. The sample code is as follows.
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 see configure environment variables.
#include "cls/producerclient.h"
#include "cls/common.h"
#include "cls/cls_logs.pb.h"
#include "cls/logproducerconfig.pb.h"
#include <string>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include "cls/result.h"
#include "cls/error.h"
using namespace tencent_log_sdk_cpp_v2;
using namespace std;


class UserResult : public CallBack
{
public:
UserResult() = default;
~UserResult() = default;
void Success(PostLogStoreLogsResponse result) override { std::cout << result.Printf() << std::endl; }
void Fail(PostLogStoreLogsResponse result) override
{
std::cout << result.Printf() << std::endl;
// std::cout<<result.loggroup_.ShortDebugString().c_str()<<std::endl;
}
};


int main()
{
cls_config::LogProducerConfig config;
// Fill in domain information. Completion guide: https://www.tencentcloud.com/document/product/614/18940?from_cn_redirect=1#.E5.9F.9F.E5.90.8D. See the Log upload via API Tab in the link for domain details.
config.set_endpoint("ap-xxxxxxx.cls.tencentcs.com");

// Fill in cloud API Key Information. For key information acquisition, please visit: https://console.tencentcloud.com/cam/capi
// Please ensure the associated account of the key has appropriate log upload permissions. Permission configuration guide: 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. Environment variable configuration guide: https://www.tencentcloud.com/document/product/614/113851?from_cn_redirect=1
config.set_acceskeyid(getenv("TENCENTCLOUD_SECRET_ID"));
config.set_accesskeysecret(getenv("TENCENTCLOUD_SECRET_KEY"));

// Set the topic ID for log upload, replace with your Topic ID
std::string topic = "xxxxxxxxxxxxxxxxxxxxxxxxx";
auto client = std::make_shared<ProducerClient>(config);
auto callback = std::make_shared<UserResult>();

client->Start();
// Create a log object
cls::Log log;
// Set the current time as the log time
log.set_time(time(NULL));
// Construct the log data content
auto content = log.add_contents();
// Set the key-value information of the log content
content->set_key("content");
content->set_value("this my test");
// Send logs and obtain returned results
PostLogStoreLogsResponse ret = client->PostLogStoreLogs(topic, log, callback);
if(ret.statusCode != 0){
cout<<ret.content<<endl;
}
client->LogProducerEnvDestroy();

return 0;
}


Common Error Handling

Executing ./autogen.sh Timed Out and Got Stuck

1. In the protobuf directory, manually download the googletest file.
Download file, decompress and rename (fit execute code)
wget https://github.com/google/googletest/archive/release-1.5.0.tar.gz
tar -xzf googletest-release-1.5.0.tar.gz
mv googletest-release-1.5.0 gtest
2. In the protobuf directory, just continue execution ./autogen.sh.
Run command
./autogen.sh

Executing the Running File ./sample and Getting an Error

./sample: error while loading shared libraries: libprotobuf.so.9: cannot open shared object file: No such file or directory
1. View the location of the protobuf.so file by executing the following commands:
whereis libprotobuf.so.9
2. Execute cat /etc/ld.so.conf to check whether the path in step one exists.



3. If none are found, add the output path from step one to ld.so.conf. Taking the above figure as an example, add /usr/local/lib to ld.so.conf.
4. Execute ldconfig via command line to load the file.
ldconfig

Conclusions

Through these steps, you can quickly use the C++ SDK of Tencent Cloud CLS to complete log upload. If you encounter any issues, please contact us to get help.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback