From c48b509959b20b00d53e32ac318d58448407de6e Mon Sep 17 00:00:00 2001 From: kaedeek Date: Sat, 14 Feb 2026 23:00:17 +0900 Subject: [PATCH 1/2] feat(Update): add progress logging. --- src/lib.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 399880b..3cad4c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,13 @@ static RESET: &str = "\x1b[0m"; static VALID_MODES: &[&str] = &["simple", "simple2", "detailed", "file"]; static VALID_SAVE_LEVELS: &[&str] = &["INFO", "ERROR", "WARNING", "DEBUG", "CRITICAL"]; +pub struct TimerGuard<'a> { + logger: &'a VersaLog, + title: String, + tags: Vec, + start: Instant, +} + pub fn NewVersaLog( enum_mode: &str, show_file: bool, @@ -166,6 +173,18 @@ pub fn NewVersaLogSimple2(enum_mode: &str, tag: &str, enable_all: bool) -> Versa ) } +impl<'a> Drop for TimerGuard<'a> { + fn drop(&mut self) { + let elapsed = self.start.elapsed().as_secs_f64(); + let msg = format!("{} : done ({:.2}s)", self.title, elapsed); + + let tag_refs: Vec<&str> = + self.tags.iter().map(|s| s.as_str()).collect(); + + self.logger.Info(&msg, &tag_refs); + } +} + impl VersaLog { pub fn log(&self, msg: String, level: String, tags: &[&str]) { let level = level.to_uppercase(); @@ -392,6 +411,34 @@ impl VersaLog { Local::now().format("%Y-%m-%d %H:%M:%S").to_string() } + pub fn Step(&self, title: &str, step: usize, total: usize, tags: &[&str]) { + let msg = format!("[STEP {}/{}] {}", step, total, title); + self.Info(&msg, tags); + } + + pub fn Progress(&self, title: &str, current: usize, total: usize, tags: &[&str]) { + let percent = if total > 0 { + (current as f64 / total as f64 * 100.0) as usize + } else { + 0 + }; + + let msg = format!("{} : {}% ({}/{})", title, percent, current, total); + self.Info(&msg, tags); + } + + pub fn Timer<'a>(&'a self, title: &str, tags: &[&str]) -> TimerGuard<'a> { + let msg = format!("{} : start", title); + self.Info(&msg, tags); + + TimerGuard { + logger: self, + title: title.to_string(), + tags: tags.iter().map(|s| s.to_string()).collect(), + start: Instant::now(), + } + } + fn get_caller(&self) -> String { let bt = Backtrace::new(); if let Some(frame) = bt.frames().get(3) { From 90783a41efabcfa3cbe2919c5149ee6631b110ff Mon Sep 17 00:00:00 2001 From: kaedeek Date: Sat, 14 Feb 2026 23:04:21 +0900 Subject: [PATCH 2/2] feat(examples): add examples --- examples/progress_logging.rs | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 examples/progress_logging.rs diff --git a/examples/progress_logging.rs b/examples/progress_logging.rs new file mode 100644 index 0000000..374cc75 --- /dev/null +++ b/examples/progress_logging.rs @@ -0,0 +1,56 @@ +use versalogrs::NewVersaLog; + +fn process_file(log: &VersaLog, index: usize, total: usize) { + log.Step( + &format!("Processing file_{}.txt", index), + index, + total, + &[], + ); + + let _timer = log.Timer( + &format!("file_{}.txt", index), + &[], + ); + + let total_lines = 10; + + for i in 1..=total_lines { + std::thread::sleep(std::time::Duration::from_millis(100)); + + log.Progress( + &format!("file_{}.txt", index), + i, + total_lines, + &[], + ); + } +} + +fn main() { + let log = VersaLog::new( + "detailed", + true, + true, + "BATCH", + ); + + log.Info("Batch Start", &[]); + + let _batch_timer = log.Timer("Total Batch", &[]); + + let total_files = 3; + + for i in 1..=total_files { + process_file(&log, i, total_files); + + log.Progress( + "Overall Progress", + i, + total_files, + &[], + ); + } + + log.Info("Batch Finished", &[]); +} \ No newline at end of file