From 10e600d27db13d0aada6917c3df9ad4afec5cf40 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 23 Apr 2021 07:34:13 +0800 Subject: [PATCH] log: new env vars to control logging behavior * LOG_NO_COLOR * LOG_CONSOLE_OUT (macOS only) --- leaf/Cargo.toml | 2 +- leaf/build.rs | 2 +- leaf/src/app/dispatcher.rs | 18 ++++------ leaf/src/app/logger.rs | 71 ++++++++++++++++++++------------------ leaf/src/lib.rs | 2 +- leaf/src/mobile/logger.rs | 4 +-- leaf/src/mobile/wrapper.h | 5 +-- leaf/src/option/mod.rs | 8 +++++ 8 files changed, 57 insertions(+), 55 deletions(-) diff --git a/leaf/Cargo.toml b/leaf/Cargo.toml index 70b9a94..a2c44ce 100644 --- a/leaf/Cargo.toml +++ b/leaf/Cargo.toml @@ -234,7 +234,7 @@ pnet_datalink = { version = "0.27", package = "pnet_datalink" } libc = "0.2" # Used in mobile logger -[target.'cfg(any(target_os = "ios", target_os = "android"))'.dependencies] +[target.'cfg(any(target_os = "ios", target_os = "macos", target_os = "android"))'.dependencies] memchr = { version = "2" } [dev-dependencies] diff --git a/leaf/build.rs b/leaf/build.rs index 821b43f..f72dbe1 100644 --- a/leaf/build.rs +++ b/leaf/build.rs @@ -154,7 +154,7 @@ fn main() { } let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if os == "ios" || os == "android" { + if os == "ios" || os == "macos" || os == "android" { generate_mobile_bindings(); } diff --git a/leaf/src/app/dispatcher.rs b/leaf/src/app/dispatcher.rs index f84e590..e26524b 100644 --- a/leaf/src/app/dispatcher.rs +++ b/leaf/src/app/dispatcher.rs @@ -201,13 +201,10 @@ impl Dispatcher { Ok(rhs) => { let elapsed = tokio::time::Instant::now().duration_since(handshake_start); - #[cfg(not(any(target_os = "ios", target_os = "android")))] - { - log_request(&sess, h.tag(), Some(h.color()), elapsed.as_millis()); - } - #[cfg(any(target_os = "ios", target_os = "android"))] - { + if *crate::option::LOG_NO_COLOR { log_request(&sess, h.tag(), None, elapsed.as_millis()); + } else { + log_request(&sess, h.tag(), Some(h.color()), elapsed.as_millis()); } let (lr, mut lw) = tokio::io::split(lhs); @@ -522,13 +519,10 @@ impl Dispatcher { Ok(c) => { let elapsed = tokio::time::Instant::now().duration_since(handshake_start); - #[cfg(not(any(target_os = "ios", target_os = "android")))] - { - log_request(&sess, h.tag(), Some(h.color()), elapsed.as_millis()); - } - #[cfg(any(target_os = "ios", target_os = "android"))] - { + if *crate::option::LOG_NO_COLOR { log_request(&sess, h.tag(), None, elapsed.as_millis()); + } else { + log_request(&sess, h.tag(), Some(h.color()), elapsed.as_millis()); } Ok(c) diff --git a/leaf/src/app/logger.rs b/leaf/src/app/logger.rs index f1bd732..ee66840 100644 --- a/leaf/src/app/logger.rs +++ b/leaf/src/app/logger.rs @@ -13,41 +13,36 @@ pub fn setup_logger(config: &config::Log) -> Result<()> { let mut dispatch = fern::Dispatch::new() .format(move |out, message, record| { - out.finish( - #[cfg(any(target_os = "ios", target_os = "android"))] - { - format_args!( - "[{date}][{level}] {message}", - date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), - level = record.level(), - message = message, - ) - }, - #[cfg(not(any(target_os = "ios", target_os = "android")))] - { - use fern::colors::{Color, ColoredLevelConfig}; - let colors_line = ColoredLevelConfig::new() - .error(Color::Red) - .warn(Color::Yellow) - .info(Color::White) - .debug(Color::White) - .trace(Color::BrightBlack); + if *crate::option::LOG_NO_COLOR { + out.finish(format_args!( + "[{date}][{level}] {message}", + date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), + level = record.level(), + message = message, + )) + } else { + use fern::colors::{Color, ColoredLevelConfig}; + let colors_line = ColoredLevelConfig::new() + .error(Color::Red) + .warn(Color::Yellow) + .info(Color::White) + .debug(Color::White) + .trace(Color::BrightBlack); - let colors_level = colors_line.clone().info(Color::Green); - format_args!( - // "{color_line}[{date}][{level}{color_line}][{target}] {message}\x1B[0m", - "{color_line}[{date}][{level}{color_line}] {message}\x1B[0m", - color_line = format_args!( - "\x1B[{}m", - colors_line.get_color(&record.level()).to_fg_str() - ), - date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), - // target = record.target(), - level = colors_level.color(record.level()), - message = message, - ) - }, - ) + let colors_level = colors_line.clone().info(Color::Green); + out.finish(format_args!( + // "{color_line}[{date}][{level}{color_line}][{target}] {message}\x1B[0m", + "{color_line}[{date}][{level}{color_line}] {message}\x1B[0m", + color_line = format_args!( + "\x1B[{}m", + colors_line.get_color(&record.level()).to_fg_str() + ), + date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), + // target = record.target(), + level = colors_level.color(record.level()), + message = message, + )) + } }) .level(log::LevelFilter::Warn) .level_for("leaf", loglevel); @@ -66,6 +61,14 @@ pub fn setup_logger(config: &config::Log) -> Result<()> { { dispatch = dispatch.chain(fern::Output::stdout("\n")); } + #[cfg(target_os = "macos")] + if *crate::option::LOG_CONSOLE_OUT { + let console_output = fern::Output::writer( + Box::new(crate::mobile::logger::ConsoleWriter::default()), + "\n", + ); + dispatch = dispatch.chain(console_output); + } } config::Log_Output::FILE => { let f = fern::log_file(&config.output_file)?; diff --git a/leaf/src/lib.rs b/leaf/src/lib.rs index 8e10e6d..dc55514 100644 --- a/leaf/src/lib.rs +++ b/leaf/src/lib.rs @@ -33,7 +33,7 @@ pub mod proxy; pub mod session; pub mod util; -#[cfg(any(target_os = "ios", target_os = "android"))] +#[cfg(any(target_os = "ios", target_os = "macos", target_os = "android"))] pub mod mobile; #[cfg(all(feature = "inbound-tun", any(target_os = "macos", target_os = "linux")))] diff --git a/leaf/src/mobile/logger.rs b/leaf/src/mobile/logger.rs index 30e0abb..40f9b60 100644 --- a/leaf/src/mobile/logger.rs +++ b/leaf/src/mobile/logger.rs @@ -5,13 +5,13 @@ use std::{ use bytes::BytesMut; -#[cfg(target_os = "ios")] +#[cfg(any(target_os = "ios", target_os = "macos"))] use super::bindings::{asl_log, ASL_LEVEL_NOTICE}; #[cfg(target_os = "android")] use super::bindings::{__android_log_print, android_LogPriority_ANDROID_LOG_VERBOSE}; -#[cfg(target_os = "ios")] +#[cfg(any(target_os = "ios", target_os = "macos"))] fn log_out(data: &[u8]) { unsafe { let s = match ffi::CString::new(data) { diff --git a/leaf/src/mobile/wrapper.h b/leaf/src/mobile/wrapper.h index c50022f..0757744 100644 --- a/leaf/src/mobile/wrapper.h +++ b/leaf/src/mobile/wrapper.h @@ -1,9 +1,6 @@ #if __APPLE__ #include - #if TARGET_OS_IPHONE - #include - // #include - #endif + #include #elif __ANDROID__ #include #endif diff --git a/leaf/src/option/mod.rs b/leaf/src/option/mod.rs index 70fc683..a472745 100644 --- a/leaf/src/option/mod.rs +++ b/leaf/src/option/mod.rs @@ -55,6 +55,14 @@ lazy_static! { } lazy_static! { + pub static ref LOG_CONSOLE_OUT: bool = { + get_env_var_or("LOG_CONSOLE_OUT", false) + }; + + pub static ref LOG_NO_COLOR: bool = { + get_env_var_or("LOG_NO_COLOR", false) + }; + /// Uplink timeout after downlink EOF. pub static ref TCP_UPLINK_TIMEOUT: u64 = { get_env_var_or("TCP_UPLINK_TIMEOUT", 2)