Skip to content

Commit

Permalink
Trying new logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 17, 2024
1 parent 3b018cf commit e105c8f
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 144 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";
20 changes: 20 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
enum Error {
kind: ErrorKind,
message: String,
linum: Option<usize>,
filename: String,
}

enum ErrorKind {
FileExtensionError,
FormatFileInfo,
IndentFileInfo,
IndentLineInfo,
IndentNegativeError,
IndentZeroError,
IgnoreEndError,
IgnoreStartError,
WrapFileInfo,
WrapLineInfo,
WrapLineError,
}
64 changes: 39 additions & 25 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,71 @@
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;
//use log::Level::Warn;

const MAX_TRIES: u8 = 10;

fn apply_passes(file: &str, args: &Cli) -> String {
let mut new_file = apply_indent(file, args);
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);
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);
new_file = wrap(&new_file, filename, &mut new_logs);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_indent(&new_file, args);
new_file = apply_indent(&new_file, args, filename, &mut new_logs);
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) {
//record_log(
//Warn,
//i,
//format!(
//" Line {}: cannot be wrapped: {}{:.50}",
//i, WHITE, line
//),
//);
//}
//}
//}

logs.append(&mut new_logs);
new_file
}

pub fn format_file(file: &str, args: &Cli) -> String {
pub fn format_file(
file: &str,
args: &Cli,
filename: &str,
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, args);
new_file = apply_passes(&new_file, args, filename, logs);
new_file
}
43 changes: 27 additions & 16 deletions src/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ impl Ignore {
}
}

pub fn get_ignore(line: &str, i: usize, prev_ignore: Ignore) -> Ignore {
pub fn get_ignore(
line: &str,
linum: usize,
filename: &str,
prev_ignore: Ignore,
logs: &mut Vec<Log>,
) -> Ignore {
let skip = contains_ignore_skip(line);
let start = contains_ignore_start(line);
let end = contains_ignore_end(line);
Expand All @@ -31,27 +37,32 @@ pub fn get_ignore(line: &str, i: usize, prev_ignore: Ignore) -> Ignore {
block = true
}
if end {
record_log(
Warn,
i,
format!(
" Line {}: no ignore block to end: {}{:.50}...",
i, WHITE, line
),
let message = format!(
" Line {}: no ignore block to end: {}{:.50}",
linum, WHITE, line
);
let log = Log {
level: Warn,
linum,
message,
filename: filename.to_string(),
};
record_log(logs, log);
}
} else {
// currently in ignore block
if start {
record_log(
Warn,
i,
format!(
" Line {}: cannot start ignore block \
before ending previous block: {}{:.50}...",
i, WHITE, line
),
let message = format!(
" Line {}: cannot start ignore block before ending: {}{:.50}",
linum, WHITE, line
);
let log = Log {
level: Warn,
linum,
message,
filename: filename.to_string(),
};
record_log(logs, log);
}
if end {
block = false
Expand Down
47 changes: 26 additions & 21 deletions src/indent.rs
Original file line number Diff line number Diff line change
@@ -1,12 +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::Error;
//use log::Level::Error;

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

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

// TODO flush the logs of indent errors first
Expand All @@ -114,34 +119,34 @@ pub fn apply_indent(file: &str, args: &Cli) -> String {
let mut new_file = String::with_capacity(file.len());
let mut verbatim_count = 0;

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, filename, ignore, logs);
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) {
record_log(
Error,
i,
format!(
"Line {}: indent is negative: {}{:.50}",
i, WHITE, line
),
);
//record_log(
//Error,
//i,
//format!(
//"Line {}: indent is negative: {}{:.50}",
//i, WHITE, line
//),
//);
}

if !args.debug {
Expand Down
59 changes: 21 additions & 38 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ 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)]
#[derive(Debug, PartialEq)]
pub struct Log {
level: Level,
linum: usize,
message: String,
pub level: Level,
pub linum: usize,
pub message: String,
pub filename: String,
}

pub fn record_log(logs: &mut Vec<Log>, log: Log) {
logs.push(log);
}

fn get_log_style(log_level: Level) -> String {
match log_level {
Info => CYAN.to_string(),
Warn => YELLOW.to_string(),
Error => RED.to_string(),
Level::Info => CYAN.to_string(),
Level::Warn => YELLOW.to_string(),
Level::Error => RED.to_string(),
_ => panic!(),
}
}
Expand Down Expand Up @@ -52,33 +50,18 @@ 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());
}
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 {
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),
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),
}
}
}
}
11 changes: 6 additions & 5 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 @@ -41,26 +41,27 @@ fn main() {
print_script_name();

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

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);
let new_file = format_file(&file, &args, filename, &mut logs);

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

print_logs(filename);
logs.dedup();
print_logs(&mut logs, filename);
}
}
Loading

0 comments on commit e105c8f

Please sign in to comment.