mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-11-23 20:19:45 +00:00
Simplify logging code
This commit is contained in:
parent
f12951bd1d
commit
c30fbdf145
@ -3,6 +3,7 @@ use std::io::{stderr, stdout, Write};
|
||||
use std::process::exit;
|
||||
|
||||
use crate::ffi::LogLevel;
|
||||
use crate::fmt_to_buf;
|
||||
|
||||
// Ugly hack to avoid using enum
|
||||
#[allow(non_snake_case, non_upper_case_globals)]
|
||||
@ -17,14 +18,12 @@ mod LogFlag {
|
||||
// We don't need to care about thread safety, because all
|
||||
// logger changes will only happen on the main thread.
|
||||
pub static mut LOGGER: Logger = Logger {
|
||||
fmt: |_, _| {},
|
||||
write: |_, _| {},
|
||||
flags: 0,
|
||||
};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Logger {
|
||||
pub fmt: fn(level: LogLevel, args: Arguments),
|
||||
pub write: fn(level: LogLevel, msg: &[u8]),
|
||||
pub flags: u32,
|
||||
}
|
||||
@ -78,20 +77,15 @@ pub fn log_impl(level: LogLevel, args: Arguments) {
|
||||
if (logger.flags & level.to_disable_flag()) != 0 {
|
||||
return;
|
||||
}
|
||||
(logger.fmt)(level, args);
|
||||
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 cmdline_logging() {
|
||||
fn cmdline_print(level: LogLevel, args: Arguments) {
|
||||
if level == LogLevel::Info {
|
||||
print!("{}", args);
|
||||
} else {
|
||||
eprint!("{}", args);
|
||||
}
|
||||
}
|
||||
fn cmdline_write(level: LogLevel, msg: &[u8]) {
|
||||
if level == LogLevel::Info {
|
||||
stdout().write_all(msg).ok();
|
||||
@ -101,7 +95,6 @@ pub fn cmdline_logging() {
|
||||
}
|
||||
|
||||
let logger = Logger {
|
||||
fmt: cmdline_print,
|
||||
write: cmdline_write,
|
||||
flags: LogFlag::ExitOnError,
|
||||
};
|
||||
|
@ -1,6 +1,5 @@
|
||||
use std::cmp::min;
|
||||
use std::ffi::{c_char, c_void};
|
||||
use std::fmt::Arguments;
|
||||
use std::fs::File;
|
||||
use std::io::{IoSlice, Read, Write};
|
||||
use std::os::fd::{AsRawFd, FromRawFd, RawFd};
|
||||
@ -58,13 +57,6 @@ fn level_to_prio(level: LogLevel) -> i32 {
|
||||
}
|
||||
|
||||
pub fn android_logging() {
|
||||
fn android_log_fmt(level: LogLevel, args: Arguments) {
|
||||
let mut buf: [u8; 4096] = [0; 4096];
|
||||
fmt_to_buf(&mut buf, args);
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), buf.as_ptr());
|
||||
}
|
||||
}
|
||||
fn android_log_write(level: LogLevel, msg: &[u8]) {
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
|
||||
@ -72,7 +64,6 @@ pub fn android_logging() {
|
||||
}
|
||||
|
||||
let logger = Logger {
|
||||
fmt: android_log_fmt,
|
||||
write: android_log_write,
|
||||
flags: 0,
|
||||
};
|
||||
@ -83,14 +74,6 @@ pub fn android_logging() {
|
||||
}
|
||||
|
||||
pub fn magisk_logging() {
|
||||
fn magisk_fmt(level: LogLevel, args: Arguments) {
|
||||
let mut buf: [u8; 4096] = [0; 4096];
|
||||
let len = fmt_to_buf(&mut buf, args);
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), buf.as_ptr());
|
||||
}
|
||||
magisk_log_write(level_to_prio(level), &buf[..len]);
|
||||
}
|
||||
fn magisk_write(level: LogLevel, msg: &[u8]) {
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
|
||||
@ -99,7 +82,6 @@ pub fn magisk_logging() {
|
||||
}
|
||||
|
||||
let logger = Logger {
|
||||
fmt: magisk_fmt,
|
||||
write: magisk_write,
|
||||
flags: 0,
|
||||
};
|
||||
@ -110,14 +92,6 @@ pub fn magisk_logging() {
|
||||
}
|
||||
|
||||
pub fn zygisk_logging() {
|
||||
fn zygisk_fmt(level: LogLevel, args: Arguments) {
|
||||
let mut buf: [u8; 4096] = [0; 4096];
|
||||
let len = fmt_to_buf(&mut buf, args);
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), buf.as_ptr());
|
||||
}
|
||||
zygisk_log_write(level_to_prio(level), &buf[..len]);
|
||||
}
|
||||
fn zygisk_write(level: LogLevel, msg: &[u8]) {
|
||||
unsafe {
|
||||
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
|
||||
@ -126,7 +100,6 @@ pub fn zygisk_logging() {
|
||||
}
|
||||
|
||||
let logger = Logger {
|
||||
fmt: zygisk_fmt,
|
||||
write: zygisk_write,
|
||||
flags: 0,
|
||||
};
|
||||
|
@ -1,4 +1,3 @@
|
||||
use std::fmt::Arguments;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
@ -50,14 +49,6 @@ pub fn setup_klog() {
|
||||
writeln!(rate, "on").ok();
|
||||
}
|
||||
|
||||
fn klog_fmt(_: LogLevel, args: Arguments) {
|
||||
if let Some(kmsg) = KMSG.get().as_mut() {
|
||||
let mut buf: [u8; 4096] = [0; 4096];
|
||||
let len = fmt_to_buf(&mut buf, format_args!("magiskinit: {}", args));
|
||||
kmsg.write_all(&buf[..len]).ok();
|
||||
}
|
||||
}
|
||||
|
||||
fn klog_write_impl(_: LogLevel, msg: &[u8]) {
|
||||
if let Some(kmsg) = KMSG.get().as_mut() {
|
||||
let mut buf: [u8; 4096] = [0; 4096];
|
||||
@ -68,7 +59,6 @@ pub fn setup_klog() {
|
||||
}
|
||||
|
||||
let logger = Logger {
|
||||
fmt: klog_fmt,
|
||||
write: klog_write_impl,
|
||||
flags: 0,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user