Skip to content

Commit

Permalink
Simpler logging
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 17, 2024
1 parent 2279ee9 commit 989e358
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 192 deletions.
2 changes: 1 addition & 1 deletion src/colors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub const CYAN: &str = "\x1b[36m\x1b[1m";
pub const PINK: &str = "\x1b[35m\x1b[1m";
pub const PURPLE: &str = "\x1b[34m\x1b[1m";
pub const RED: &str = "\x1b[31m\x1b[1m";
pub const RESET: &str = "\x1b[00m\x1b[0m";
pub const WHITE: &str = "\x1b[37m\x1b[1m";
pub const YELLOW: &str = "\x1b[33m\x1b[1m";
pub const PURPLE: &str = "\x1b[34m\x1b[1m";
60 changes: 22 additions & 38 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,53 @@
//use crate::colors::*;
use crate::colors::*;
use crate::indent::*;
//use crate::logging::*;
use crate::subs::*;
use crate::wrap::*;
use crate::Cli;
//use log::Level::Warn;

const MAX_TRIES: u8 = 10;

fn apply_passes(
file: &str,
args: &Cli,
filename: &str,
logs: &mut Vec<Log>,
) -> String {
let mut new_logs: Vec<Log> = vec![];
let mut new_file = apply_indent(file, args, filename, &mut new_logs);
fn apply_passes(file: &str, args: &Cli) -> String {
let mut new_file = apply_indent(file, args);
let mut finished = false;
let mut tries = 0;

while needs_wrap(&new_file) && !finished && tries < MAX_TRIES {
new_logs.clear();
let old_file = new_file.clone();
new_file = wrap(&new_file, filename, &mut new_logs);
new_file = wrap(&new_file);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_indent(&new_file, args, filename, &mut new_logs);
new_file = apply_indent(&new_file, args);
tries += 1;
if new_file == old_file {
finished = true;
}
}

// check indents return to zero
//if new_file.lines().last().unwrap().starts_with(' ') {
//log::error!("Indent does not return to zero at end of file");
//}
if new_file.lines().last().unwrap().starts_with(' ') {
log::error!("Indent does not return to zero at end of file");
}

// 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) {
log::warn!(
" Line {}: cannot be wrapped: {}{:.50}...",
i,
WHITE,
line
);
}
}
}
new_file
}

pub fn format_file(
file: &str,
args: &Cli,
filename: &str,
logs: &mut Vec<Log>,
) -> String {
pub fn format_file(file: &str, args: &Cli) -> String {
let mut new_file = remove_extra_newlines(file);
new_file = begin_end_environments_new_line(&new_file);
new_file = remove_tabs(&new_file);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_passes(&new_file, args, filename, logs);
new_file = apply_passes(&new_file, args);
new_file
}
32 changes: 5 additions & 27 deletions src/ignore.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::colors::*;
//use crate::logging::*;
//use log::Level::Warn;

//const IG_STARTS: [&str; 1] = ["\\begin{verbatim}"];
//const IG_ENDS: [&str; 1] = ["\\end{verbatim}"];
Expand All @@ -19,13 +17,7 @@ impl Ignore {
}
}

