log: new env vars to control logging behavior

* LOG_NO_COLOR
* LOG_CONSOLE_OUT (macOS only)
This commit is contained in:
eric committed 2021-04-23 07:34:13 +08:00
1 parent af3d588a59
commit 10e600d27d
8 files changed
+57 -55

No files matched your search

+1 -1
View File
@@ -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]
+1 -1
View File
@@ -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();
}
+6 -12
View File
@@ -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)
+37 -34
View File
@@ -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)?;
+1 -1
View File
@@ -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")))]
+2 -2
View File
@@ -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) {
+1 -4
View File
@@ -1,9 +1,6 @@
#if __APPLE__
#include <TargetConditionals.h>
#if TARGET_OS_IPHONE
#include <asl.h>
// #include <os/proc.h>
#endif
#include <asl.h>
#elif __ANDROID__
#include <android/log.h>
#endif
+8
View File
@@ -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)