2025-04-14 18:05:14 -07:00
|
|
|
use crate::logging::Formatter;
|
2025-09-08 23:59:29 -07:00
|
|
|
use crate::{LogLevel, log_with_args, log_with_formatter};
|
|
|
|
|
use nix::errno::Errno;
|
|
|
|
|
use std::fmt;
|
2024-03-05 01:48:35 -08:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
use std::panic::Location;
|
2025-04-14 13:31:52 -07:00
|
|
|
use std::ptr::NonNull;
|
2024-03-05 01:48:35 -08:00
|
|
|
|
|
|
|
|
// Error handling throughout the Rust codebase in Magisk:
|
|
|
|
|
//
|
|
|
|
|
// All errors should be logged and consumed as soon as possible and converted into LoggedError.
|
|
|
|
|
// For `Result` with errors that implement the `Display` trait, use the `?` operator to
|
|
|
|
|
// log and convert to LoggedResult.
|
|
|
|
|
//
|
|
|
|
|
// To log an error with more information, use `ResultExt::log_with_msg()`.
|
|
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct LoggedError {}
|
|
|
|
|
pub type LoggedResult<T> = Result<T, LoggedError>;
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! log_err {
|
2025-08-21 21:05:35 -07:00
|
|
|
() => {{
|
|
|
|
|
Err($crate::LoggedError::default())
|
|
|
|
|
}};
|
2024-03-05 01:48:35 -08:00
|
|
|
($($args:tt)+) => {{
|
2025-05-30 11:11:36 -07:00
|
|
|
$crate::error!($($args)+);
|
2025-08-21 21:05:35 -07:00
|
|
|
Err($crate::LoggedError::default())
|
2024-03-05 01:48:35 -08:00
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Any result or option can be silenced
|
2025-08-21 22:26:36 -07:00
|
|
|
pub trait SilentLogExt<T> {
|
2024-03-05 01:48:35 -08:00
|
|
|
fn silent(self) -> LoggedResult<T>;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
impl<T, E> SilentLogExt<T> for Result<T, E> {
|
2024-03-05 01:48:35 -08:00
|
|
|
fn silent(self) -> LoggedResult<T> {
|
2025-08-21 22:26:36 -07:00
|
|
|
self.map_err(|_| LoggedError::default())
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
impl<T> SilentLogExt<T> for Option<T> {
|
2024-03-05 01:48:35 -08:00
|
|
|
fn silent(self) -> LoggedResult<T> {
|
2025-08-21 22:26:36 -07:00
|
|
|
self.ok_or_else(LoggedError::default)
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Public API for logging results
|
|
|
|
|
pub trait ResultExt<T> {
|
|
|
|
|
fn log(self) -> LoggedResult<T>;
|
|
|
|
|
fn log_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T>;
|
2025-02-04 21:15:16 +08:00
|
|
|
fn log_ok(self);
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
// Public API for converting Option to LoggedResult
|
|
|
|
|
pub trait OptionExt<T> {
|
|
|
|
|
fn ok_or_log(self) -> LoggedResult<T>;
|
|
|
|
|
fn ok_or_log_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> OptionExt<T> for Option<T> {
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn ok_or_log(self) -> LoggedResult<T> {
|
|
|
|
|
self.ok_or_else(LoggedError::default)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
|
fn ok_or_log_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
|
|
|
|
|
self.ok_or_else(|| {
|
|
|
|
|
do_log_msg(LogLevel::Error, None, f);
|
|
|
|
|
LoggedError::default()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
|
fn ok_or_log_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
|
|
|
|
|
let caller = Some(Location::caller());
|
|
|
|
|
self.ok_or_else(|| {
|
|
|
|
|
do_log_msg(LogLevel::Error, caller, f);
|
|
|
|
|
LoggedError::default()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait Loggable {
|
|
|
|
|
fn do_log(self, level: LogLevel, caller: Option<&'static Location>) -> LoggedError;
|
2024-03-05 01:48:35 -08:00
|
|
|
fn do_log_msg<F: FnOnce(Formatter) -> fmt::Result>(
|
|
|
|
|
self,
|
|
|
|
|
level: LogLevel,
|
|
|
|
|
caller: Option<&'static Location>,
|
|
|
|
|
f: F,
|
2025-08-21 22:26:36 -07:00
|
|
|
) -> LoggedError;
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
impl<T, E: Loggable> ResultExt<T> for Result<T, E> {
|
2024-03-05 01:48:35 -08:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
|
fn log(self) -> LoggedResult<T> {
|
2025-08-21 22:26:36 -07:00
|
|
|
self.map_err(|e| e.do_log(LogLevel::Error, None))
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
|
fn log(self) -> LoggedResult<T> {
|
2025-08-21 22:26:36 -07:00
|
|
|
let caller = Some(Location::caller());
|
|
|
|
|
self.map_err(|e| e.do_log(LogLevel::Error, caller))
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
|
2025-02-16 11:46:42 -08:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
|
fn log_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
|
2025-08-21 22:26:36 -07:00
|
|
|
self.map_err(|e| e.do_log_msg(LogLevel::Error, None, f))
|
2025-02-16 11:46:42 -08:00
|
|
|
}
|
|
|
|
|
|
2025-02-04 21:15:16 +08:00
|
|
|
#[track_caller]
|
|
|
|
|
#[cfg(debug_assertions)]
|
2025-02-16 11:46:42 -08:00
|
|
|
fn log_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
|
2025-08-21 22:26:36 -07:00
|
|
|
let caller = Some(Location::caller());
|
|
|
|
|
self.map_err(|e| e.do_log_msg(LogLevel::Error, caller, f))
|
2025-02-04 21:15:16 +08:00
|
|
|
}
|
2025-02-16 11:46:42 -08:00
|
|
|
|
2025-02-04 21:15:16 +08:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
|
fn log_ok(self) {
|
2025-08-21 22:26:36 -07:00
|
|
|
self.map_err(|e| e.do_log(LogLevel::Error, None)).ok();
|
2025-02-04 21:15:16 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-05 01:48:35 -08:00
|
|
|
#[track_caller]
|
|
|
|
|
#[cfg(debug_assertions)]
|
2025-02-16 11:46:42 -08:00
|
|
|
fn log_ok(self) {
|
2025-08-21 22:26:36 -07:00
|
|
|
let caller = Some(Location::caller());
|
|
|
|
|
self.map_err(|e| e.do_log(LogLevel::Error, caller)).ok();
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
impl<T> ResultExt<T> for LoggedResult<T> {
|
|
|
|
|
fn log(self) -> LoggedResult<T> {
|
2024-03-05 01:48:35 -08:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
|
fn log_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
|
2025-08-23 04:17:01 -07:00
|
|
|
self.inspect_err(|_| do_log_msg(LogLevel::Error, None, f))
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
#[track_caller]
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
|
fn log_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
|
|
|
|
|
let caller = Some(Location::caller());
|
2025-08-23 04:17:01 -07:00
|
|
|
self.inspect_err(|_| do_log_msg(LogLevel::Error, caller, f))
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
fn log_ok(self) {}
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
// Allow converting Loggable errors to LoggedError to support `?` operator
|
|
|
|
|
impl<T: Loggable> From<T> for LoggedError {
|
2024-03-05 01:48:35 -08:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
|
fn from(e: T) -> Self {
|
2025-08-21 22:26:36 -07:00
|
|
|
e.do_log(LogLevel::Error, None)
|
2024-03-05 01:48:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
|
fn from(e: T) -> Self {
|
2025-08-21 22:26:36 -07:00
|
|
|
let caller = Some(Location::caller());
|
|
|
|
|
e.do_log(LogLevel::Error, caller)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Actual logging implementation
|
|
|
|
|
|
|
|
|
|
// Make all printable objects Loggable
|
|
|
|
|
impl<T: Display> Loggable for T {
|
|
|
|
|
fn do_log(self, level: LogLevel, caller: Option<&'static Location>) -> LoggedError {
|
|
|
|
|
if let Some(caller) = caller {
|
|
|
|
|
log_with_args!(level, "[{}:{}] {:#}", caller.file(), caller.line(), self);
|
|
|
|
|
} else {
|
|
|
|
|
log_with_args!(level, "{:#}", self);
|
|
|
|
|
}
|
|
|
|
|
LoggedError::default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn do_log_msg<F: FnOnce(Formatter) -> fmt::Result>(
|
|
|
|
|
self,
|
|
|
|
|
level: LogLevel,
|
|
|
|
|
caller: Option<&'static Location>,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> LoggedError {
|
|
|
|
|
log_with_formatter(level, |w| {
|
|
|
|
|
if let Some(caller) = caller {
|
|
|
|
|
write!(w, "[{}:{}] ", caller.file(), caller.line())?;
|
|
|
|
|
}
|
|
|
|
|
f(w)?;
|
|
|
|
|
writeln!(w, ": {self:#}")
|
|
|
|
|
});
|
2024-03-05 01:48:35 -08:00
|
|
|
LoggedError::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-14 18:05:14 -07:00
|
|
|
|
2025-08-21 22:26:36 -07:00
|
|
|
fn do_log_msg<F: FnOnce(Formatter) -> fmt::Result>(
|
|
|
|
|
level: LogLevel,
|
|
|
|
|
caller: Option<&'static Location>,
|
|
|
|
|
f: F,
|
|
|
|
|
) {
|
|
|
|
|
log_with_formatter(level, |w| {
|
|
|
|
|
if let Some(caller) = caller {
|
|
|
|
|
write!(w, "[{}:{}] ", caller.file(), caller.line())?;
|
|
|
|
|
}
|
|
|
|
|
f(w)?;
|
|
|
|
|
w.write_char('\n')
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 18:05:14 -07:00
|
|
|
// Check libc return value and map to Result
|
|
|
|
|
pub trait LibcReturn
|
|
|
|
|
where
|
2025-09-08 10:55:57 -07:00
|
|
|
Self: Sized,
|
2025-04-14 18:05:14 -07:00
|
|
|
{
|
2025-04-14 13:31:52 -07:00
|
|
|
type Value;
|
|
|
|
|
|
2025-09-08 23:59:29 -07:00
|
|
|
fn check_err(self) -> nix::Result<Self::Value>;
|
2025-04-14 18:05:14 -07:00
|
|
|
|
2025-09-08 10:55:57 -07:00
|
|
|
fn into_os_result<'a>(
|
2025-04-14 18:05:14 -07:00
|
|
|
self,
|
|
|
|
|
name: &'static str,
|
|
|
|
|
arg1: Option<&'a str>,
|
|
|
|
|
arg2: Option<&'a str>,
|
2025-04-14 13:31:52 -07:00
|
|
|
) -> OsResult<'a, Self::Value> {
|
2025-09-08 23:59:29 -07:00
|
|
|
self.check_err()
|
2025-09-08 10:55:57 -07:00
|
|
|
.map_err(|e| OsError::new(e, name, arg1, arg2))
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_os_err<'a>(
|
|
|
|
|
self,
|
|
|
|
|
name: &'static str,
|
|
|
|
|
arg1: Option<&'a str>,
|
|
|
|
|
arg2: Option<&'a str>,
|
|
|
|
|
) -> OsResult<'a, ()> {
|
2025-09-08 23:59:29 -07:00
|
|
|
self.check_err()
|
2025-09-08 10:55:57 -07:00
|
|
|
.map(|_| ())
|
|
|
|
|
.map_err(|e| OsError::new(e, name, arg1, arg2))
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_libc_return {
|
|
|
|
|
($($t:ty)*) => ($(
|
|
|
|
|
impl LibcReturn for $t {
|
2025-04-14 13:31:52 -07:00
|
|
|
type Value = Self;
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2025-09-08 23:59:29 -07:00
|
|
|
fn check_err(self) -> nix::Result<Self::Value> {
|
2025-09-08 10:55:57 -07:00
|
|
|
if self < 0 {
|
2025-09-08 23:59:29 -07:00
|
|
|
Err(Errno::last())
|
2025-09-08 10:55:57 -07:00
|
|
|
} else {
|
|
|
|
|
Ok(self)
|
|
|
|
|
}
|
2025-04-14 13:31:52 -07:00
|
|
|
}
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
)*)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl_libc_return! { i8 i16 i32 i64 isize }
|
|
|
|
|
|
2025-04-14 13:31:52 -07:00
|
|
|
impl<T> LibcReturn for *mut T {
|
|
|
|
|
type Value = NonNull<T>;
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2025-09-08 23:59:29 -07:00
|
|
|
fn check_err(self) -> nix::Result<Self::Value> {
|
|
|
|
|
NonNull::new(self).ok_or_else(Errno::last)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> LibcReturn for nix::Result<T> {
|
|
|
|
|
type Value = T;
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn check_err(self) -> Self {
|
|
|
|
|
self
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct OsError<'a> {
|
2025-09-08 23:59:29 -07:00
|
|
|
errno: Errno,
|
2025-04-14 18:05:14 -07:00
|
|
|
name: &'static str,
|
2025-09-08 11:24:33 -07:00
|
|
|
arg1: Option<&'a str>,
|
|
|
|
|
arg2: Option<&'a str>,
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl OsError<'_> {
|
2025-09-08 10:55:57 -07:00
|
|
|
pub fn new<'a>(
|
2025-09-08 23:59:29 -07:00
|
|
|
errno: Errno,
|
2025-04-14 18:05:14 -07:00
|
|
|
name: &'static str,
|
|
|
|
|
arg1: Option<&'a str>,
|
|
|
|
|
arg2: Option<&'a str>,
|
|
|
|
|
) -> OsError<'a> {
|
|
|
|
|
OsError {
|
2025-09-08 23:59:29 -07:00
|
|
|
errno,
|
2025-04-14 18:05:14 -07:00
|
|
|
name,
|
2025-09-08 11:24:33 -07:00
|
|
|
arg1,
|
|
|
|
|
arg2,
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn last_os_error<'a>(
|
|
|
|
|
name: &'static str,
|
|
|
|
|
arg1: Option<&'a str>,
|
|
|
|
|
arg2: Option<&'a str>,
|
|
|
|
|
) -> OsError<'a> {
|
2025-09-08 23:59:29 -07:00
|
|
|
Self::new(Errno::last(), name, arg1, arg2)
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_args<'a>(self, arg1: Option<&'a str>, arg2: Option<&'a str>) -> OsError<'a> {
|
2025-09-08 23:59:29 -07:00
|
|
|
Self::new(self.errno, self.name, arg1, arg2)
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for OsError<'_> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
if self.name.is_empty() {
|
2025-09-08 23:59:29 -07:00
|
|
|
write!(f, "{}", self.errno)
|
2025-04-14 18:05:14 -07:00
|
|
|
} else {
|
2025-09-08 11:24:33 -07:00
|
|
|
match (self.arg1, self.arg2) {
|
2025-04-14 18:05:14 -07:00
|
|
|
(Some(arg1), Some(arg2)) => {
|
2025-09-08 23:59:29 -07:00
|
|
|
write!(f, "{} '{arg1}' '{arg2}': {}", self.name, self.errno)
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
(Some(arg1), None) => {
|
2025-09-08 23:59:29 -07:00
|
|
|
write!(f, "{} '{arg1}': {}", self.name, self.errno)
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
_ => {
|
2025-09-08 23:59:29 -07:00
|
|
|
write!(f, "{}: {}", self.name, self.errno)
|
2025-04-14 18:05:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::error::Error for OsError<'_> {}
|
|
|
|
|
|
|
|
|
|
pub type OsResult<'a, T> = Result<T, OsError<'a>>;
|