Standardize logging and error handling

- Introduce new types: LoggedResult and LoggedError
- Introduce new extension methods to log and add additional msgs
- Never exit when logging error messages in Rust (all errors should be
  handled by using Result and Rust's error propagation)
- Remove all usages of anyhow as it doesn't fit Magisk's use cases
This commit is contained in:
topjohnwu
2023-06-29 16:44:44 -07:00
parent dbc2236dd2
commit 4ee4cbada6
16 changed files with 254 additions and 145 deletions

View File

@@ -1,9 +1,9 @@
// Functions listed here are just to export to C++
use std::fmt::Write;
use std::io;
use std::os::fd::{BorrowedFd, OwnedFd, RawFd};
use anyhow::Context;
use cxx::private::c_char;
use libc::mode_t;
@@ -14,8 +14,7 @@ use crate::{
pub(crate) fn fd_path_for_cxx(fd: RawFd, buf: &mut [u8]) -> isize {
fd_path(fd, buf)
.context("fd_path failed")
.log()
.log_cxx_with_msg(|w| w.write_str("fd_path failed"))
.map_or(-1_isize, |v| v as isize)
}
@@ -54,7 +53,7 @@ unsafe extern "C" fn frm_rf(fd: OwnedFd) -> bool {
pub(crate) fn map_file_for_cxx(path: &[u8], rw: bool) -> &'static mut [u8] {
unsafe {
map_file(Utf8CStr::from_bytes_unchecked(path), rw)
.log()
.log_cxx()
.unwrap_or(&mut [])
}
}
@@ -62,7 +61,7 @@ pub(crate) fn map_file_for_cxx(path: &[u8], rw: bool) -> &'static mut [u8] {
pub(crate) fn map_fd_for_cxx(fd: RawFd, sz: usize, rw: bool) -> &'static mut [u8] {
unsafe {
map_fd(BorrowedFd::borrow_raw(fd), sz, rw)
.log()
.log_cxx()
.unwrap_or(&mut [])
}
}