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

@@ -26,18 +26,18 @@ pub fn copy_cstr<T: AsRef<CStr> + ?Sized>(dest: &mut [u8], src: &T) -> usize {
len - 1
}
struct BufFmtWriter<'a> {
pub struct BufFormatter<'a> {
buf: &'a mut [u8],
used: usize,
pub used: usize,
}
impl<'a> BufFmtWriter<'a> {
fn new(buf: &'a mut [u8]) -> Self {
BufFmtWriter { buf, used: 0 }
impl<'a> BufFormatter<'a> {
pub fn new(buf: &'a mut [u8]) -> Self {
BufFormatter { buf, used: 0 }
}
}
impl<'a> fmt::Write for BufFmtWriter<'a> {
impl<'a> fmt::Write for BufFormatter<'a> {
// The buffer should always be null terminated
fn write_str(&mut self, s: &str) -> fmt::Result {
if self.used >= self.buf.len() - 1 {
@@ -51,7 +51,7 @@ impl<'a> fmt::Write for BufFmtWriter<'a> {
}
pub fn fmt_to_buf(buf: &mut [u8], args: Arguments) -> usize {
let mut w = BufFmtWriter::new(buf);
let mut w = BufFormatter::new(buf);
if let Ok(()) = fmt::write(&mut w, args) {
w.used
} else {