From 7c4eee2862f6d8a21316f08f053ce82f53de3890 Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Sat, 31 Aug 2024 12:36:28 -0400 Subject: [PATCH] fix: Ensure that debug logs header for builds properly display the time --- process/logging.rs | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/process/logging.rs b/process/logging.rs index 1d1f0cb1..f5157323 100644 --- a/process/logging.rs +++ b/process/logging.rs @@ -4,7 +4,7 @@ use std::{ io::{BufRead, BufReader, Result, Write as IoWrite}, path::{Path, PathBuf}, process::{Command, ExitStatus, Stdio}, - sync::{Arc, Mutex}, + sync::Mutex, thread, time::Duration, }; @@ -196,7 +196,6 @@ impl CommandLogging for Command { let ansi_color = gen_random_ansi_color(); let name = color_str(&image_ref, ansi_color); let short_name = color_str(shorten_name(&image_ref), ansi_color); - let log_prefix = Arc::new(log_header(short_name)); let (reader, writer) = os_pipe::pipe()?; self.stdout(writer.try_clone()?) @@ -233,7 +232,7 @@ impl CommandLogging for Command { let mp = Logger::multi_progress(); reader.lines().for_each(|line| { if let Ok(l) = line { - let text = format!("{log_prefix} {l}"); + let text = format!("{log_prefix} {l}", log_prefix = log_header(&short_name)); if mp.is_hidden() { eprintln!("{text}"); } else { @@ -321,19 +320,24 @@ impl Encode for CustomPatternEncoder { /// Used to keep the style of logs consistent between /// normal log use and command output. -fn log_header>(text: T) -> String { - let text = text.as_ref(); - match log::max_level() { - LevelFilter::Error | LevelFilter::Warn | LevelFilter::Info => { - format!("{text} {sep}", sep = "=>".bold()) +fn log_header(text: T) -> String +where + T: AsRef, +{ + fn inner(text: &str) -> String { + match log::max_level() { + LevelFilter::Error | LevelFilter::Warn | LevelFilter::Info => { + format!("{text} {sep}", sep = "=>".bold()) + } + LevelFilter::Debug | LevelFilter::Trace => format!( + "[{time} {text}] {sep}", + time = Local::now().format("%H:%M:%S"), + sep = "=>".bold(), + ), + LevelFilter::Off => String::new(), } - LevelFilter::Debug | LevelFilter::Trace => format!( - "[{time} {text}] {sep}", - time = Local::now().format("%H:%M:%S"), - sep = "=>".bold(), - ), - LevelFilter::Off => String::new(), } + inner(text.as_ref()) } /// Shortens the image name so that it won't take up the