Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: eliminate false panics #61

Merged
merged 5 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions crates/diagnostics/src/local_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
) -> Vec<Box<dyn Layer<tracing_subscriber::Registry> + 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<dyn Layer<tracing_subscriber::Registry> + 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
}
11 changes: 5 additions & 6 deletions crates/package_json/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
collections::HashMap,
convert::Into,
ffi::OsStr,
fs,
io::{Read, Write},
path::PathBuf,
Expand Down Expand Up @@ -77,11 +76,11 @@ impl PackageJson {

fn write_to_file(path: &PathBuf) -> Result<Value, PackageJsonError> {
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",
Expand Down