Skip to content

Commit daeafea

Browse files
committed
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 <[email protected]>
1 parent 3638b41 commit daeafea

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/src/utils.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::time::Duration;
66

77
use anyhow::{Context, Result};
88
use cap_std_ext::cap_std::fs::Dir;
9+
use indicatif::HumanDuration;
10+
use libsystemd::logging::journal_print;
911
use ostree::glib;
1012
use ostree_ext::container::SignatureSource;
1113
use ostree_ext::ostree;
@@ -119,6 +121,7 @@ pub(crate) async fn async_task_with_spinner<F, T>(msg: &str, f: F) -> T
119121
where
120122
F: Future<Output = T>,
121123
{
124+
let start_time = std::time::Instant::now();
122125
let pb = indicatif::ProgressBar::new_spinner();
123126
let style = indicatif::ProgressStyle::default_bar();
124127
pb.set_style(style.template("{spinner} {msg}").unwrap());
@@ -131,10 +134,15 @@ where
131134
std::io::stdout().flush().unwrap();
132135
}
133136
let r = f.await;
137+
let elapsed = HumanDuration(start_time.elapsed());
138+
let _ = journal_print(
139+
libsystemd::logging::Priority::Info,
140+
&format!("completed task in {elapsed}: {msg}"),
141+
);
134142
if pb.is_hidden() {
135-
println!("done");
143+
println!("done ({elapsed})");
136144
} else {
137-
pb.finish_with_message(format!("{msg}: done"));
145+
pb.finish_with_message(format!("{msg}: done ({elapsed})"));
138146
}
139147
r
140148
}

0 commit comments

Comments
 (0)