]>
git.proxmox.com Git - rustc.git/blob - vendor/prodash/src/progress/log.rs
3 atomic
::{AtomicBool, Ordering}
,
10 messages
::MessageLevel
,
11 progress
::{Id, Step, StepShared}
,
15 /// A [`Progress`] implementation which displays progress as it happens without the use of a renderer.
17 /// Note that this incurs considerable performance cost as each progress calls ends up getting the system time
18 /// to see if progress information should actually be emitted.
27 trigger
: Arc
<AtomicBool
>,
30 const EMIT_LOG_EVERY_S
: f32 = 0.5;
31 const SEP
: &str = "::";
34 /// Create a new instance from `name` while displaying progress information only up to `max_level`.
35 pub fn new(name
: impl Into
<String
>, max_level
: Option
<usize>) -> Self {
36 let trigger
= Arc
::new(AtomicBool
::new(true));
38 let duration
= Duration
::from_secs_f32(EMIT_LOG_EVERY_S
);
39 let trigger
= Arc
::downgrade(&trigger
);
41 while let Some(t
) = trigger
.upgrade() {
42 t
.store(true, Ordering
::Relaxed
);
43 std
::thread
::sleep(duration
);
49 id
: crate::progress
::UNKNOWN
,
51 max_level
: max_level
.unwrap_or(usize::MAX
),
60 impl Progress
for Log
{
61 type SubProgress
= Log
;
63 fn add_child(&mut self, name
: impl Into
<String
>) -> Self::SubProgress
{
64 self.add_child_with_id(name
, crate::progress
::UNKNOWN
)
67 fn add_child_with_id(&mut self, name
: impl Into
<String
>, id
: Id
) -> Self::SubProgress
{
69 name
: format
!("{}{}{}", self.name
, SEP
, Into
::<String
>::into(name
)),
71 current_level
: self.current_level
+ 1,
72 max_level
: self.max_level
,
76 trigger
: Arc
::clone(&self.trigger
),
80 fn init(&mut self, max
: Option
<usize>, unit
: Option
<Unit
>) {
85 fn set(&mut self, step
: usize) {
87 if self.current_level
> self.max_level
{
90 if self.trigger
.swap(false, Ordering
::Relaxed
) {
91 match (self.max
, &self.unit
) {
92 (max
, Some(unit
)) => log
::info
!("{} → {}", self.name
, unit
.display(step
, max
, None
)),
93 (Some(max
), None
) => log
::info
!("{} → {} / {}", self.name
, step
, max
),
94 (None
, None
) => log
::info
!("{} → {}", self.name
, step
),
99 fn unit(&self) -> Option
<Unit
> {
103 fn max(&self) -> Option
<usize> {
107 fn set_max(&mut self, max
: Option
<Step
>) -> Option
<Step
> {
113 fn step(&self) -> usize {
117 fn inc_by(&mut self, step
: usize) {
118 self.set(self.step
+ step
)
121 fn set_name(&mut self, name
: impl Into
<String
>) {
122 let name
= name
.into();
127 .map(|parent
| format
!("{}{}{}", parent
.to_owned(), SEP
, name
))
131 fn name(&self) -> Option
<String
> {
132 self.name
.split(SEP
).nth(1).map(ToOwned
::to_owned
)
139 fn message(&self, level
: MessageLevel
, message
: impl Into
<String
>) {
140 let message
: String
= message
.into();
142 MessageLevel
::Info
=> log
::info
!("ℹ{} → {}", self.name
, message
),
143 MessageLevel
::Failure
=> log
::error
!("𐄂{} → {}", self.name
, message
),
144 MessageLevel
::Success
=> log
::info
!("✓{} → {}", self.name
, message
),
148 fn counter(&self) -> Option
<StepShared
> {