diff --git a/crates/diagnostics/src/local_tracing.rs b/crates/diagnostics/src/local_tracing.rs index 3da11835b..f2848ffaa 100644 --- a/crates/diagnostics/src/local_tracing.rs +++ b/crates/diagnostics/src/local_tracing.rs @@ -6,43 +6,34 @@ use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter, Layer}; static IS_TRACING_ENABLED: AtomicBool = AtomicBool::new(false); pub fn enable_tracing_by_env() { - let trace_var = std::env::var("TRACE").ok(); - let is_enable_tracing = trace_var.is_some(); + let Ok(trace_var) = std::env::var("TRACE") else { + return + }; - if is_enable_tracing && !IS_TRACING_ENABLED.swap(true, std::sync::atomic::Ordering::SeqCst) { + if !IS_TRACING_ENABLED.swap(true, std::sync::atomic::Ordering::SeqCst) { use tracing_subscriber::{fmt, prelude::*}; - let layers = generate_common_layers(trace_var); + let layer = common_layer(&trace_var); tracing_subscriber::registry() - .with(layers) + .with(layer) .with(fmt::layer().pretty().with_file(true).with_span_events(FmtSpan::CLOSE)) .init(); tracing::trace!("enable_tracing_by_env"); } } -fn generate_common_layers( - trace_var: Option, -) -> Vec + Send + Sync>> { - let default_level = trace_var.as_ref().and_then(|var| Level::from_str(var).ok()); - - let mut layers = vec![]; - if let Some(default_level) = default_level { - layers.push( - tracing_subscriber::filter::Targets::new() - .with_targets(vec![("pacquet_tarball", default_level)]) - .boxed(), - ); +fn common_layer(trace_var: &str) -> Box + Send + Sync> { + if let Ok(default_level) = Level::from_str(trace_var) { + tracing_subscriber::filter::Targets::new() + .with_target("pacquet_tarball", default_level) + .boxed() } else { - // SAFETY: we know that trace_var is `Ok(String)` now, - // for the second unwrap, if we can't parse the directive, then the tracing result would be + // SAFETY: for the `expect`, if we can't parse the directive, then the tracing result would be // unexpected, then panic is reasonable - let env_layer = EnvFilter::builder() + EnvFilter::builder() .with_regex(true) - .parse(trace_var.expect("Should not be empty")) - .expect("Parse tracing directive syntax failed,for details about the directive syntax you could refer https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives"); - - layers.push(env_layer.boxed()); + .parse(trace_var) + .expect("Parse tracing directive syntax failed,for details about the directive syntax you could refer https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives") + .boxed() } - layers } diff --git a/crates/package_json/src/lib.rs b/crates/package_json/src/lib.rs index 3f3004f8f..f83f8c591 100644 --- a/crates/package_json/src/lib.rs +++ b/crates/package_json/src/lib.rs @@ -1,7 +1,6 @@ use std::{ collections::HashMap, convert::Into, - ffi::OsStr, fs, io::{Read, Write}, path::PathBuf, @@ -77,11 +76,11 @@ impl PackageJson { fn write_to_file(path: &PathBuf) -> Result { let mut file = fs::File::create(path)?; - let mut name = ""; - if let Some(folder) = path.parent() { - // Set the default package name as the folder of the current directory - name = folder.file_name().unwrap_or(OsStr::new("")).to_str().unwrap(); - } + let name = path + .parent() + .and_then(|folder| folder.file_name()) + .and_then(|file_name| file_name.to_str()) + .unwrap_or(""); let package_json = json!({ "name": name, "version": "1.0.0",