Skip to content

Commit

Permalink
Merge branch 'logging' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 17, 2024
2 parents e105c8f + fe19a75 commit a0a9e83
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 50 deletions.
6 changes: 2 additions & 4 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//use crate::colors::*;
use crate::indent::*;
use crate::logging::*;
//use crate::logging::*;
use crate::subs::*;
use crate::wrap::*;
use crate::Cli;
Expand Down Expand Up @@ -44,15 +44,13 @@ fn apply_passes(
//Warn,
//i,
//format!(
//" Line {}: cannot be wrapped: {}{:.50}",
//"Line {}: cannot be wrapped: {}{:.50}...",
//i, WHITE, line
//),
//);
//}
//}
//}

logs.append(&mut new_logs);
new_file
}

Expand Down
21 changes: 19 additions & 2 deletions src/ignore.rs
Original file line number Diff line number Diff line change
@@ -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}"];
Expand Down Expand Up @@ -37,9 +37,17 @@ pub fn get_ignore(
block = true
}
if end {
<<<<<<< HEAD
let message = format!(
" Line {}: no ignore block to end: {}{:.50}",
linum, WHITE, line
=======
log::error!(
"Line {}: no ignore block to end: {}{:.50}...",
i,
WHITE,
line
>>>>>>> logging
);
let log = Log {
level: Warn,
Expand All @@ -52,9 +60,18 @@ pub fn get_ignore(
} else {
// currently in ignore block
if start {
<<<<<<< HEAD
let message = format!(
" Line {}: cannot start ignore block before ending: {}{:.50}",
linum, WHITE, line
=======
log::error!(
"Line {}: cannot start ignore block \
before ending previous block: {}{:.50}...",
i,
WHITE,
line
>>>>>>> logging
);
let log = Log {
level: Warn,
Expand Down
18 changes: 6 additions & 12 deletions src/indent.rs
Original file line number Diff line number Diff line change
@@ -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] = [')', ']', '}'];
Expand Down Expand Up @@ -112,8 +110,6 @@ pub fn apply_indent(
) -> 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());
Expand All @@ -139,14 +135,12 @@ pub fn apply_indent(
//);

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

if !args.debug {
Expand Down
69 changes: 43 additions & 26 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
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::LevelFilter;
//use once_cell::sync::Lazy;
//use std::collections::HashSet;
use std::io::Write;
//use std::sync::Mutex;

#[derive(Debug, PartialEq)]
pub struct Log {
pub level: Level,
pub linum: usize,
pub message: String,
pub filename: String,
}
//pub static LOGS: Lazy<Mutex<HashSet<Log>>> =
//Lazy::new(|| Mutex::new(HashSet::new()));

pub fn record_log(logs: &mut Vec<Log>, log: Log) {
logs.push(log);
}
//#[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 {
Expand Down Expand Up @@ -50,18 +52,33 @@ pub fn init_logger(args: &Cli) {
.init();
}

pub fn print_logs(logs: &mut Vec<Log>, filename: &str) {
logs.sort_by_key(|l| l.linum);
if !logs.is_empty() {
print_filename(filename);
for log in logs {
match log.level {
Level::Error => log::error!("{}", log.message),
Level::Warn => log::warn!("{}", log.message),
Level::Info => log::info!("{}", log.message),
Level::Debug => log::debug!("{}", log.message),
Level::Trace => log::trace!("{}", log.message),
}
}
}
}
//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),
//}
//}
//}
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ fn main() {
};

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

for filename in &args.filenames {
let mut logs: Vec<Log> = vec![];
//LOGS.lock().unwrap().clear();

if args.verbose {
print_filename(filename);
Expand All @@ -61,7 +63,6 @@ fn main() {
write_file(filename, &new_file);
}

logs.dedup();
print_logs(&mut 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!("{}", String::new() + PURPLE + filename + RESET);
println!("{}{}{}{}{}", PINK, "tex-fmt ", PURPLE, filename, RESET);
}

pub fn print_file(new_file: &str) {
Expand Down
17 changes: 16 additions & 1 deletion src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ fn find_wrap_point(line: &str) -> Option<usize> {
wrap_point
}

<<<<<<< HEAD
fn wrap_line(line: &str) -> String {
//log::info!("Wrap long line: {}{}", WHITE, line);
=======
fn wrap_line(line: &str, linum: usize) -> String {
log::info!("Wrap long line: {}{}", WHITE, line);
>>>>>>> logging
let mut remaining_line = line.to_string();
let mut new_line = "".to_string();
let mut can_wrap = true;
Expand All @@ -59,6 +64,12 @@ fn wrap_line(line: &str) -> String {
}
None => {
can_wrap = false;
log::error!(
"Line {}: cannot be wrapped: {}{:.50}...",
linum,
WHITE,
line
);
}
}
}
Expand All @@ -76,10 +87,14 @@ pub fn wrap(file: &str, filename: &str, logs: &mut Vec<Log>) -> String {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
<<<<<<< HEAD
ignore = get_ignore(line, linum, filename, ignore, logs);
=======
ignore = get_ignore(line, linum, ignore);
>>>>>>> logging
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);
Expand Down
2 changes: 2 additions & 0 deletions tests/wrap_out.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit a0a9e83

Please sign in to comment.