mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-06-12 13:17:39 +02:00
Move all logging into Rust
This commit is contained in:
@ -1,6 +1,10 @@
|
||||
#![feature(format_args_nl)]
|
||||
|
||||
pub use logging::*;
|
||||
pub use misc::*;
|
||||
|
||||
mod logging;
|
||||
mod misc;
|
||||
|
||||
#[cxx::bridge]
|
||||
pub mod ffi {
|
||||
@ -16,7 +20,7 @@ pub mod ffi {
|
||||
}
|
||||
}
|
||||
|
||||
#[cxx::bridge(namespace = "rs::logging")]
|
||||
#[cxx::bridge(namespace = "rust")]
|
||||
pub mod ffi2 {
|
||||
extern "Rust" {
|
||||
fn cmdline_logging();
|
||||
|
@ -23,10 +23,6 @@ pub struct Logger {
|
||||
|
||||
pub fn nop_log(_: Arguments) {}
|
||||
|
||||
fn println(args: Arguments) { println!("{}", args); }
|
||||
|
||||
fn eprintln(args: Arguments) { eprintln!("{}", args); }
|
||||
|
||||
pub fn log_with_rs(level: LogLevel, msg: &str) {
|
||||
log_impl(level, format_args!("{}", msg));
|
||||
}
|
||||
@ -36,11 +32,14 @@ pub fn exit_on_error(b: bool) {
|
||||
}
|
||||
|
||||
pub fn cmdline_logging() {
|
||||
fn print(args: Arguments) { print!("{}", args); }
|
||||
fn eprint(args: Arguments) { eprint!("{}", args); }
|
||||
|
||||
let logger = Logger {
|
||||
d: eprintln,
|
||||
i: println,
|
||||
w: eprintln,
|
||||
e: eprintln,
|
||||
d: eprint,
|
||||
i: print,
|
||||
w: eprint,
|
||||
e: eprint,
|
||||
};
|
||||
unsafe {
|
||||
LOGGER = logger;
|
||||
@ -65,23 +64,23 @@ pub fn log_impl(level: LogLevel, args: Arguments) {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! error {
|
||||
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Error, format_args!($($arg)+)))
|
||||
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Error, format_args_nl!($($arg)+)))
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! warn {
|
||||
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Warn, format_args!($($arg)+)))
|
||||
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Warn, format_args_nl!($($arg)+)))
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! info {
|
||||
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Info, format_args!($($arg)+)))
|
||||
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Info, format_args_nl!($($arg)+)))
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[macro_export]
|
||||
macro_rules! debug {
|
||||
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Debug, format_args!($($arg)+)))
|
||||
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Debug, format_args_nl!($($arg)+)))
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
|
41
native/rust/base/src/misc.rs
Normal file
41
native/rust/base/src/misc.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use std::cmp::min;
|
||||
use std::fmt;
|
||||
use std::fmt::Arguments;
|
||||
|
||||
struct BufFmtWriter<'a> {
|
||||
buf: &'a mut [u8],
|
||||
used: usize,
|
||||
}
|
||||
|
||||
impl<'a> BufFmtWriter<'a> {
|
||||
fn new(buf: &'a mut [u8]) -> Self {
|
||||
BufFmtWriter { buf, used: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Write for BufFmtWriter<'a> {
|
||||
// The buffer should always be null terminated
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
if self.used >= self.buf.len() - 1 {
|
||||
// Silent truncate
|
||||
return Ok(());
|
||||
}
|
||||
let remain = &mut self.buf[self.used..];
|
||||
let s_bytes = s.as_bytes();
|
||||
let copied = min(s_bytes.len(), remain.len() - 1);
|
||||
remain[..copied].copy_from_slice(&s_bytes[..copied]);
|
||||
self.used += copied;
|
||||
self.buf[self.used] = b'\0';
|
||||
// Silent truncate
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fmt_to_buf(buf: &mut [u8], args: Arguments) -> usize {
|
||||
let mut w = BufFmtWriter::new(buf);
|
||||
if let Ok(()) = fmt::write(&mut w, args) {
|
||||
w.used
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user