Implement logging purely in Rust

This commit is contained in:
topjohnwu
2023-05-09 18:54:38 -07:00
parent 8c2ad3883a
commit 7518092ad2
17 changed files with 433 additions and 228 deletions

View File

@@ -0,0 +1,9 @@
// We expose constant values as macros so that all constants are literals
// An advantage for doing this is that we can use concat!() on these values
#[macro_export]
macro_rules! LOGFILE {
() => {
"/cache/magisk.log"
};
}

View File

@@ -2,11 +2,13 @@
pub use libc;
pub use consts::*;
pub use files::*;
pub use logging::*;
pub use misc::*;
pub use xwrap::*;
mod consts;
mod files;
mod logging;
mod misc;

View File

@@ -130,7 +130,7 @@ uint64_t parse_uint64_hex(std::string_view s);
int parse_int(std::string_view s);
using thread_entry = void *(*)(void *);
int new_daemon_thread(thread_entry entry, void *arg = nullptr);
extern "C" int new_daemon_thread(thread_entry entry, void *arg = nullptr);
static inline bool str_contains(std::string_view s, std::string_view ss) {
return s.find(ss) != std::string::npos;

View File

@@ -151,3 +151,24 @@ pub unsafe fn slice_from_ptr_mut<'a, T>(buf: *mut T, len: usize) -> &'a mut [T]
slice::from_raw_parts_mut(buf, len)
}
}
pub trait FlatData {
fn as_raw_bytes(&self) -> &[u8]
where
Self: Sized,
{
unsafe {
let self_ptr = self as *const Self as *const u8;
slice::from_raw_parts(self_ptr, std::mem::size_of::<Self>())
}
}
fn as_raw_bytes_mut(&mut self) -> &mut [u8]
where
Self: Sized,
{
unsafe {
let self_ptr = self as *mut Self as *mut u8;
slice::from_raw_parts_mut(self_ptr, std::mem::size_of::<Self>())
}
}
}