referrerpolicy=no-referrer-when-downgrade

frame_remote_externalities/
logging.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use std::{
19	future::Future,
20	io::{self, IsTerminal},
21	time::Instant,
22};
23
24use spinners::{Spinner, Spinners};
25
26use super::Result;
27
28// A simple helper to time a operation with a nice spinner, start message, and end message.
29//
30// The spinner is only displayed when stdout is a terminal.
31pub(super) fn with_elapsed<F, R, EndMsg>(f: F, start_msg: &str, end_msg: EndMsg) -> Result<R>
32where
33	F: FnOnce() -> Result<R>,
34	EndMsg: FnOnce(&R) -> String,
35{
36	let timer = Instant::now();
37	let mut maybe_sp = start(start_msg);
38
39	Ok(end(f()?, timer, maybe_sp.as_mut(), end_msg))
40}
41
42// A simple helper to time an async operation with a nice spinner, start message, and end message.
43//
44// The spinner is only displayed when stdout is a terminal.
45pub(super) async fn with_elapsed_async<F, Fut, R, EndMsg>(
46	f: F,
47	start_msg: &str,
48	end_msg: EndMsg,
49) -> Result<R>
50where
51	F: FnOnce() -> Fut,
52	Fut: Future<Output = Result<R>>,
53	EndMsg: FnOnce(&R) -> String,
54{
55	let timer = Instant::now();
56	let mut maybe_sp = start(start_msg);
57
58	Ok(end(f().await?, timer, maybe_sp.as_mut(), end_msg))
59}
60
61fn start(start_msg: &str) -> Option<Spinner> {
62	let msg = format!("⏳ {start_msg}");
63
64	if io::stdout().is_terminal() {
65		Some(Spinner::new(Spinners::Dots, msg))
66	} else {
67		println!("{msg}");
68
69		None
70	}
71}
72
73fn end<T, EndMsg>(val: T, timer: Instant, maybe_sp: Option<&mut Spinner>, end_msg: EndMsg) -> T
74where
75	EndMsg: FnOnce(&T) -> String,
76{
77	let msg = format!("✅ {} in {:.2}s", end_msg(&val), timer.elapsed().as_secs_f32());
78
79	if let Some(sp) = maybe_sp {
80		sp.stop_with_message(msg);
81	} else {
82		println!("{msg}");
83	}
84
85	val
86}