fix: Ensure that debug logs header for builds properly display the time

This commit is contained in:
Gerald Pinder 2024-08-31 12:36:28 -04:00
parent 74d99f2b17
commit 7c4eee2862

View file

@ -4,7 +4,7 @@ use std::{
io::{BufRead, BufReader, Result, Write as IoWrite}, io::{BufRead, BufReader, Result, Write as IoWrite},
path::{Path, PathBuf}, path::{Path, PathBuf},
process::{Command, ExitStatus, Stdio}, process::{Command, ExitStatus, Stdio},
sync::{Arc, Mutex}, sync::Mutex,
thread, thread,
time::Duration, time::Duration,
}; };
@ -196,7 +196,6 @@ impl CommandLogging for Command {
let ansi_color = gen_random_ansi_color(); let ansi_color = gen_random_ansi_color();
let name = color_str(&image_ref, ansi_color); let name = color_str(&image_ref, ansi_color);
let short_name = color_str(shorten_name(&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()?; let (reader, writer) = os_pipe::pipe()?;
self.stdout(writer.try_clone()?) self.stdout(writer.try_clone()?)
@ -233,7 +232,7 @@ impl CommandLogging for Command {
let mp = Logger::multi_progress(); let mp = Logger::multi_progress();
reader.lines().for_each(|line| { reader.lines().for_each(|line| {
if let Ok(l) = 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() { if mp.is_hidden() {
eprintln!("{text}"); eprintln!("{text}");
} else { } else {
@ -321,19 +320,24 @@ impl Encode for CustomPatternEncoder {
/// Used to keep the style of logs consistent between /// Used to keep the style of logs consistent between
/// normal log use and command output. /// normal log use and command output.
fn log_header<T: AsRef<str>>(text: T) -> String { fn log_header<T>(text: T) -> String
let text = text.as_ref(); where
match log::max_level() { T: AsRef<str>,
LevelFilter::Error | LevelFilter::Warn | LevelFilter::Info => { {
format!("{text} {sep}", sep = "=>".bold()) 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 /// Shortens the image name so that it won't take up the