pub fn get_ignore(
line: &str,
linum: usize,
filename: &str,
prev_ignore: Ignore,
logs: &mut Vec<Log>,
) -> Ignore {
pub fn get_ignore(line: &str, i: usize, prev_ignore: Ignore) -> Ignore {
let skip = contains_ignore_skip(line);
let start = contains_ignore_start(line);
let end = contains_ignore_end(line);
Expand All @@ -37,37 +29,23 @@ pub fn get_ignore(
block = true
}
if end {
log::error!(
"Line {}: no ignore block to end: {}{:.50}...",
log::warn!(
" Line {}: no ignore block to end: {}{:.50}...",
i,
WHITE,
line
);
let log = Log {
level: Warn,
linum,
message,
filename: filename.to_string(),
};
record_log(logs, log);
}
} else {
// currently in ignore block
if start {
log::error!(
"Line {}: cannot start ignore block \
log::warn!(
" Line {}: cannot start ignore block \
before ending previous block: {}{:.50}...",
i,
WHITE,
line
);
let log = Log {
level: Warn,
linum,
message,
filename: filename.to_string(),
};
record_log(logs, log);
}
if end {
block = false
Expand Down
29 changes: 12 additions & 17 deletions src/indent.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//use crate::colors::*;
use crate::colors::*;
use crate::comments::*;
use crate::ignore::*;
use crate::parse::*;
Expand Down Expand Up @@ -102,37 +102,32 @@ fn get_indent(line: &str, prev_indent: Indent) -> Indent {
Indent { actual, visual }
}

pub fn apply_indent(
file: &str,
args: &Cli,
filename: &str,
logs: &mut Vec<Log>,
) -> String {
pub fn apply_indent(file: &str, args: &Cli) -> String {
log::info!("Indenting file");

let mut indent = Indent::new();
let mut ignore = Ignore::new();
let mut new_file = String::with_capacity(file.len());
let mut verbatim_count = 0;

for (linum, line) in file.lines().enumerate() {
for (i, line) in file.lines().enumerate() {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
ignore = get_ignore(line, linum, filename, ignore, logs);
ignore = get_ignore(line, i, ignore);
if verbatim_count == 0 && !is_ignored(&ignore) {
// calculate indent
let comment_index = find_comment_index(line);
let line_strip = remove_comment(line, comment_index);
indent = get_indent(line_strip, indent);
//log::info!(
//"Indent line {}: actual = {}, visual = {}:{} {}",
//i,
//indent.actual,
//indent.visual,
//WHITE,
//line,
//);
log::info!(
"Indent line {}: actual = {}, visual = {}:{} {}",
i,
indent.actual,
indent.visual,
WHITE,
line,
);

if (indent.visual < 0) || (indent.actual < 0) {
log::error!(
Expand Down
80 changes: 35 additions & 45 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
use crate::colors::*;
//use crate::print::*;
use crate::print::*;
use crate::Cli;
use env_logger::Builder;
use log::Level;
use log::Level::{Error, Info, Warn};
use log::Level::{Debug, Error, Info, Trace, 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<Mutex<HashSet<Log>>> =
//Lazy::new(|| Mutex::new(HashSet::new()));
pub static LOGS: Lazy<Mutex<HashSet<Log>>> =
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 {
Level::Info => CYAN.to_string(),
Level::Warn => YELLOW.to_string(),
Level::Error => RED.to_string(),
Info => CYAN.to_string(),
Warn => YELLOW.to_string(),
Error => RED.to_string(),
_ => panic!(),
}
}
Expand Down Expand Up @@ -52,33 +52,23 @@ 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 print_logs(filename: &str) {
//let mut logs: Vec<Log> = 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<Log> = 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),
}
}
}
14 changes: 6 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod regexes;
mod subs;
mod wrap;
mod write;
//use crate::colors::*;
use crate::colors::*;
use crate::format::*;
use crate::logging::*;
use crate::parse::*;
Expand All @@ -38,31 +38,29 @@ fn main() {
};

init_logger(&args);
if args.verbose {
print_script_name();
}
print_script_name();

for filename in &args.filenames {
//LOGS.lock().unwrap().clear();
LOGS.lock().unwrap().clear();

if args.verbose {
print_filename(filename);
}

if !check_extension_valid(filename) {
//log::error!("File type invalid for {}{}", WHITE, filename);
log::error!("File type invalid for {}{}", WHITE, filename);
continue;
};

let file = fs::read_to_string(filename).unwrap();
let new_file = format_file(&file, &args, filename, &mut logs);
let new_file = format_file(&file, &args);

if args.print {
print_file(&new_file);
} else {
write_file(filename, &new_file);
}

//print_logs(filename);
print_logs(filename);
}
}
2 changes: 1 addition & 1 deletion src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn print_script_name() {
}

pub fn print_filename(filename: &str) {
println!("{}{}{}{}{}", PINK, "tex-fmt ", PURPLE, filename, RESET);
println!("{}", String::new() + PURPLE + filename + RESET);
}

pub fn print_file(new_file: &str) {
Expand Down
Loading

0 comments on commit 989e358

Please sign in to comment.