From fe19a750344e8601e138520cdd29dce31ee10119 Mon Sep 17 00:00:00 2001 From: William G Underwood <42812654+WGUNDERWOOD@users.noreply.github.com> Date: Thu, 16 May 2024 23:31:57 -0400 Subject: [PATCH] Simpler logging --- src/format.rs | 34 +++++++++---------- src/ignore.rs | 26 +++++++-------- src/indent.rs | 14 +++----- src/logging.rs | 82 +++++++++++++++++++++++----------------------- src/main.rs | 8 +++-- src/print.rs | 2 +- src/wrap.rs | 14 +++++--- tests/wrap_out.tex | 2 ++ 8 files changed, 91 insertions(+), 91 deletions(-) diff --git a/src/format.rs b/src/format.rs index b051ed5..db7922e 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,10 +1,10 @@ -use crate::colors::*; +//use crate::colors::*; use crate::indent::*; -use crate::logging::*; +//use crate::logging::*; use crate::subs::*; use crate::wrap::*; use crate::Cli; -use log::Level::Warn; +//use log::Level::Warn; const MAX_TRIES: u8 = 10; @@ -30,20 +30,20 @@ fn apply_passes(file: &str, args: &Cli) -> String { } // TODO move this logging into wrap.rs - if needs_wrap(&new_file) { - for (i, line) in new_file.lines().enumerate() { - if line_needs_wrap(line) { - record_log( - Warn, - i, - format!( - " Line {}: cannot be wrapped: {}{:.50}...", - i, WHITE, line - ), - ); - } - } - } + //if needs_wrap(&new_file) { + //for (i, line) in new_file.lines().enumerate() { + //if line_needs_wrap(line) { + //record_log( + //Warn, + //i, + //format!( + //"Line {}: cannot be wrapped: {}{:.50}...", + //i, WHITE, line + //), + //); + //} + //} + //} new_file } diff --git a/src/ignore.rs b/src/ignore.rs index ed2d14f..d5b0729 100644 --- a/src/ignore.rs +++ b/src/ignore.rs @@ -1,6 +1,6 @@ use crate::colors::*; -use crate::logging::*; -use log::Level::Warn; +//use crate::logging::*; +//use log::Level::Warn; //const IG_STARTS: [&str; 1] = ["\\begin{verbatim}"]; //const IG_ENDS: [&str; 1] = ["\\end{verbatim}"]; @@ -31,26 +31,22 @@ pub fn get_ignore(line: &str, i: usize, prev_ignore: Ignore) -> Ignore { block = true } if end { - record_log( - Warn, + log::error!( + "Line {}: no ignore block to end: {}{:.50}...", i, - format!( - " Line {}: no ignore block to end: {}{:.50}...", - i, WHITE, line - ), + WHITE, + line ); } } else { // currently in ignore block if start { - record_log( - Warn, - i, - format!( - " Line {}: cannot start ignore block \ + log::error!( + "Line {}: cannot start ignore block \ before ending previous block: {}{:.50}...", - i, WHITE, line - ), + i, + WHITE, + line ); } if end { diff --git a/src/indent.rs b/src/indent.rs index 15c059c..543906e 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -1,12 +1,10 @@ use crate::colors::*; use crate::comments::*; use crate::ignore::*; -use crate::logging::*; use crate::parse::*; use crate::regexes::*; use crate::TAB; use core::cmp::max; -use log::Level::Error; const OPENS: [char; 3] = ['(', '[', '{']; const CLOSES: [char; 3] = [')', ']', '}']; @@ -107,8 +105,6 @@ fn get_indent(line: &str, prev_indent: Indent) -> Indent { pub fn apply_indent(file: &str, args: &Cli) -> String { log::info!("Indenting file"); - // TODO flush the logs of indent errors first - let mut indent = Indent::new(); let mut ignore = Ignore::new(); let mut new_file = String::with_capacity(file.len()); @@ -134,13 +130,11 @@ pub fn apply_indent(file: &str, args: &Cli) -> String { ); if (indent.visual < 0) || (indent.actual < 0) { - record_log( - Error, + log::error!( + "Line {}: indent is negative: {}{:.50}", i, - format!( - "Line {}: indent is negative: {}{:.50}", - i, WHITE, line - ), + WHITE, + line ); } diff --git a/src/logging.rs b/src/logging.rs index af44782..415844f 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -1,24 +1,24 @@ use crate::colors::*; -use crate::print::*; +//use crate::print::*; use crate::Cli; use env_logger::Builder; use log::Level; -use log::Level::{Debug, Error, Info, Trace, Warn}; +use log::Level::{Error, Info, Warn}; use log::LevelFilter; -use once_cell::sync::Lazy; -use std::collections::HashSet; +//use once_cell::sync::Lazy; +//use std::collections::HashSet; use std::io::Write; -use std::sync::Mutex; +//use std::sync::Mutex; -pub static LOGS: Lazy>> = - Lazy::new(|| Mutex::new(HashSet::new())); +//pub static LOGS: Lazy>> = +//Lazy::new(|| Mutex::new(HashSet::new())); -#[derive(Eq, Hash, PartialEq, Clone)] -pub struct Log { - level: Level, - linum: usize, - message: String, -} +//#[derive(Eq, Hash, PartialEq, Clone)] +//pub struct Log { +//level: Level, +//linum: usize, +//message: String, +//} fn get_log_style(log_level: Level) -> String { match log_level { @@ -52,33 +52,33 @@ pub fn init_logger(args: &Cli) { .init(); } -pub fn record_log(level: Level, linum: usize, message: String) { - let mut logs = LOGS.lock().unwrap(); - let log = Log { - level, - linum, - message, - }; - logs.insert(log); -} +//pub fn record_log(level: Level, linum: usize, message: String) { +//let mut logs = LOGS.lock().unwrap(); +//let log = Log { +//level, +//linum, +//message, +//}; +//logs.insert(log); +//} -pub fn print_logs(filename: &str) { - let mut logs: Vec = vec![]; - for log in LOGS.lock().unwrap().iter() { - logs.push(log.clone()); - } - logs.sort_by_key(|l| l.linum); +//pub fn print_logs(filename: &str) { +//let mut logs: Vec = vec![]; +//for log in LOGS.lock().unwrap().iter() { +//logs.push(log.clone()); +//} +//logs.sort_by_key(|l| l.linum); - if !logs.is_empty() { - print_filename(filename); - } - for log in logs { - match log.level { - Error => log::error!("{}", log.message), - Warn => log::warn!("{}", log.message), - Info => log::info!("{}", log.message), - Debug => log::debug!("{}", log.message), - Trace => log::trace!("{}", log.message), - } - } -} +//if !logs.is_empty() { +//print_filename(filename); +//} +//for log in logs { +//match log.level { +//Error => log::error!("{}", log.message), +//Warn => log::warn!("{}", log.message), +//Info => log::info!("{}", log.message), +//Debug => log::debug!("{}", log.message), +//Trace => log::trace!("{}", log.message), +//} +//} +//} diff --git a/src/main.rs b/src/main.rs index 64ca934..fcf06b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,10 +38,12 @@ fn main() { }; init_logger(&args); - print_script_name(); + if args.verbose { + print_script_name(); + } for filename in &args.filenames { - LOGS.lock().unwrap().clear(); + //LOGS.lock().unwrap().clear(); if args.verbose { print_filename(filename); @@ -61,6 +63,6 @@ fn main() { write_file(filename, &new_file); } - print_logs(filename); + //print_logs(filename); } } diff --git a/src/print.rs b/src/print.rs index a4aca4c..7f2152a 100644 --- a/src/print.rs +++ b/src/print.rs @@ -5,7 +5,7 @@ pub fn print_script_name() { } pub fn print_filename(filename: &str) { - println!("{}", String::new() + PURPLE + filename + RESET); + println!("{}{}{}{}{}", PINK, "tex-fmt ", PURPLE, filename, RESET); } pub fn print_file(new_file: &str) { diff --git a/src/wrap.rs b/src/wrap.rs index bf06e33..4c38cd9 100644 --- a/src/wrap.rs +++ b/src/wrap.rs @@ -30,7 +30,7 @@ fn find_wrap_point(line: &str) -> Option { wrap_point } -fn wrap_line(line: &str) -> String { +fn wrap_line(line: &str, linum: usize) -> String { log::info!("Wrap long line: {}{}", WHITE, line); let mut remaining_line = line.to_string(); let mut new_line = "".to_string(); @@ -58,6 +58,12 @@ fn wrap_line(line: &str) -> String { } None => { can_wrap = false; + log::error!( + "Line {}: cannot be wrapped: {}{:.50}...", + linum, + WHITE, + line + ); } } } @@ -71,14 +77,14 @@ pub fn wrap(file: &str) -> String { let mut new_line: String; let mut verbatim_count = 0; let mut ignore = Ignore::new(); - for (i, line) in file.lines().enumerate() { + for (linum, line) in file.lines().enumerate() { if RE_VERBATIM_BEGIN.is_match(line) { verbatim_count += 1; } - ignore = get_ignore(line, i, ignore); + ignore = get_ignore(line, linum, ignore); if line_needs_wrap(line) && verbatim_count == 0 && !is_ignored(&ignore) { - new_line = wrap_line(line); + new_line = wrap_line(line, linum); new_file.push_str(&new_line); } else { new_file.push_str(line); diff --git a/tests/wrap_out.tex b/tests/wrap_out.tex index 70fda6f..552f4fd 100644 --- a/tests/wrap_out.tex +++ b/tests/wrap_out.tex @@ -41,4 +41,6 @@ This line would usually be split at the special character part with a slash\ but it's best to break the line earlier. +)) + \end{document}