Skip to content

Commit

Permalink
Merge branch 'log-fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 16, 2023
2 parents 02fcb9b + 78272c0 commit a38d22c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ all-features = true
[dev-dependencies]
rand = "0.8.1"
env_logger = { version = "0.10.0", default-features = false, features = ["humantime"] }
criterion = { version = "0.4.0", default-features = false }
criterion = { version = "0.5.1", default-features = false }
futures-util = { version = "0.3.4", default-features = false }
argh = "0.1.3"
futures = "0.3.5"
Expand Down
50 changes: 29 additions & 21 deletions src/progress/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Log {
id: Id,
max: Option<usize>,
unit: Option<Unit>,
step: usize,
step: StepShared,
current_level: usize,
max_level: usize,
trigger: Arc<AtomicBool>,
Expand Down Expand Up @@ -50,13 +50,29 @@ impl Log {
current_level: 0,
max_level: max_level.unwrap_or(usize::MAX),
max: None,
step: 0,
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 Progress for Log {
type SubProgress = Log;

Expand All @@ -70,37 +86,28 @@ impl Progress for Log {
id,
current_level: self.current_level + 1,
max_level: self.max_level,
step: 0,
step: Default::default(),
max: None,
unit: None,
trigger: Arc::clone(&self.trigger),
}
}

fn init(&mut self, max: Option<usize>, unit: Option<Unit>) {
fn init(&mut self, max: Option<Step>, unit: Option<Unit>) {
self.max = max;
self.unit = unit;
}

fn set(&mut self, step: usize) {
self.step = step;
if self.current_level > self.max_level {
return;
}
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),
}
}
fn set(&mut self, step: Step) {
self.step.store(step, Ordering::SeqCst);
self.maybe_log()
}

fn unit(&self) -> Option<Unit> {
self.unit.clone()
}

fn max(&self) -> Option<usize> {
fn max(&self) -> Option<Step> {
self.max
}

Expand All @@ -111,11 +118,12 @@ impl Progress for Log {
}

fn step(&self) -> usize {
self.step
self.step.load(Ordering::Relaxed)
}

fn inc_by(&mut self, step: usize) {
self.set(self.step + step)
fn inc_by(&mut self, step: Step) {
self.step.fetch_add(step, Ordering::SeqCst);
self.maybe_log()
}

fn set_name(&mut self, name: impl Into<String>) {
Expand Down Expand Up @@ -146,6 +154,6 @@ impl Progress for Log {
}

fn counter(&self) -> Option<StepShared> {
None
Some(self.step.clone())
}
}
4 changes: 2 additions & 2 deletions src/tree/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl Item {
.value_mut()
.progress
.as_mut()
.and_then(|mut p| {
.and_then(|p| {
let prev = p.done_at;
p.done_at = max;
prev
Expand All @@ -176,7 +176,7 @@ impl Item {
{
self.tree
.get_mut(&self.key, |v| {
v.progress.as_mut().and_then(|mut p| {
v.progress.as_mut().and_then(|p| {
let prev = p.done_at;
p.done_at = max;
prev
Expand Down

0 comments on commit a38d22c

Please sign in to comment.