Skip to content

Commit

Permalink
Add support for --quiet flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sathwikmatsa authored and emilio committed Oct 12, 2019
1 parent 087d36f commit 603aa2f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use log::*;
pub struct TraceLogger;
pub struct WarnLogger;
pub struct InfoLogger;
pub struct ErrorLogger;

impl TraceLogger {
pub fn init() -> Result<(), SetLoggerError> {
Expand Down Expand Up @@ -58,6 +59,29 @@ impl log::Log for WarnLogger {
}
}

impl ErrorLogger {
pub fn init() -> Result<(), SetLoggerError> {
log::set_logger(&InfoLogger)?;
log::set_max_level(LevelFilter::Error);
Ok(())
}
}
impl log::Log for ErrorLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Error
}

fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
writeln!(io::stderr(), "{}: {}", record.level(), record.args()).unwrap();
}
}

fn flush(&self) {
io::stderr().flush().unwrap();
}
}

impl InfoLogger {
pub fn init() -> Result<(), SetLoggerError> {
log::set_logger(&InfoLogger)?;
Expand Down
19 changes: 15 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ fn main() {
projects that use workspaces.")
.required(false),
)
.arg(
Arg::with_name("quiet")
.short("q")
.long("quiet")
.help("Report errors only.")
.required(false),
)
.get_matches();

if !matches.is_present("out") && matches.is_present("verify") {
Expand All @@ -220,10 +227,14 @@ fn main() {
}

// Initialize logging
match matches.occurrences_of("v") {
0 => logging::WarnLogger::init().unwrap(),
1 => logging::InfoLogger::init().unwrap(),
_ => logging::TraceLogger::init().unwrap(),
if matches.is_present("quiet") {
logging::ErrorLogger::init().unwrap();
} else {
match matches.occurrences_of("v") {
0 => logging::WarnLogger::init().unwrap(),
1 => logging::InfoLogger::init().unwrap(),
_ => logging::TraceLogger::init().unwrap(),
}
}

// Find the input directory
Expand Down

0 comments on commit 603aa2f

Please sign in to comment.