Skip to content

Commit

Permalink
devenv: simplify logging inside direnv
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo committed Nov 28, 2024
1 parent 0c1ed40 commit d9fff24
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 60 deletions.
14 changes: 3 additions & 11 deletions devenv/src/devenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Devenv {
}

pub async fn print_dev_env(&mut self, json: bool) -> Result<()> {
let env = self.get_dev_environment(json, false).await?;
let env = self.get_dev_environment(json).await?;
print!(
"{}",
String::from_utf8(env.output).expect("Failed to convert env to utf-8")
Expand Down Expand Up @@ -223,7 +223,7 @@ impl Devenv {
args: &[String],
) -> Result<Vec<String>> {
self.assemble(false)?;
let env = self.get_dev_environment(false, true).await?;
let env = self.get_dev_environment(false).await?;

let mut develop_args = vec![
"develop",
Expand Down Expand Up @@ -839,13 +839,9 @@ impl Devenv {
Ok(())
}

pub async fn get_dev_environment(&mut self, json: bool, logging: bool) -> Result<DevEnv> {
pub async fn get_dev_environment(&mut self, json: bool) -> Result<DevEnv> {
self.assemble(false)?;

if !logging {
log::disable_user_messages();
}

let gc_root = self.devenv_dot_gc.join("shell");
let span = tracing::info_span!("building_shell", devenv.user_message = "Building shell",);
let env = self.nix.dev_env(json, &gc_root).instrument(span).await?;
Expand All @@ -860,10 +856,6 @@ impl Devenv {
)
.expect("Failed to write input-paths.txt");

if !logging {
log::enable_user_messages();
}

Ok(DevEnv {
output: env.stdout,
gc_root,
Expand Down
75 changes: 26 additions & 49 deletions devenv/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,13 @@ use std::sync::atomic::{AtomicBool, Ordering};

#[derive(Default)]
pub struct DevenvLayer {
/// Whether to show user messages.
/// Can be dynamically toggled by [enable_user_messages] and [disable_user_messages].
show_user_messages: AtomicBool,
/// Whether the span has an error event.
has_error: AtomicBool,
}

impl DevenvLayer {
pub fn new() -> Self {
Self {
show_user_messages: AtomicBool::new(true),
has_error: AtomicBool::new(false),
}
}
Expand Down Expand Up @@ -240,20 +236,18 @@ where
timings: SpanTimings::new(),
});

if self.show_user_messages.load(Ordering::Relaxed) {
with_event_from_span!(
id,
span,
"message" = msg,
"devenv.is_user_message" = true,
"devenv.span_event_kind" = SpanKind::Start as u8,
|event| {
drop(ext);
drop(span);
ctx.event(&event);
}
);
}
with_event_from_span!(
id,
span,
"message" = msg,
"devenv.is_user_message" = true,
"devenv.span_event_kind" = SpanKind::Start as u8,
|event| {
drop(ext);
drop(span);
ctx.event(&event);
}
);
}
}

Expand Down Expand Up @@ -288,22 +282,20 @@ where
let msg = span_ctx.msg.clone();
let time_total = format!("{}", span_ctx.timings.total_duration());

if self.show_user_messages.load(Ordering::Relaxed) {
with_event_from_span!(
id,
span,
"message" = msg,
"devenv.is_user_message" = true,
"devenv.span_event_kind" = SpanKind::End as u8,
"devenv.span_has_error" = has_error,
"devenv.time_total" = time_total,
|event| {
drop(extensions);
drop(span);
ctx.event(&event);
}
);
}
with_event_from_span!(
id,
span,
"message" = msg,
"devenv.is_user_message" = true,
"devenv.span_event_kind" = SpanKind::End as u8,
"devenv.span_has_error" = has_error,
"devenv.time_total" = time_total,
|event| {
drop(extensions);
drop(span);
ctx.event(&event);
}
);
}
}

Expand All @@ -315,21 +307,6 @@ where
}
}

pub fn enable_user_messages() {
set_show_user_messages(true);
}

pub fn disable_user_messages() {
set_show_user_messages(false);
}

fn set_show_user_messages(show: bool) {
let dispatcher = tracing::dispatcher::get_default(|dispatcher| dispatcher.clone());
if let Some(layer) = dispatcher.downcast_ref::<DevenvLayer>() {
layer.show_user_messages.store(show, Ordering::SeqCst);
};
}

#[derive(Default)]
pub struct DevenvFormat {
pub verbose: bool,
Expand Down

0 comments on commit d9fff24

Please sign in to comment.