| 1 | module nspkg |
| 2 | |
| 3 | #include "@DIR/native_log.h" |
| 4 | |
| 5 | // the message is borrowed for the callback; copy it before retaining it. |
| 6 | pub type CbLogLine = fn (voidptr, LogLevel, &u8) |
| 7 | |
| 8 | fn C.v_alpm_log_line_function() CbLog |
| 9 | fn C.v_alpm_log_line_create(CbLogLine, voidptr) voidptr |
| 10 | fn C.v_alpm_log_line_receiver(voidptr) CbLogLine |
| 11 | fn C.v_alpm_log_line_context(voidptr) voidptr |
| 12 | |
| 13 | fn (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. |
| 22 | pub 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 | |
| 34 | pub fn (handle &Handle) option_get_logcb_line() CbLogLine { |
| 35 | return C.v_alpm_log_line_receiver(handle.owned_log_line_context()) |
| 36 | } |
| 37 | |
| 38 | pub fn (handle &Handle) option_get_logcb_line_ctx() voidptr { |
| 39 | return C.v_alpm_log_line_context(handle.owned_log_line_context()) |
| 40 | } |
| 41 | |