- Commit
- 3afa2e1ee441531fedda7e9c71a0f226d642bc32
- Parent
- f6ea799c636d7efc47209979bf67e5d27e4b0031
- Author
- Pablo <pablo-pie@riseup.net>
- Date
Simplified the logging system
Removed some functionality from the logging system to simplify it
Custum build of stapix for tikz.pablopie.xyz
Simplified the logging system
Removed some functionality from the logging system to simplify it
2 files changed, 11 insertions, 88 deletions
| Status | Name | Changes | Insertions | Deletions |
| Modified | src/log.rs | 2 files changed | 4 | 61 |
| Modified | src/main.rs | 2 files changed | 7 | 27 |
diff --git a/src/log.rs b/src/log.rs @@ -5,13 +5,12 @@ //! require synchronization because they happen right before the entire //! application is killed. -use std::{io::{self, Write}, fmt::{Display, Arguments}, time::Duration}; +use std::{fmt::{Display, Arguments}, time::Duration}; const BOLD_RED: &str = "\u{001b}[1;31m"; const BOLD_GREEN: &str = "\u{001b}[1;32m"; const BOLD_YELLOW: &str = "\u{001b}[1;33m"; const BOLD_BLUE: &str = "\u{001b}[1;34m"; -const BOLD_CYAN: &str = "\u{001b}[1;36m"; const BOLD_WHITE: &str = "\u{001b}[1;37m"; const RESET: &str = "\u{001b}[0m"; @@ -24,12 +23,6 @@ pub(crate) enum Level { Warn, } -pub struct JobListLogger { - total: usize, - count: usize, - current_job_name: String, -} - #[macro_export] macro_rules! infoln { // infoln!("a {} event", "log"); @@ -92,59 +85,9 @@ pub fn usage_config() { println!(" {BOLD_YELLOW}Usage{RESET} See `examples/config.yaml` for an example of configuration file"); } -impl JobListLogger { - pub fn new(total_jobs: usize) -> Self { - const STRING_CAPACITY: usize = 64; - Self { - total: total_jobs, - count: 0, - current_job_name: String::with_capacity(STRING_CAPACITY), - } - } - - /// Adds a job to the queue and logs the fact this job has started - pub fn job_started<T: Display + ?Sized>(&mut self, job_name: &T) { - self.inc_counter(job_name); - - let mut stdout = io::stdout(); - let _ = write!( - stdout, - " {BOLD_CYAN}Rendering{RESET} {name}... {BOLD_WHITE}[{count:>padding$}/{total}]{RESET}\r", - count = self.count, - total = self.total, - padding = crate::log_floor(self.total), - name = self.current_job_name, - ); - let _ = stdout.flush(); - } - - /// Logs the fact the current job has just finished - pub fn job_finished(&self) { - debug_assert!(self.count > 0); - self.job_finished_impl(); - } - - /// Adds a job to the queue and logs the fact the it has just finished - pub fn job_started_and_finished(&mut self, job_name: &str) { - self.inc_counter(job_name); - self.job_finished_impl(); - } - - fn inc_counter<T: Display + ?Sized>(&mut self, job_name: &T) { - use std::fmt::Write; - self.count += 1; - self.current_job_name.clear(); - let _ = write!(&mut self.current_job_name, "{}", job_name); - } - - fn job_finished_impl(&self) { - let space_padding = "... [/]".len() + 2 * crate::log_floor(self.total); - println!( - " {BOLD_GREEN}Rendered{RESET} {name}{empty:space_padding$}", - name = self.current_job_name, - empty = "", - ); - } +/// Logs the fact a rendering job just finished +pub fn job_finished<T: Display + ?Sized>(job_name: &T) { + println!(" {BOLD_GREEN}Rendered{RESET} {job_name}"); } pub(crate) fn log(level: Level, args: &Arguments<'_>) {
diff --git a/src/main.rs b/src/main.rs @@ -13,7 +13,6 @@ use std::{ use gallery_entry::{GalleryEntry, FileFormat, LicenseType}; use threadpool::ThreadPool; -use log::JobListLogger; use escape::Escaped; #[macro_use] @@ -169,11 +168,9 @@ fn render_gallery( if !jobs.is_empty() { infoln!("Rendering HTML files..."); } - let mut pages_logger = JobListLogger::new(1 + jobs.len()); - pages_logger.job_started("index.html"); - render_index(&pics).map_err(|_| ())?; - pages_logger.job_finished(); + render_index(&pics).map_err(|_| ())?; + log::job_finished("index.html"); if jobs.is_empty() { warnln!( @@ -186,9 +183,8 @@ fn render_gallery( for Job { pic_id, page_path, .. } in &jobs { let pic = &pics[*pic_id]; - pages_logger.job_started(&HtmlFileName(&pic.file_name)); - render_pic_page(pic, page_path).map_err(|_| ())?; - pages_logger.job_finished(); + render_pic_page(pic, page_path).map_err(|_| ())?; + log::job_finished(&HtmlFileName(&pic.file_name)); } // ======================================================================== @@ -201,7 +197,6 @@ fn render_gallery( // ======================================================================== let num_cores = cmp::min(num_cores, jobs.len()); - let mut thumbs_logger = JobListLogger::new(jobs.len()); // NOTE: only spawn the threads if necessary if num_cores > 1 { @@ -235,16 +230,15 @@ fn render_gallery( let pic_id = msg.unwrap()?; let pic = &pics[pic_id]; - thumbs_logger.job_started_and_finished(&pic.file_name); + log::job_finished(&pic.file_name); } } else { infoln!("Rendering thumbnails... (using 1/{total_cores} core)"); for Job { pic_id, thumb_path, .. } in &jobs { let pic = &pics[*pic_id]; - thumbs_logger.job_started(&pic.file_name); - render_thumbnail(pic, thumb_path)?; - thumbs_logger.job_finished(); + render_thumbnail(pic, thumb_path)?; + log::job_finished(&pic.file_name); } } @@ -549,20 +543,6 @@ fn copy(from: &Path, to: &Path) -> Result<(), ()> { .map_err(|e| errorln!("Failed to copy {from:?} to {to:?}: {e}")) } -fn log_floor(n: usize) -> usize { - if n == 0 { return 1; } - - let mut d = 0; - let mut m = n; - - while m > 0 { - d += 1; - m /= 10; - } - - d -} - impl Display for ThumbPath<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!(f, "{THUMBS_PATH}/{name}", name = Escaped(&self.0.file_name))?;