Skip to content

Commit

Permalink
More logging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 18, 2024
1 parent d2c61a8 commit 28e6f5c
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 100 deletions.
4 changes: 4 additions & 0 deletions notes.org
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
** Handle list items and comments properly
** Handle verbatim and ignores better
** Only print ignore warnings once
* Logging
** Store all logs on the fly in a vector
** Print them all at the end in an intelligent way
** Avoid panics to make sure we always reach the end
43 changes: 32 additions & 11 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,60 @@
use crate::indent::*;
use crate::logging::*;
use crate::subs::*;
use crate::wrap::*;
use crate::Cli;
use log::Level::Error;

const MAX_TRIES: u8 = 10;
const MAX_PASS: usize = 10;

fn apply_passes(file: &str, filename: &str, args: &Cli) -> String {
let mut new_file = apply_indent(file, filename, args);
fn apply_passes(
file: &str,
filename: &str,
args: &Cli,
logs: &mut Vec<Log>,
) -> String {
let mut new_file = apply_indent(file, filename, args, logs, Some(1));
let mut finished = false;
let mut tries = 0;
let mut pass = 2;

while needs_wrap(&new_file) && !finished && tries < MAX_TRIES {
while needs_wrap(&new_file) && !finished && pass < MAX_PASS + 2 {
let old_file = new_file.clone();
new_file = wrap(&new_file, filename);
new_file = wrap(&new_file, filename, logs, Some(pass));
new_file = remove_trailing_spaces(&new_file);
new_file = apply_indent(&new_file, filename, args);
tries += 1;
new_file = apply_indent(&new_file, filename, args, logs, Some(pass));
pass += 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");
//log::error!("");
record_log(
logs,
Error,
None,
filename.to_string(),
None,
None,
"Indent does not return to zero at end of file.".to_string(),
);
}

new_file
}

pub fn format_file(file: &str, filename: &str, args: &Cli) -> String {
pub fn format_file(
file: &str,
filename: &str,
args: &Cli,
logs: &mut Vec<Log>,
) -> 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, filename, args);
new_file = apply_passes(&new_file, filename, args, logs);
new_file
}
46 changes: 24 additions & 22 deletions src/ignore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::colors::*;
use crate::logging::*;
use log::Level::Error;

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

pub fn get_ignore(
line: &str,
i: usize,
linum: usize,
ignore: Ignore,
filename: &str,
logs: &mut Vec<Log>,
pass: Option<usize>,
warn: bool,
) -> Ignore {
let skip = contains_ignore_skip(line);
let start = contains_ignore_start(line);
Expand All @@ -33,30 +37,28 @@ pub fn get_ignore(
if start {
block = true
}
if end {
log::warn!(
"{}tex-fmt {}{}: {}Line {}. \
{}No ignore block to end: \
{}{:.50}",
PINK,
PURPLE,
filename,
WHITE,
i,
YELLOW,
RESET,
line,
if end && warn {
record_log(
logs,
Error,
pass,
filename.to_string(),
Some(linum),
Some(line.to_string()),
"No ignore block to end:".to_string(),
);
}
} else {
// currently in ignore block
if start {
log::warn!(
"Line {}: cannot start ignore block \
before ending previous block: {}{:.50}",
i,
WHITE,
line
if start && warn {
record_log(
logs,
Error,
pass,
filename.to_string(),
Some(linum),
Some(line.to_string()),
"Cannot start ignore block:".to_string(),
);
}
if end {
Expand Down
59 changes: 36 additions & 23 deletions src/indent.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::colors::*;
//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::{Info, Warn};

const OPENS: [char; 3] = ['(', '[', '{'];
const CLOSES: [char; 3] = [')', ']', '}'];
Expand Down Expand Up @@ -102,8 +104,22 @@ fn get_indent(line: &str, prev_indent: Indent) -> Indent {
Indent { actual, visual }
}

pub fn apply_indent(file: &str, filename: &str, args: &Cli) -> String {
log::info!("Indenting file");
pub fn apply_indent(
file: &str,
filename: &str,
args: &Cli,
logs: &mut Vec<Log>,
pass: Option<usize>,
) -> String {
record_log(
logs,
Info,
pass,
filename.to_string(),
None,
None,
format!("Indent pass {}.", pass.unwrap()),
);

let mut indent = Indent::new();
let mut ignore = Ignore::new();
Expand All @@ -114,34 +130,31 @@ pub fn apply_indent(file: &str, filename: &str, args: &Cli) -> String {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
ignore = get_ignore(line, linum, ignore, filename);
ignore = get_ignore(line, linum, ignore, filename, logs, pass, true);
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 = {}:{} {}",
linum,
indent.actual,
indent.visual,
WHITE,
line,
record_log(
logs,
Info,
pass,
filename.to_string(),
Some(linum),
Some(line.to_string()),
format!("Indent: actual = {}, visual = {}", indent.actual, indent.visual),
);

if (indent.visual < 0) || (indent.actual < 0) {
log::warn!(
"{}tex-fmt {}{}: {}Line {}. \
{}Indent is negative: \
{}{:.50}",
PINK,
PURPLE,
filename,
WHITE,
linum,
YELLOW,
RESET,
line,
record_log(
logs,
Warn,
pass,
filename.to_string(),
Some(linum),
Some(line.to_string()),
"Indent is negative.".to_string(),
);
}

Expand Down
76 changes: 76 additions & 0 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,40 @@ use log::Level;
use log::Level::{Error, Info, Warn};
use log::LevelFilter;
use std::io::Write;
use std::time::Instant;
use std::path::Path;

#[derive(Debug)]
pub struct Log {
pub level: Level,
pub pass: Option<usize>,
pub time: Instant,
pub filename: String,
pub linum: Option<usize>,
pub line: Option<String>,
pub message: String,
}

pub fn record_log(
logs: &mut Vec<Log>,
level: Level,
pass: Option<usize>,
filename: String,
linum: Option<usize>,
line: Option<String>,
message: String,
) {
let log = Log {
level,
pass,
time: Instant::now(),
filename,
linum,
line,
message,
};
logs.push(log);
}

fn get_log_style(log_level: Level) -> String {
match log_level {
Expand Down Expand Up @@ -37,3 +71,45 @@ pub fn init_logger(args: &Cli) {
})
.init();
}

pub fn print_logs(args: &Cli, mut logs: Vec<Log>) {

if get_log_level(args) == LevelFilter::Warn {
let max_pass = &logs.iter().map(|l| l.pass).max().unwrap();
logs.retain(|l| l.pass == *max_pass || l.pass == None);
}

logs.sort_by_key(|l| l.time);

for log in logs {
let linum = match log.linum {
Some(i) => format!("Line {}. ", i),
None => "".to_string(),
};

let line = match &log.line {
Some(l) => l.to_string(),
None => "".to_string(),
};

let log_string = format!(
"{}tex-fmt {}{}: {}{}{}{} {}{:.50}",
PINK,
PURPLE,
Path::new(&log.filename).file_name().unwrap().to_str().unwrap(),
WHITE,
linum,
YELLOW,
log.message,
RESET,
line,
);

match log.level {
Error => log::error!("{}", log_string),
Warn => log::warn!("{}", log_string),
Info => log::info!("{}", log_string),
_ => panic!(),
}
}
}
Loading

0 comments on commit 28e6f5c

Please sign in to comment.