nspkg / logging.v
40 lines · 33 sloc · 1.34 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module nspkg
2
3#include "@DIR/native_log.h"
4
5// the message is borrowed for the callback; copy it before retaining it.
6pub type CbLogLine = fn (voidptr, LogLevel, &u8)
7
8fn C.v_alpm_log_line_function() CbLog
9fn C.v_alpm_log_line_create(CbLogLine, voidptr) voidptr
10fn C.v_alpm_log_line_receiver(voidptr) CbLogLine
11fn C.v_alpm_log_line_context(voidptr) voidptr
12
13fn (handle &Handle) owned_log_line_context() voidptr {
14 if handle == unsafe { nil } { return unsafe { nil } }
15 if voidptr(handle.option_get_logcb()) == voidptr(C.v_alpm_log_line_function()) {
16 return handle.option_get_logcb_ctx()
17 }
18 return unsafe { nil }
19}
20
21// formatting stays behind the library boundary rather than in frontend C shims.
22pub fn (handle &Handle) option_set_logcb_line(callback CbLogLine, context voidptr) ! {
23 if voidptr(callback) == unsafe { nil } {
24 return handle.option_set_logcb(unsafe { nil }, context)
25 }
26 state := C.v_alpm_log_line_create(callback, context)
27 if state == unsafe { nil } { return Errno.memory.ierror() }
28 handle.option_set_logcb(C.v_alpm_log_line_function(), state) or {
29 C.free(state)
30 return err
31 }
32}
33
34pub fn (handle &Handle) option_get_logcb_line() CbLogLine {
35 return C.v_alpm_log_line_receiver(handle.owned_log_line_context())
36}
37
38pub fn (handle &Handle) option_get_logcb_line_ctx() voidptr {
39 return C.v_alpm_log_line_context(handle.owned_log_line_context())
40}
41