-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced a new `Logger` trait which can be used to access all log events emitted during compilation. Currently these only include messages emitted by the `@debug` and `@warn` statements. Changes were implemented in a backwards-compatible manner, but the current `Options::quiet` method has been marked as deprecated, as its behavior can be achieved using the `NullLogger` structure. The default logger used is `StdLogger` which writes all log events to standard error. This reflect the default behavior prior to introduction of `Logger`. With these new changes, it is also now possible to properly test the `@debug` and `@warn` statements.
- Loading branch information
Showing
8 changed files
with
174 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use codemap::SpanLoc; | ||
use std::fmt::Debug; | ||
|
||
/// Sink for log messages | ||
pub trait Logger: Debug { | ||
/// Logs message from a `@debug` statement | ||
fn debug(&self, location: SpanLoc, message: &str); | ||
|
||
/// Logs message from a `@warn` statement | ||
fn warning(&self, location: SpanLoc, message: &str); | ||
} | ||
|
||
/// Logs events to standard error | ||
#[derive(Debug)] | ||
pub struct StdLogger; | ||
|
||
impl Logger for StdLogger { | ||
#[inline] | ||
fn debug(&self, location: SpanLoc, message: &str) { | ||
eprintln!( | ||
"{}:{} DEBUG: {}", | ||
location.file.name(), | ||
location.begin.line + 1, | ||
message | ||
); | ||
} | ||
|
||
#[inline] | ||
fn warning(&self, location: SpanLoc, message: &str) { | ||
eprintln!( | ||
"Warning: {}\n ./{}:{}:{}", | ||
message, | ||
location.file.name(), | ||
location.begin.line + 1, | ||
location.begin.column + 1 | ||
); | ||
} | ||
} | ||
|
||
/// Discards all log events | ||
#[derive(Debug)] | ||
pub struct NullLogger; | ||
|
||
impl Logger for NullLogger { | ||
#[inline] | ||
fn debug(&self, _location: SpanLoc, _message: &str) {} | ||
|
||
#[inline] | ||
fn warning(&self, _location: SpanLoc, _message: &str) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,37 @@ | ||
use macros::TestLogger; | ||
|
||
#[macro_use] | ||
mod macros; | ||
|
||
test!(simple_debug, "@debug 2", ""); | ||
test!(simple_debug_with_semicolon, "@debug 2;", ""); | ||
test!( | ||
// todo: test stdout | ||
debug_while_quiet, | ||
"@debug 2;", | ||
"", | ||
grass::Options::default().quiet(true) | ||
); | ||
#[test] | ||
fn simple_debug() { | ||
let input = "@debug 2"; | ||
let logger = TestLogger::default(); | ||
let options = grass::Options::default().logger(&logger); | ||
let output = grass::from_string(input.to_string(), &options).expect(input); | ||
assert_eq!(&output, ""); | ||
assert_eq!(&[String::from("2")], logger.debug_messages().as_slice()); | ||
assert_eq!(&[] as &[String], logger.warning_messages().as_slice()); | ||
} | ||
|
||
#[test] | ||
fn simple_debug_with_semicolon() { | ||
let input = "@debug 2;"; | ||
let logger = TestLogger::default(); | ||
let options = grass::Options::default().logger(&logger); | ||
let output = grass::from_string(input.to_string(), &options).expect(input); | ||
assert_eq!(&output, ""); | ||
assert_eq!(&[String::from("2")], logger.debug_messages().as_slice()); | ||
assert_eq!(&[] as &[String], logger.warning_messages().as_slice()); | ||
} | ||
|
||
#[test] | ||
fn debug_while_quiet() { | ||
let input = "@debug 2;"; | ||
let logger = TestLogger::default(); | ||
let options = grass::Options::default().logger(&logger).quiet(true); | ||
let output = grass::from_string(input.to_string(), &options).expect(input); | ||
assert_eq!(&output, ""); | ||
assert_eq!(&[] as &[String], logger.debug_messages().as_slice()); | ||
assert_eq!(&[] as &[String], logger.warning_messages().as_slice()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,37 @@ | ||
use macros::TestLogger; | ||
|
||
#[macro_use] | ||
mod macros; | ||
|
||
test!(simple_warn, "@warn 2", ""); | ||
test!( | ||
// todo: test stdout | ||
warn_while_quiet, | ||
"@warn 2;", | ||
"", | ||
grass::Options::default().quiet(true) | ||
); | ||
#[test] | ||
fn warn_debug() { | ||
let input = "@warn 2"; | ||
let logger = TestLogger::default(); | ||
let options = grass::Options::default().logger(&logger); | ||
let output = grass::from_string(input.to_string(), &options).expect(input); | ||
assert_eq!(&output, ""); | ||
assert_eq!(&[] as &[String], logger.debug_messages().as_slice()); | ||
assert_eq!(&[String::from("2")], logger.warning_messages().as_slice()); | ||
} | ||
|
||
#[test] | ||
fn simple_warn_with_semicolon() { | ||
let input = "@warn 2;"; | ||
let logger = TestLogger::default(); | ||
let options = grass::Options::default().logger(&logger); | ||
let output = grass::from_string(input.to_string(), &options).expect(input); | ||
assert_eq!(&output, ""); | ||
assert_eq!(&[] as &[String], logger.debug_messages().as_slice()); | ||
assert_eq!(&[String::from("2")], logger.warning_messages().as_slice()); | ||
} | ||
|
||
#[test] | ||
fn warn_while_quiet() { | ||
let input = "@warn 2;"; | ||
let logger = TestLogger::default(); | ||
let options = grass::Options::default().logger(&logger).quiet(true); | ||
let output = grass::from_string(input.to_string(), &options).expect(input); | ||
assert_eq!(&output, ""); | ||
assert_eq!(&[] as &[String], logger.debug_messages().as_slice()); | ||
assert_eq!(&[] as &[String], logger.warning_messages().as_slice()); | ||
} |