Skip to content

Commit

Permalink
fix: Ensure that debug logs header for builds properly display the time
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Aug 31, 2024
1 parent 74d99f2 commit 7c4eee2
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions process/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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()?)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<T: AsRef<str>>(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<T>(text: T) -> String
where
T: AsRef<str>,
{
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
Expand Down

0 comments on commit 7c4eee2

Please sign in to comment.