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,23 @@
use std::fmt::{Arguments, Display};
use std::fmt;
use std::fmt::{Arguments, Display, Write as fWrite};
use std::io::{stderr, stdout, Write};
use std::process::exit;
use crate::ffi::LogLevel;
use crate::fmt_to_buf;
use crate::BufFormatter;
// 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.
// Implement `From<ErrorType> for LoggedError` for non-standard error types so that we can
// directly use the `?` operator to propagate LoggedResult.
//
// To log an error with more information, use `ResultExt::log_with_msg()`.
//
// The "cxx" method variants in `ResultExt` 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)]
@@ -41,7 +55,7 @@ pub fn exit_on_error(b: bool) {
impl LogLevel {
fn as_disable_flag(&self) -> u32 {
match *self {
LogLevel::Error => LogFlag::DisableError,
LogLevel::Error | LogLevel::ErrorCxx => LogFlag::DisableError,
LogLevel::Warn => LogFlag::DisableWarn,
LogLevel::Info => LogFlag::DisableInfo,
LogLevel::Debug => LogFlag::DisableDebug,
@@ -61,28 +75,32 @@ pub fn set_log_level_state(level: LogLevel, enabled: bool) {
}
}
pub fn log_with_rs(level: LogLevel, msg: &[u8]) {
fn do_log<F: FnOnce(fn(level: LogLevel, msg: &[u8]))>(level: LogLevel, f: F) {
let logger = unsafe { LOGGER };
if (logger.flags & level.as_disable_flag()) != 0 {
return;
}
(logger.write)(level, msg);
if level == LogLevel::Error && (logger.flags & LogFlag::ExitOnError) != 0 {
f(logger.write);
if level == LogLevel::ErrorCxx && (logger.flags & LogFlag::ExitOnError) != 0 {
exit(1);
}
}
pub fn log_impl(level: LogLevel, args: Arguments) {
let logger = unsafe { LOGGER };
if (logger.flags & level.as_disable_flag()) != 0 {
return;
}
let mut buf: [u8; 4096] = [0; 4096];
let len = fmt_to_buf(&mut buf, args);
(logger.write)(level, &buf[..len]);
if level == LogLevel::Error && (logger.flags & LogFlag::ExitOnError) != 0 {
exit(1);
}
pub fn log_from_cxx(level: LogLevel, msg: &[u8]) {
do_log(level, |write| write(level, msg));
}
pub fn log_with_formatter<F: FnOnce(&mut BufFormatter) -> fmt::Result>(level: LogLevel, f: F) {
do_log(level, |write| {
let mut buf = [0_u8; 4096];
let mut w = BufFormatter::new(&mut buf);
let len = if f(&mut w).is_ok() { w.used } else { 0 };
write(level, &buf[..len]);
});
}
pub fn log_with_args(level: LogLevel, args: Arguments) {
log_with_formatter(level, |w| w.write_fmt(args));
}
pub fn cmdline_logging() {
@@ -103,44 +121,33 @@ pub fn cmdline_logging() {
}
}
#[macro_export]
macro_rules! perror {
($fmt:expr) => {
$crate::log_impl($crate::ffi::LogLevel::Error, format_args_nl!(
concat!($fmt, " failed with {}: {}"),
$crate::errno(),
$crate::error_str()
))
};
($fmt:expr, $($args:tt)*) => {
$crate::log_impl($crate::ffi::LogLevel::Error, format_args_nl!(
concat!($fmt, " failed with {}: {}"),
$($args)*,
$crate::errno(),
$crate::error_str()
))
};
}
#[macro_export]
macro_rules! error {
($($args:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Error, format_args_nl!($($args)+)))
($($args:tt)+) => {
$crate::log_with_args($crate::ffi::LogLevel::Error, format_args_nl!($($args)+))
}
}
#[macro_export]
macro_rules! warn {
($($args:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Warn, format_args_nl!($($args)+)))
($($args:tt)+) => {
$crate::log_with_args($crate::ffi::LogLevel::Warn, format_args_nl!($($args)+))
}
}
#[macro_export]
macro_rules! info {
($($args:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Info, format_args_nl!($($args)+)))
($($args:tt)+) => {
$crate::log_with_args($crate::ffi::LogLevel::Info, format_args_nl!($($args)+))
}
}
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug {
($($args:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Debug, format_args_nl!($($args)+)))
($($args:tt)+) => {
$crate::log_with_args($crate::ffi::LogLevel::Debug, format_args_nl!($($args)+))
}
}
#[cfg(not(debug_assertions))]
@@ -149,15 +156,111 @@ macro_rules! debug {
($($args:tt)+) => {};
}
pub trait ResultExt {
fn log(self) -> Self;
}
#[derive(Default)]
pub struct LoggedError {}
impl<T, E: Display> ResultExt for Result<T, E> {
fn log(self) -> Self {
if let Err(e) = &self {
error!("{:#}", e);
}
self
// Automatically handle all printable errors
impl<T: Display> From<T> for LoggedError {
fn from(e: T) -> Self {
log_with_args(LogLevel::Error, format_args_nl!("{:#}", e));
LoggedError::default()
}
}
pub type LoggedResult<T> = Result<T, LoggedError>;
#[macro_export]
macro_rules! log_err {
($msg:literal $(,)?) => {{
$crate::log_with_args($crate::ffi::LogLevel::Error, format_args_nl!($msg));
$crate::LoggedError::default()
}};
($err:expr $(,)?) => {{
$crate::log_with_args($crate::ffi::LogLevel::Error, format_args_nl!("{}", $err));
$crate::LoggedError::default()
}};
($($args:tt)+) => {{
$crate::log_with_args($crate::ffi::LogLevel::Error, format_args_nl!($($args)+));
$crate::LoggedError::default()
}};
}
pub trait ResultExt<T>
where
Self: Sized,
{
fn log(self) -> LoggedResult<T> {
self.log_impl(LogLevel::Error)
}
fn log_cxx(self) -> LoggedResult<T> {
self.log_impl(LogLevel::ErrorCxx)
}
fn log_with_msg<F: FnOnce(&mut BufFormatter) -> fmt::Result>(self, f: F) -> LoggedResult<T> {
self.log_with_msg_impl(LogLevel::Error, f)
}
fn log_cxx_with_msg<F: FnOnce(&mut BufFormatter) -> fmt::Result>(
self,
f: F,
) -> LoggedResult<T> {
self.log_with_msg_impl(LogLevel::ErrorCxx, f)
}
fn log_impl(self, level: LogLevel) -> LoggedResult<T>;
fn log_with_msg_impl<F: FnOnce(&mut BufFormatter) -> fmt::Result>(
self,
level: LogLevel,
f: F,
) -> LoggedResult<T>;
}
impl<T> ResultExt<T> for LoggedResult<T> {
fn log_impl(self, _: LogLevel) -> LoggedResult<T> {
self
}
fn log_with_msg_impl<F: FnOnce(&mut BufFormatter) -> fmt::Result>(
self,
level: LogLevel,
f: F,
) -> LoggedResult<T> {
match self {
Ok(v) => Ok(v),
Err(_) => {
log_with_formatter(level, |w| {
f(w)?;
w.write_char('\n')
});
Err(LoggedError::default())
}
}
}
}
impl<T, E: Display> ResultExt<T> for Result<T, E> {
fn log_impl(self, level: LogLevel) -> LoggedResult<T> {
match self {
Ok(v) => Ok(v),
Err(e) => {
log_with_args(level, format_args_nl!("{:#}", e));
Err(LoggedError::default())
}
}
}
fn log_with_msg_impl<F: FnOnce(&mut BufFormatter) -> fmt::Result>(
self,
level: LogLevel,
f: F,
) -> LoggedResult<T> {
match self {
Ok(v) => Ok(v),
Err(e) => {
log_with_formatter(level, |w| {
f(w)?;
writeln!(w, ": {:#}", e)
});
Err(LoggedError::default())
}
}
}
}