// logfile lifetime and action records follow pacman log.c and handle.c. module engine import os import time #include fn C.syslog(i32, &char, ...voidptr) pub fn (mut handle Handle) log_action(prefix string, message string) ! { if handle == unsafe { nil } { return AlpmError{ kind: .handle_null } } if handle.released { return handle.fail(.handle_null, '') } mut problem := Errno.ok mut detail := '' if handle.action_log_fd < 0 && StringOption.logfile in handle.opts.strings { path := c_string_prefix(handle.opts.strings[.logfile]) for { handle.action_log_fd = C.open(unsafe { &char(path.str) }, C.O_WRONLY | C.O_APPEND | C.O_CREAT | C.O_CLOEXEC, 0o644) if handle.action_log_fd >= 0 { break } if C.errno == C.EINTR { continue } failure := os.last_error() detail = failure.msg() problem = match failure.code() { 13 { Errno.badperms } 2 { Errno.not_a_dir } else { Errno.system } } break } } text := c_string_prefix(message) if handle.opts.integers[.use_syslog] != 0 { C.syslog(C.LOG_WARNING, c'%s', text.str) } if handle.action_log_fd >= 0 { supplied := c_string_prefix(prefix) caller := if supplied == '' { 'UNKNOWN' } else { supplied } stamp := time.now().custom_format('YYYY-MM-DDTHH:mm:ssZZ') record := '[${stamp}] [${caller}] ${text}' mut position := 0 for position < record.len { count := C.write(handle.action_log_fd, unsafe { record.str + position }, uint(record.len - position)) if count < 0 && C.errno == C.EINTR { continue } if count <= 0 { problem = .system detail = os.last_error().msg() break } position += count } } if problem != .ok { return handle.fail(problem, detail) } } fn (mut handle Handle) close_action_log() { if handle.action_log_fd < 0 { return } descriptor := handle.action_log_fd handle.action_log_fd = -1 if C.close(descriptor) != 0 { handle.diagnostics << 'could not close action log: ${os.last_error().msg()}' } } fn (mut handle Handle) record_action(message string) { handle.record_action_as('ALPM', message) } fn (mut handle Handle) record_action_as(prefix string, message string) { handle.log_action(prefix, message) or { handle.diagnostics << 'could not write action log: ${err.msg()}' } } fn (mut handle Handle) record_failed_transaction() { // trans.c preserves the transaction error even if writing its failure fails. kind := handle.last_error detail := handle.last_detail valid := handle.last_detail_valid handle.record_action('transaction failed\n') handle.last_error = kind handle.last_detail = detail handle.last_detail_valid = valid }