1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
use std::{
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};
use crate::{
messages::MessageLevel,
progress::{Id, Step, StepShared},
Count, NestedProgress, Progress, Unit,
};
/// A [`NestedProgress`] implementation which displays progress as it happens without the use of a renderer.
///
/// Note that this incurs considerable performance cost as each progress calls ends up getting the system time
/// to see if progress information should actually be emitted.
pub struct Log {
name: String,
id: Id,
max: Option<usize>,
unit: Option<Unit>,
step: StepShared,
current_level: usize,
max_level: usize,
trigger: Arc<AtomicBool>,
}
const EMIT_LOG_EVERY_S: f32 = 0.5;
const SEP: &str = "::";
impl Log {
/// Create a new instance from `name` while displaying progress information only up to `max_level`.
pub fn new(name: impl Into<String>, max_level: Option<usize>) -> Self {
let trigger = Arc::new(AtomicBool::new(true));
std::thread::spawn({
let duration = Duration::from_secs_f32(EMIT_LOG_EVERY_S);
let trigger = Arc::downgrade(&trigger);
move || {
while let Some(t) = trigger.upgrade() {
t.store(true, Ordering::Relaxed);
std::thread::sleep(duration);
}
}
});
Log {
name: name.into(),
id: crate::progress::UNKNOWN,
current_level: 0,
max_level: max_level.unwrap_or(usize::MAX),
max: None,
step: Default::default(),
unit: None,
trigger,
}
}
}
impl Log {
fn maybe_log(&self) {
if self.current_level > self.max_level {
return;
}
let step = self.step();
if self.trigger.swap(false, Ordering::Relaxed) {
match (self.max, &self.unit) {
(max, Some(unit)) => log::info!("{} → {}", self.name, unit.display(step, max, None)),
(Some(max), None) => log::info!("{} → {} / {}", self.name, step, max),
(None, None) => log::info!("{} → {}", self.name, step),
}
}
}
}
impl Count for Log {
fn set(&self, step: Step) {
self.step.store(step, Ordering::SeqCst);
self.maybe_log()
}
fn step(&self) -> usize {
self.step.load(Ordering::Relaxed)
}
fn inc_by(&self, step: Step) {
self.step.fetch_add(step, Ordering::Relaxed);
self.maybe_log()
}
fn counter(&self) -> StepShared {
self.step.clone()
}
}
impl Progress for Log {
fn init(&mut self, max: Option<Step>, unit: Option<Unit>) {
self.max = max;
self.unit = unit;
}
fn unit(&self) -> Option<Unit> {
self.unit.clone()
}
fn max(&self) -> Option<Step> {
self.max
}
fn set_max(&mut self, max: Option<Step>) -> Option<Step> {
let prev = self.max;
self.max = max;
prev
}
fn set_name(&mut self, name: String) {
self.name = self
.name
.split("::")
.next()
.map(|parent| format!("{}{}{}", parent.to_owned(), SEP, name))
.unwrap_or(name);
}
fn name(&self) -> Option<String> {
self.name.split(SEP).nth(1).map(ToOwned::to_owned)
}
fn id(&self) -> Id {
self.id
}
fn message(&self, level: MessageLevel, message: String) {
match level {
MessageLevel::Info => log::info!("ℹ{} → {}", self.name, message),
MessageLevel::Failure => log::error!("𐄂{} → {}", self.name, message),
MessageLevel::Success => log::info!("✓{} → {}", self.name, message),
}
}
}
impl NestedProgress for Log {
type SubProgress = Log;
fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
self.add_child_with_id(name, crate::progress::UNKNOWN)
}
fn add_child_with_id(&mut self, name: impl Into<String>, id: Id) -> Self::SubProgress {
Log {
name: format!("{}{}{}", self.name, SEP, Into::<String>::into(name)),
id,
current_level: self.current_level + 1,
max_level: self.max_level,
step: Default::default(),
max: None,
unit: None,
trigger: Arc::clone(&self.trigger),
}
}
}
|