github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/logtail/logtail.go (about)

     1  // Copyright 2021 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build linux || windows
    16  // +build linux windows
    17  
    18  package logtail
    19  
    20  /*
    21  #cgo CFLAGS: -I./
    22  #cgo !windows LDFLAGS: -L./ -lGoPluginAdapter -Wl,-rpath,/usr/lib64:
    23  #cgo windows LDFLAGS: -L./ -lGoPluginAdapter -static-libgcc
    24  #include "LogtailPluginAdapter.h"
    25  */
    26  import "C"
    27  
    28  import (
    29  	"fmt"
    30  	"unsafe"
    31  
    32  	"github.com/alibaba/ilogtail/pkg/util"
    33  )
    34  
    35  func ExecuteCMD(configName string, cmdType int, params []byte) error {
    36  	var rstVal C.int
    37  	if len(params) == 0 {
    38  		rstVal = C.LogtailCtlCmd((*C.char)(util.StringPointer(configName)), C.int(len(configName)),
    39  			C.int(cmdType),
    40  			(*C.char)(unsafe.Pointer(nil)), C.int(0))
    41  	} else {
    42  		rstVal = C.LogtailCtlCmd((*C.char)(util.StringPointer(configName)), C.int(len(configName)),
    43  			C.int(cmdType),
    44  			(*C.char)(unsafe.Pointer(&params[0])), C.int(len(params)))
    45  	}
    46  
    47  	if rstVal < C.int(0) {
    48  		return fmt.Errorf("execute cmd error %d", int(rstVal))
    49  	}
    50  	return nil
    51  }
    52  
    53  func SendPb(configName string, logstore string, pbBuffer []byte, lines int) int {
    54  	rstVal := C.LogtailSendPb((*C.char)(util.StringPointer(configName)), C.int(len(configName)),
    55  		(*C.char)(util.StringPointer(logstore)), C.int(len(logstore)),
    56  		(*C.char)(unsafe.Pointer(&pbBuffer[0])), C.int(len(pbBuffer)),
    57  		C.int(lines))
    58  	return int(rstVal)
    59  }
    60  
    61  func SendPbV2(configName string, logstore string, pbBuffer []byte, lines int, hash string) int {
    62  	rstVal := C.LogtailSendPbV2((*C.char)(util.StringPointer(configName)), C.int(len(configName)),
    63  		(*C.char)(util.StringPointer(logstore)), C.int(len(logstore)),
    64  		(*C.char)(unsafe.Pointer(&pbBuffer[0])), C.int(len(pbBuffer)),
    65  		C.int(lines),
    66  		(*C.char)(util.StringPointer(hash)), C.int(len(hash)))
    67  	return int(rstVal)
    68  }
    69  
    70  func IsValidToSend(logstoreKey int64) bool {
    71  	return C.LogtailIsValidToSend(C.longlong(logstoreKey)) == 0
    72  }