module nspkg import os #include "@VMODROOT/tests/log_probe.h" fn C.v_alpm_log_probe(CbLog, voidptr, LogLevel, &u8, i32) struct LogLineTrace { mut: messages []string levels []LogLevel handle &Handle = unsafe { nil } replacement &LogLineTrace = unsafe { nil } } fn record_log_line(context voidptr, level LogLevel, text &u8) { mut state := unsafe { &LogLineTrace(context) } state.messages << unsafe { cstring_to_vstring(text) } state.levels << level } fn replace_log_line(context voidptr, level LogLevel, text &u8) { mut state := unsafe { &LogLineTrace(context) } record_log_line(context, level, text) state.handle.option_set_logcb_line(record_log_line, state.replacement) or { assert false, err.msg() } } fn record_raw_log(context voidptr, level LogLevel, format &u8, _ voidptr) { record_log_line(context, level, format) } fn test_log_line_callback_formats_full_messages_and_preserves_raw_callback_api() ! { base := os.join_path(os.vtmp_dir(), 'alpm-log-lines-${os.getpid()}') os.mkdir_all(base)! defer { os.rmdir_all(base) or { eprintln(err) } } handle := initialize('/', base)! defer { handle.release() or { eprintln(err) } } assert voidptr(handle.option_get_logcb_line()) == unsafe { nil } assert handle.option_get_logcb_line_ctx() == unsafe { nil } mut state := &LogLineTrace{} handle.option_set_logcb_line(record_log_line, state)! assert voidptr(handle.option_get_logcb_line()) == voidptr(record_log_line) assert handle.option_get_logcb_line_ctx() == voidptr(state) C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .warning, c'café λ', 12) assert state.messages == ['café λ:12\n'] assert state.levels == [LogLevel.warning] long_message := 'data'.repeat(2048) C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .debug, long_message.str, -8) assert state.messages.last() == long_message + ':-8\n' // resetting the current raw pair must not free the active line adapter's state. handle.option_set_logcb(handle.option_get_logcb(), handle.option_get_logcb_ctx())! C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .error, c'after-reset', 3) assert state.messages.last() == 'after-reset:3\n' handle.option_set_logcb(record_raw_log, state)! assert voidptr(handle.option_get_logcb_line()) == unsafe { nil } assert handle.option_get_logcb_line_ctx() == unsafe { nil } assert handle.option_get_logcb_ctx() == voidptr(state) C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .error, c'raw', 7) assert state.messages.last() == '%s:%d\n' handle.option_set_logcb_line(record_log_line, state)! handle.option_set_logcb_line(unsafe { nil }, unsafe { nil })! assert voidptr(handle.option_get_logcb()) == unsafe { nil } assert voidptr(handle.option_get_logcb_line()) == unsafe { nil } } fn test_log_line_callback_can_replace_itself_during_delivery() ! { base := os.join_path(os.vtmp_dir(), 'alpm-log-replace-${os.getpid()}') os.mkdir_all(base)! defer { os.rmdir_all(base) or { eprintln(err) } } handle := initialize('/', base)! defer { handle.release() or { eprintln(err) } } mut second := &LogLineTrace{} mut first := &LogLineTrace{ handle: handle, replacement: second } handle.option_set_logcb_line(replace_log_line, first)! C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .warning, c'first', 1) C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .error, c'second', 2) assert first.messages == ['first:1\n'] assert second.messages == ['second:2\n'] assert handle.option_get_logcb_line_ctx() == voidptr(second) } fn handle_with_only_a_log_line_context(path string) !&Handle { handle := initialize('/', path)! handle.option_set_logcb_line(record_log_line, &LogLineTrace{})! return handle } fn test_log_line_context_in_its_libc_adapter_remains_reachable_by_the_collector() ! { base := os.join_path(os.vtmp_dir(), 'alpm-log-line-gc-${os.getpid()}') os.mkdir_all(base)! defer { os.rmdir_all(base) or { eprintln(err) } } handle := handle_with_only_a_log_line_context(base)! defer { handle.release() or { eprintln(err) } } for index in 0 .. 8 { gc_collect() C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .warning, c'retained', i32(index)) } state := unsafe { &LogLineTrace(handle.option_get_logcb_line_ctx()) } assert state.messages == ['retained:0\n', 'retained:1\n', 'retained:2\n', 'retained:3\n', 'retained:4\n', 'retained:5\n', 'retained:6\n', 'retained:7\n'] handle.option_set_logcb_line(unsafe { nil }, unsafe { nil })! assert handle.option_get_logcb_line_ctx() == unsafe { nil } }