Skip to content

Commit

Permalink
first bare addition of log progress (from gitoxide)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 10, 2020
1 parent fa688c8 commit 006ba9d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ required-features = [
default = ["progress-tree", "progress-tree-log", "localtime"]
progress-tree = ["dashmap", "parking_lot"]
progress-tree-log = ["log"]
log-progress = ["log"]
unit-bytes = ["bytesize"]
unit-human = ["human_format"]
unit-duration = ["compound_duration"]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ This crate comes with various cargo features to tailor it to your needs.
* **progress-tree-log** _(default)_
* If logging in the `log` crate is initialized, a `log` will be used to output all messages provided to
`tree::Item::message(…)` and friends. No actual progress is written.
* May interfere with `tui-renderer` or `line-renderer`
* May interfere with `tui-renderer` or `line-renderer`, or any renderer outputting to the console.
* **log-progress**
* A `Progress` implementation which logs messages and progress using the `log` crate
* **local-time** _(default)_
* If set, timestamps in the message pane of the `tui-renderer` will be using the local time, not UTC
* If set, timestamps of the log messages of the `line-renderer` will be using the local time, not UTC
Expand Down
88 changes: 88 additions & 0 deletions src/progress/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::{messages::MessageLevel, Progress, Unit};
use std::time::Duration;

pub struct Log {
name: String,
max: Option<usize>,
unit: Option<Unit>,
last_set: Option<std::time::SystemTime>,
step: usize,
current_level: usize,
max_level: usize,
}

const EMIT_LOG_EVERY_S: f32 = 0.5;

impl Log {
pub fn new(name: impl Into<String>, max_level: Option<usize>) -> Self {
Log {
name: name.into(),
current_level: 0,
max_level: max_level.unwrap_or(usize::MAX),
max: None,
step: 0,
unit: None,
last_set: None,
}
}
}

impl Progress for Log {
type SubProgress = Log;

fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
Log {
name: format!("{}::{}", self.name, Into::<String>::into(name)),
current_level: self.current_level + 1,
max_level: self.max_level,
step: 0,
max: None,
unit: None,
last_set: None,
}
}

fn init(&mut self, max: Option<usize>, 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;
}
let now = std::time::SystemTime::now();
if self
.last_set
.map(|last| {
now.duration_since(last)
.unwrap_or_else(|_| Duration::default())
.as_secs_f32()
})
.unwrap_or_else(|| EMIT_LOG_EVERY_S * 2.0)
> EMIT_LOG_EVERY_S
{
self.last_set = Some(now);
match (self.max, self.unit) {
(Some(max), Some(unit)) => log::info!("{} → {} / {} {}", self.name, step, max, unit),
(None, Some(unit)) => log::info!("{} → {} {}", self.name, step, unit),
(Some(max), None) => log::info!("{} → {} / {}", self.name, step, max),
(None, None) => log::info!("{} → {}", self.name, step),
}
}
}

fn inc_by(&mut self, step: usize) {
self.set(self.step + step)
}

fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
let message: String = message.into();
match level {
MessageLevel::Info => log::info!("ℹ{} → {}", self.name, message),
MessageLevel::Failure => log::error!("𐄂{} → {}", self.name, message),
MessageLevel::Success => log::info!("✓{} → {}", self.name, message),
}
}
}
4 changes: 4 additions & 0 deletions src/progress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use std::time::SystemTime;

pub mod key;
mod utils;

#[cfg(feature = "log-progress")]
pub mod log;

#[doc(inline)]
pub use key::Key;

Expand Down

0 comments on commit 006ba9d

Please sign in to comment.