mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-12 05:32:27 +00:00
Reorganize some code
This commit is contained in:
@@ -1,27 +1,14 @@
|
||||
use std::fmt;
|
||||
use std::fmt::Arguments;
|
||||
use std::io::{stderr, stdout, Write};
|
||||
use std::process::exit;
|
||||
|
||||
use num_derive::{FromPrimitive, ToPrimitive};
|
||||
use num_traits::FromPrimitive;
|
||||
use std::fmt;
|
||||
use std::fmt::{Arguments, Display};
|
||||
use std::io::{stderr, stdout, Write};
|
||||
use std::panic::Location;
|
||||
use std::process::exit;
|
||||
|
||||
use crate::ffi::LogLevelCxx;
|
||||
use crate::{Utf8CStr, Utf8CStrBufArr};
|
||||
|
||||
// Error handling and logging 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()`.
|
||||
//
|
||||
// The "cxx" method variants in `CxxResultExt` are only used for C++ interop and
|
||||
// should not be used directly in any Rust code.
|
||||
//
|
||||
// For general logging, use the <level>!(...) macros.
|
||||
|
||||
// Ugly hack to avoid using enum
|
||||
#[allow(non_snake_case, non_upper_case_globals)]
|
||||
mod LogFlag {
|
||||
@@ -50,7 +37,7 @@ pub static mut LOGGER: Logger = Logger {
|
||||
};
|
||||
|
||||
type LogWriter = fn(level: LogLevel, msg: &Utf8CStr);
|
||||
type Formatter<'a> = &'a mut dyn fmt::Write;
|
||||
pub(crate) type Formatter<'a> = &'a mut dyn fmt::Write;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Logger {
|
||||
@@ -171,188 +158,3 @@ macro_rules! debug {
|
||||
macro_rules! debug {
|
||||
($($args:tt)+) => {};
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct LoggedError {}
|
||||
|
||||
// Automatically handle all printable errors
|
||||
impl<T: Display> From<T> for LoggedError {
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn from(e: T) -> Self {
|
||||
log_with_args(LogLevel::Error, format_args_nl!("{:#}", e));
|
||||
LoggedError::default()
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
#[cfg(debug_assertions)]
|
||||
fn from(e: T) -> Self {
|
||||
let caller = Location::caller();
|
||||
log_with_args(
|
||||
LogLevel::Error,
|
||||
format_args_nl!("[{}:{}] {:#}", caller.file(), caller.line(), e),
|
||||
);
|
||||
LoggedError::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub type LoggedResult<T> = Result<T, LoggedError>;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_err {
|
||||
($msg:literal $(,)?) => {{
|
||||
$crate::log_with_args($crate::LogLevel::Error, format_args_nl!($msg));
|
||||
$crate::LoggedError::default()
|
||||
}};
|
||||
($err:expr $(,)?) => {{
|
||||
$crate::log_with_args($crate::LogLevel::Error, format_args_nl!("{}", $err));
|
||||
$crate::LoggedError::default()
|
||||
}};
|
||||
($($args:tt)+) => {{
|
||||
$crate::log_with_args($crate::LogLevel::Error, format_args_nl!($($args)+));
|
||||
$crate::LoggedError::default()
|
||||
}};
|
||||
}
|
||||
|
||||
pub trait ResultExt<T> {
|
||||
fn log(self) -> LoggedResult<T>;
|
||||
fn log_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T>;
|
||||
}
|
||||
|
||||
pub trait ResultNoLog<T> {
|
||||
fn no_log(self) -> LoggedResult<T>;
|
||||
}
|
||||
|
||||
// Internal C++ bridging logging routines
|
||||
pub(crate) trait CxxResultExt<T> {
|
||||
fn log_cxx(self) -> LoggedResult<T>;
|
||||
fn log_cxx_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T>;
|
||||
}
|
||||
|
||||
trait LogImpl<T> {
|
||||
fn log_impl(self, level: LogLevel, caller: Option<&'static Location>) -> LoggedResult<T>;
|
||||
fn log_with_msg_impl<F: FnOnce(Formatter) -> fmt::Result>(
|
||||
self,
|
||||
level: LogLevel,
|
||||
caller: Option<&'static Location>,
|
||||
f: F,
|
||||
) -> LoggedResult<T>;
|
||||
}
|
||||
|
||||
impl<T, E> ResultNoLog<T> for Result<T, E> {
|
||||
fn no_log(self) -> LoggedResult<T> {
|
||||
match self {
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => Err(LoggedError::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ResultNoLog<T> for Option<T> {
|
||||
fn no_log(self) -> LoggedResult<T> {
|
||||
match self {
|
||||
Some(v) => Ok(v),
|
||||
None => Err(LoggedError::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, R: LogImpl<T>> CxxResultExt<T> for R {
|
||||
fn log_cxx(self) -> LoggedResult<T> {
|
||||
self.log_impl(LogLevel::ErrorCxx, None)
|
||||
}
|
||||
|
||||
fn log_cxx_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
|
||||
self.log_with_msg_impl(LogLevel::ErrorCxx, None, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, R: LogImpl<T>> ResultExt<T> for R {
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn log(self) -> LoggedResult<T> {
|
||||
self.log_impl(LogLevel::Error, None)
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
#[cfg(debug_assertions)]
|
||||
fn log(self) -> LoggedResult<T> {
|
||||
self.log_impl(LogLevel::Error, Some(Location::caller()))
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn log_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
|
||||
self.log_with_msg_impl(LogLevel::Error, None, f)
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
#[cfg(debug_assertions)]
|
||||
fn log_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
|
||||
self.log_with_msg_impl(LogLevel::Error, Some(Location::caller()), f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> LogImpl<T> for LoggedResult<T> {
|
||||
fn log_impl(self, _: LogLevel, _: Option<&'static Location>) -> LoggedResult<T> {
|
||||
self
|
||||
}
|
||||
|
||||
fn log_with_msg_impl<F: FnOnce(Formatter) -> fmt::Result>(
|
||||
self,
|
||||
level: LogLevel,
|
||||
caller: Option<&'static Location>,
|
||||
f: F,
|
||||
) -> LoggedResult<T> {
|
||||
match self {
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => {
|
||||
log_with_formatter(level, |w| {
|
||||
if let Some(caller) = caller {
|
||||
write!(w, "[{}:{}] ", caller.file(), caller.line())?;
|
||||
}
|
||||
f(w)?;
|
||||
w.write_char('\n')
|
||||
});
|
||||
Err(LoggedError::default())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: Display> LogImpl<T> for Result<T, E> {
|
||||
fn log_impl(self, level: LogLevel, caller: Option<&'static Location>) -> LoggedResult<T> {
|
||||
match self {
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => {
|
||||
if let Some(caller) = caller {
|
||||
log_with_args(
|
||||
level,
|
||||
format_args_nl!("[{}:{}] {:#}", caller.file(), caller.line(), e),
|
||||
);
|
||||
} else {
|
||||
log_with_args(level, format_args_nl!("{:#}", e));
|
||||
}
|
||||
Err(LoggedError::default())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn log_with_msg_impl<F: FnOnce(Formatter) -> fmt::Result>(
|
||||
self,
|
||||
level: LogLevel,
|
||||
caller: Option<&'static Location>,
|
||||
f: F,
|
||||
) -> LoggedResult<T> {
|
||||
match self {
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => {
|
||||
log_with_formatter(level, |w| {
|
||||
if let Some(caller) = caller {
|
||||
write!(w, "[{}:{}] ", caller.file(), caller.line())?;
|
||||
}
|
||||
f(w)?;
|
||||
writeln!(w, ": {:#}", e)
|
||||
});
|
||||
Err(LoggedError::default())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user