github.com/ubuntu/ubuntu-report@v1.7.4-0.20240410144652-96f37d845fac/pkg/sysmetrics/C/libsysmetrics.go (about)

     1  package main
     2  
     3  /*
     4  // sysmetrics_report_type define the desired kind of interaction in sysmetrics_collect_and_send()
     5  typedef enum {
     6      // sysmetrics_report_interactive will show report content on stdout and read anwser on stdin
     7      sysmetrics_report_interactive = 0,
     8      // sysmetrics_report_auto will send a report without printing report
     9      sysmetrics_report_auto = 1,
    10      // sysmetrics_report_optout will send opt-out message without printing report
    11      sysmetrics_report_optout = 2,
    12  } sysmetrics_report_type;
    13  */
    14  import "C"
    15  
    16  import (
    17  	"github.com/ubuntu/ubuntu-report/pkg/sysmetrics"
    18  )
    19  
    20  // generate shared library and header
    21  //go:generate sh -c "go build -o ../../../build/libsysmetrics.so.1 -buildmode=c-shared -ldflags \"-extldflags -Wl,-soname,libsysmetrics.so.1\" libsysmetrics.go && mv ../../../build/libsysmetrics.so.h ../../../build/libsysmetrics.h"
    22  
    23  // sysmetrics_collect system info and return a pretty printed version of collected data
    24  //export sysmetrics_collect
    25  func sysmetrics_collect(res **C.char) *C.char {
    26  	b, err := sysmetrics.Collect()
    27  	*res = C.CString(string(b))
    28  	if err != nil {
    29  		*res = C.CString("") // scratch data
    30  		return C.CString(err.Error())
    31  	}
    32  	return nil
    33  }
    34  
    35  // sysmetrics_send_report sends provided metrics data to server.
    36  // The report will not be sent if a report has already been sent for this version unless "alwaysReport" is true.
    37  // If "baseURL" is not an empty string, this overrides the server the report is sent to.
    38  // The return "err" will be != NULL in case any error occurred during POST.
    39  //export sysmetrics_send_report
    40  func sysmetrics_send_report(data *C.char, alwaysReport bool, baseURL *C.char) *C.char {
    41  	err := sysmetrics.SendReport([]byte(C.GoString(data)), alwaysReport, C.GoString(baseURL))
    42  	if err != nil {
    43  		return C.CString(err.Error())
    44  	}
    45  	return nil
    46  }
    47  
    48  // sysmetrics_send_decline sends denial message to server.
    49  // The denial message will not be sent if a report has already been sent for this version unless "alwaysReport" is true.
    50  // If "baseURL" is not an empty string, this overrides the server the report is sent to.
    51  // The return "err" will be != NULL in case any error occurred during POST.
    52  //export sysmetrics_send_decline
    53  func sysmetrics_send_decline(alwaysReport bool, baseURL *C.char) *C.char {
    54  	err := sysmetrics.SendDecline(alwaysReport, C.GoString(baseURL))
    55  	if err != nil {
    56  		return C.CString(err.Error())
    57  	}
    58  	return nil
    59  }
    60  
    61  // sysmetrics_collect_and_send gather system info and send them
    62  // The report will not be sent if a report has already been sent for this version unless "alwaysReport" is true.
    63  // It can be send to an alternative url via baseURL to send the report to, if not empty
    64  // The return "err" will be != NULL in case any error occurred during POST.
    65  //export sysmetrics_collect_and_send
    66  func sysmetrics_collect_and_send(r C.sysmetrics_report_type, alwaysReport bool, baseURL *C.char) *C.char {
    67  	err := sysmetrics.CollectAndSend(sysmetrics.ReportType(r), alwaysReport, C.GoString(baseURL))
    68  	if err != nil {
    69  		return C.CString(err.Error())
    70  	}
    71  	return nil
    72  }
    73  
    74  func main() {
    75  
    76  }