Skip to content

Commit

Permalink
Show line when logging warnings/errors and add reg test. Fixes #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Feb 15, 2023
1 parent c7e86c1 commit b910575
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 20 deletions.
38 changes: 38 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,44 @@ impl Context {
line: None,
}
}

pub fn label(&self) -> Option<String> {
self.path.as_ref().map(|path| {
if self.line_number == 0 {
if self.column_number == 0 {
format!("{}:{}", path.display(), self.line_number)
} else {
format!(
"{}:{}:{}",
path.display(),
self.line_number,
self.column_number
)
}
} else {
path.display().to_string()
}
})
}

pub fn display_line(&self) -> Option<String> {
self.line.as_ref().map(|line| {
let line_number_s = if self.line_number == 0 {
String::new()
} else {
self.line_number.to_string()
};
let pad = " ".repeat(line_number_s.len());
let caret = String::new();
format!(
"{pad} |\n{line_number} | {line}\n{pad} | {caret}\n",
pad = pad,
line_number = line_number_s,
line = line,
caret = caret,
)
})
}
}

impl Default for Context {
Expand Down
48 changes: 28 additions & 20 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,51 @@ use std::fmt;

use super::context::Context;

const INFO: &str = "INFO";
const WARN: &str = "WARN";
const ERROR: &str = "ERROR";
const MAX_SEVERITY_LENGTH: usize = 5;

/// Formatter for all log messages.
fn format_log(msg: impl AsRef<str>, level: &str, context: Option<&Context>) -> String {
// Format log level.
let level_display = format!("{:5}", level);
// Format log level and context label/line.
let level_display = format!("{:0width$}", level, width = MAX_SEVERITY_LENGTH);
let context_label = context
.and_then(|c| c.label())
.map(|l| format!("[{}] ", l))
.unwrap_or_default();

// Format context.
let context_display = match context {
None => String::new(),
Some(context) => match &context.path {
None => String::new(),
Some(path) => {
if context.line_number == 0 {
format!("[{}:{}] ", path.display(), context.line_number)
} else {
format!("[{}] ", path.display())
}
}
},
// Only show the context line if we are logging warnings or errors.
let context_line = if level == "WARN" || level == "ERROR" {
context
.and_then(|c| c.display_line())
.map(|l| format!("\n{}", l))
.unwrap_or_default()
} else {
String::new()
};

// Print the log message.
format!("make: {level_display} {context_display}| {}", msg.as_ref())
// Return the formatted message.
format!(
"make: {level_display} {context_label}| {}{}",
msg.as_ref(),
context_line
)
}

/// Helper to format info.
fn format_info(msg: impl AsRef<str>, context: Option<&Context>) -> String {
format_log(msg, "INFO", context)
format_log(msg, INFO, context)
}

/// Helper to format warnings.
fn format_warn(msg: impl AsRef<str>, context: Option<&Context>) -> String {
format_log(msg, "WARN", context)
format_log(msg, WARN, context)
}

/// Helper to format errors.
fn format_err(msg: impl AsRef<str>, context: Option<&Context>) -> String {
format_log(msg, "ERROR", context)
format_log(msg, ERROR, context)
}

/// Helper to log info to STDERR.
Expand Down
1 change: 1 addition & 0 deletions tests/scenarios/simple_makefiles/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod t1_simple;
mod t2_prereq;
mod t3_invalid_line_type;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a:
echo "bad indentation on this line is intentional" > a
8 changes: 8 additions & 0 deletions tests/scenarios/simple_makefiles/t3_invalid_line_type/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
crate::system_test_cases!(
{
args: &[],
expected_stdout: "",
expected_stderr: "make: ERROR [Makefile] | Invalid line type.\n |\n2 | echo \"bad indentation on this line is intentional\" > a\n | \n\n",
expected_files: &[],
},
);

0 comments on commit b910575

Please sign in to comment.