すべてのプロダクト
Search
ドキュメントセンター

Simple Log Service:Simple Log Service C++ SDK クイックスタート

最終更新日:Apr 10, 2026

このトピックでは、Simple Log Service の C++ SDK の使用を開始し、一般的な操作を実行する方法について説明します。

前提条件

  • アクセス認証情報があります。

  • エンドポイントの取得

    エンドポイントとは、クライアントが Alibaba Cloud サービスにアクセスするために使用する URL です。プロトコル、ホスト名、ポートなどの詳細を指定します。

    • パブリックエンドポイント:Simple Log Service のパブリックエンドポイントからデータをプルする場合、アウトバウンドトラフィックに対して課金されます。課金の詳細については、「取り込みデータ量課金の課金項目」および「機能別課金の課金項目」をご参照ください。エンドポイントの完全なリストについては、「エンドポイント」をご参照ください。

    • VPC エンドポイント:プロジェクトと同じリージョンにある他の Alibaba Cloud サービスから Simple Log Service にアクセスする場合は、VPC エンドポイントを使用します。詳細については、「エンドポイント」をご参照ください。

    • Transfer Acceleration ドメイン名:アプリケーションサーバと Simple Log Service プロジェクトが異なるリージョンにある場合は、Transfer Acceleration ドメイン名を使用して、パブリックエンドポイント使用時の高いネットワーク遅延や潜在的な不安定性を回避します。詳細については、「Transfer Acceleration の管理」をご参照ください。

  • Simple Log Service の C++ SDK がインストール済みであること。詳細については、「Simple Log Service C++ SDK のインストール」をご参照ください。

サンプルコード

LogClient は、プロジェクトや Logstore などの Simple Log Service リソースを管理するために使用できる C++ クライアントです。Simple Log Service の C++ SDK を使用してリクエストを開始する前に、クライアントインスタンスを初期化する必要があります。例:

#include "client.h"
#include "common.h"
#include <string>
#include <cstdlib>
#include <iostream>
#include <unistd.h>
using namespace aliyun_log_sdk_v6;
using namespace std;
int main(int argc,char ** argv)
{
    // Simple Log Service のエンドポイント。この例では、中国 (杭州) リージョンのエンドポイントを使用します。実際にご利用のリージョンのエンドポイントに置き換えてください。
    string endpoint = "cn-hangzhou.log.aliyuncs.com";
    // この例では、環境変数から AccessKey ID と AccessKey Secret を取得します。
    string accessKeyId = (string)getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
    string accessKey = (string)getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
    // プロジェクト名。
    string project = "";
    // Logstore 名。
    string logstore = "";
    // ログトピック。
    string topic = "";
    // Simple Log Service クライアント。
    LOGClient * ptr = new LOGClient(endpoint,accessKeyId,accessKey,LOG_REQUEST_TIMEOUT,"127.0.0.1",false);
    // ログデータの構築。
    vector<LogItem> logGroup;
    for (int i = 0;i < 1;++i)
    {
        logGroup.push_back(LogItem());
        LogItem & item =  logGroup.back();
        item.timestamp = time(NULL) ;
        item.source="127.0.0.1";
        item.topic=topic;
        item.data.push_back(make_pair<string,string>("status","200"));
        item.data.push_back(make_pair<string,string>("latency","126"));
    }
    try{
        // ログの書き込み。
        ptr -> DeleteConsumerGroup(project,logstore,"test-consumer-group");
        uint32_t shardId = 2;
        int32_t beginTime =  time(NULL);
        ptr -> PostLogStoreLogs(project,logstore,topic,logGroup);
        sleep(1);
        ptr -> PostLogStoreLogs(project,logstore,topic,logGroup);
        sleep(1);
        int32_t endTime = time(NULL);
        ptr -> PostLogStoreLogs(project,logstore,topic,logGroup);
        sleep(1);
        ptr -> PostLogStoreLogs(project,logstore,topic,logGroup);
        sleep(1);
        // ログのクエリ。
        GetCursorResponse beginCursorResp = ptr->GetCursor(project, logstore, shardId, beginTime);
        GetCursorResponse endCursorResp = ptr->GetCursor(project, logstore, shardId, endTime);
        GetBatchLogResponse getLogResp = ptr->GetBatchLog(project, logstore, shardId, 1000, beginCursorResp.result);
        cout << getLogResp.result.logGroups.size() << "\t" << getLogResp.result.logGroupCount << endl;
        getLogResp = ptr->GetBatchLog(project, logstore, 2, 1000, beginCursorResp.result, endCursorResp.result);
        cout << getLogResp.result.logGroups.size() << "\t" << getLogResp.result.logGroupCount << endl;

        ListConsumerGroupResponse lrs = ptr -> ListConsumerGroup(project, logstore);
        for(int i = 0; i < lrs.consumerGroups.size(); ++i)
            cout<<lrs.consumerGroups[i].GetConsumerGroupName()<<", " <<lrs.consumerGroups[i].GetTimeoutInSec()<<", " <<lrs.consumerGroups[i].GetInOrder()<<endl;
        ptr->UpdateCheckpoint(project, logstore, "hahhah", 0, "V0hBVFRIRlVDSw==");
        ListCheckpointResponse lcps = ptr -> ListCheckpoint(project, logstore, "hahhah");
        for(int i =0; i < lcps.consumerGroupCheckpoints.size(); ++i)
        {
            cout<<lcps.consumerGroupCheckpoints[i].GetShard() <<", " << lcps.consumerGroupCheckpoints[i].GetCheckpoint()<<", "<<lcps.consumerGroupCheckpoints[i].GetUpdateTime()<<endl;
        }
        while(true){
            HeartbeatResponse resp = ptr->ConsumerGroupHeartbeat(project, logstore, "hahhah", "cc1", std::vector<uint32_t>());
            cout<<"cc1, heartbeat: ";
            for(std::vector<uint32_t>::const_iterator it = resp.shards.begin(); it != resp.shards.end(); ++it)
                cout<<*it<<", ";
            cout<<endl;
            resp = ptr->ConsumerGroupHeartbeat(project, logstore, "hahhah", "cc2", std::vector<uint32_t>());
            cout<<"cc2, heartbeat: ";
            for(std::vector<uint32_t>::const_iterator it = resp.shards.begin(); it != resp.shards.end(); ++it)
                cout<<*it<<", ";
            cout<<endl;
            usleep(2 * 1000 * 1000);
        }
    }
    catch(LOGException & e)
    {
        cout<<e.GetErrorCode()<<":"<<e.GetMessage()<<endl;
    }
    delete ptr;
}