yay/vendor/github.com/jguer/go-alpm/callbacks.go

57 lines
1.3 KiB
Go
Raw Normal View History

2017-07-14 19:03:54 +02:00
// callbacks.go - Handles libalpm callbacks.
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.
package alpm
/*
#include <stdint.h>
#include <alpm.h>
2018-07-26 14:58:51 +02:00
void logCallback(alpm_loglevel_t level, char *cstring);
2017-07-14 19:03:54 +02:00
void go_alpm_log_cb(alpm_loglevel_t level, const char *fmt, va_list arg);
void go_alpm_set_logging(alpm_handle_t *handle);
2018-03-16 21:54:28 +01:00
void go_alpm_set_question(alpm_handle_t *handle);
2017-07-14 19:03:54 +02:00
*/
import "C"
2018-03-16 21:54:28 +01:00
import (
"unsafe"
)
2018-07-26 14:58:51 +02:00
type logCallbackSig func(LogLevel, string)
2018-03-16 21:54:28 +01:00
type questionCallbackSig func(QuestionAny)
2017-07-14 19:03:54 +02:00
var DefaultLogLevel = LogWarning
2018-07-26 14:58:51 +02:00
func DefaultLogCallback(lvl LogLevel, s string) {
2017-07-14 19:03:54 +02:00
if lvl <= DefaultLogLevel {
print("go-alpm: ", s)
}
}
2018-03-16 21:54:28 +01:00
var log_callback logCallbackSig
var question_callback questionCallbackSig
2017-07-14 19:03:54 +02:00
//export logCallback
2018-07-26 14:58:51 +02:00
func logCallback(level C.alpm_loglevel_t, cstring *C.char) {
log_callback(LogLevel(level), C.GoString(cstring))
2017-07-14 19:03:54 +02:00
}
2018-03-16 21:54:28 +01:00
//export questionCallback
func questionCallback(question *C.alpm_question_t) {
q := (*C.alpm_question_any_t)(unsafe.Pointer(question))
question_callback(QuestionAny{q})
}
func (h *Handle) SetLogCallback(cb logCallbackSig) {
2017-07-14 19:03:54 +02:00
log_callback = cb
C.go_alpm_set_logging(h.ptr)
}
2018-03-16 21:54:28 +01:00
func (h *Handle) SetQuestionCallback(cb questionCallbackSig) {
question_callback = cb
C.go_alpm_set_question(h.ptr)
}