Skip to content

Commit

Permalink
utils: Print and log time for async_task_with_spinner
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cgwalters committed Nov 5, 2024
1 parent 3638b41 commit daeafea
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,6 +121,7 @@ pub(crate) async fn async_task_with_spinner<F, T>(msg: &str, f: F) -> T
where
F: Future<Output = T>,
{
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());
Expand All @@ -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
}
Expand Down

0 comments on commit daeafea

Please sign in to comment.