tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

Unable to set log message callback form golang #2611

Closed krlspj closed 1 year ago

krlspj commented 1 year ago

Hello,

I've been attempting to use td_set_log_message_callback in Go, but despite doing some research, I'm unable to get it working. I'm sure there's a way to make it work but im stuck... I would appreciate any assistance or guidance you can provide. Thank you! this is the code Im trying to make it work

package main

// #cgo CFLAGS: -I/usr/local/include
// #cgo LDFLAGS: -L/usr/local/lib -ltdjson
// #include <td/telegram/td_json_client.h>
// #include <td/telegram/td_log.h>
// #include <stdlib.h>
// extern void log_callback(void);
import "C"
import (
    "fmt"
)

// export log_callback
func log_callback() {
    fmt.Println("---------- this is my log callback from go!! ")
}

func main() {
    fmt.Println(">>", C.GoString(C.td_execute(C.CString(`{"@type":"setLogVerbosityLevel", "new_verbosity_level":1, "@extra": 1.01234}`))))

    // Create a TDLib client
    cClientID := C.td_create_client_id()

    C.td_execute(C.CString(`{'@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me', '@extra': ['5', 7.0, 'a']}`))

    C.td_send(cClientID, C.CString(`{"@type":"getOption", "name":"version", "@extra": 1.01234}`))

    C.td_set_log_message_callback(C.int(2), (*[0]byte)(C.log_callback))

}

This is the error i got when trying to run:

# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: /tmp/go-link-1230379501/go.o:(.data+0x8): undefined reference to `log_callback'
collect2: error: ld returned 1 exit status
levlam commented 1 year ago

You wrote orfinary comment "// export" instead of cgo command "//export".

krlspj commented 1 year ago

you are right! thanks a lot