From daeafeadd5a4c8c6457b7062c6c43b32345cd495 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sun, 27 Oct 2024 15:41:48 -0400 Subject: [PATCH] utils: Print and log time for async_task_with_spinner Things we do via this mechanism can take some time; let's log the elapsed time for greater visibility both to the human output *and* log to the journal. Signed-off-by: Colin Walters --- lib/src/utils.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/src/utils.rs b/lib/src/utils.rs index 81b253fe..33403afe 100644 --- a/lib/src/utils.rs +++ b/lib/src/utils.rs @@ -6,6 +6,8 @@ use std::time::Duration; use anyhow::{Context, Result}; use cap_std_ext::cap_std::fs::Dir; +use indicatif::HumanDuration; +use libsystemd::logging::journal_print; use ostree::glib; use ostree_ext::container::SignatureSource; use ostree_ext::ostree; @@ -119,6 +121,7 @@ pub(crate) async fn async_task_with_spinner(msg: &str, f: F) -> T where F: Future, { + let start_time = std::time::Instant::now(); let pb = indicatif::ProgressBar::new_spinner(); let style = indicatif::ProgressStyle::default_bar(); pb.set_style(style.template("{spinner} {msg}").unwrap()); @@ -131,10 +134,15 @@ where std::io::stdout().flush().unwrap(); } let r = f.await; + let elapsed = HumanDuration(start_time.elapsed()); + let _ = journal_print( + libsystemd::logging::Priority::Info, + &format!("completed task in {elapsed}: {msg}"), + ); if pb.is_hidden() { - println!("done"); + println!("done ({elapsed})"); } else { - pb.finish_with_message(format!("{msg}: done")); + pb.finish_with_message(format!("{msg}: done ({elapsed})")); } r }