Skip to content

Commit

Permalink
Working on new logging framework
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 16, 2024
1 parent 83bd4b8 commit 3b018cf
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ clap = { version = "=4.4.0", features = ["derive"] }
env_logger = "0.11.3"
lazy_static = "1.4.0"
log = "0.4.21"
once_cell = "1.19.0"
regex = "1.10.3"
rstest = "0.19.0"
rstest_reuse = "0.6.0"
1 change: 1 addition & 0 deletions src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ 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";
23 changes: 11 additions & 12 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
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) -> String {
let (mut new_file, mut indent_errs) = apply_indent(file, args);
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 {
let old_file = new_file.clone();
new_file = wrap(&new_file);
new_file = remove_trailing_spaces(&new_file);
(new_file, indent_errs) = apply_indent(&new_file, args);
new_file = apply_indent(&new_file, args);
tries += 1;
if new_file == old_file {
finished = true;
}
}

for indent_err in indent_errs {
let i = indent_err.0;
let line = new_file.lines().nth(i).unwrap();
log::error!("Indent is negative on line {}: {}{}", i, WHITE, line);
}

// 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");
}

// 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) {
log::warn!(
"Line {} cannot be wrapped: {}{:.50}...",
record_log(
Warn,
i,
WHITE,
line
format!(
" Line {}: cannot be wrapped: {}{:.50}...",
i, WHITE, line
),
);
}
}
Expand Down
23 changes: 15 additions & 8 deletions src/ignore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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 Down Expand Up @@ -29,21 +31,26 @@ pub fn get_ignore(line: &str, i: usize, prev_ignore: Ignore) -> Ignore {
block = true
}
if end {
log::warn!(
"No ignore block to end on line {}: {}{:.50}...",
record_log(
Warn,
i,
WHITE,
line
format!(
" Line {}: no ignore block to end: {}{:.50}...",
i, WHITE, line
),
);
}
} else {
// currently in ignore block
if start {
log::warn!(
"Cannot start ignore block on line {} before ending previous block: {}{:.50}...",
record_log(
Warn,
i,
WHITE,
line
format!(
" Line {}: cannot start ignore block \
before ending previous block: {}{:.50}...",
i, WHITE, line
),
);
}
if end {
Expand Down
19 changes: 15 additions & 4 deletions src/indent.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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] = [')', ']', '}'];
Expand Down Expand Up @@ -102,13 +104,15 @@ fn get_indent(line: &str, prev_indent: Indent) -> Indent {
Indent { actual, visual }
}

pub fn apply_indent(file: &str, args: &Cli) -> (String, Vec<(usize, 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());
let mut verbatim_count = 0;
let mut indent_errs = vec![];

for (i, line) in file.lines().enumerate() {
if RE_VERBATIM_BEGIN.is_match(line) {
Expand All @@ -130,7 +134,14 @@ pub fn apply_indent(file: &str, args: &Cli) -> (String, Vec<(usize, Indent)>) {
);

if (indent.visual < 0) || (indent.actual < 0) {
indent_errs.push((i, indent.clone()))
record_log(
Error,
i,
format!(
"Line {}: indent is negative: {}{:.50}",
i, WHITE, line
),
);
}

if !args.debug {
Expand Down Expand Up @@ -159,5 +170,5 @@ pub fn apply_indent(file: &str, args: &Cli) -> (String, Vec<(usize, Indent)>) {
}
}

(new_file, indent_errs)
new_file
}
52 changes: 49 additions & 3 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
use crate::colors::*;
use crate::print::*;
use crate::Cli;
use env_logger::Builder;
use log::Level;
use log::Level::{Debug, Error, Info, Trace, Warn};
use log::LevelFilter;
use once_cell::sync::Lazy;
use std::collections::HashSet;
use std::io::Write;
use std::sync::Mutex;

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,
}

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 All @@ -36,3 +51,34 @@ 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);

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),
}
}
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ fn main() {
print_script_name();

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

if args.verbose {
print_file_name(filename);
print_filename(filename);
}

if !check_extension_valid(filename) {
Expand All @@ -58,5 +60,7 @@ fn main() {
} else {
write_file(filename, &new_file);
}

print_logs(filename);
}
}
4 changes: 2 additions & 2 deletions src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub fn print_script_name() {
println!("{}", String::new() + PINK + "tex-fmt" + RESET);
}

pub fn print_file_name(filename: &str) {
println!("{}", String::new() + YELLOW + filename + RESET);
pub fn print_filename(filename: &str) {
println!("{}", String::new() + PURPLE + filename + RESET);
}

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

0 comments on commit 3b018cf

Please sign in to comment.