Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Added support for quickmain to use log crate. (Address #165) #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Mask the `quick_error` macro from the doc](https://github.com/rust-lang-nursery/error-chain/pull/210)
- [Make generated `ErrorKind` enums non-exhaustive](https://github.com/rust-lang-nursery/error-chain/pull/193)
- All 0.11.0-rc.2 changes
- [Allow users of `quick_main!` to use the `log` crate](https://github.com/rust-lang-nursery/error-chain/pull/216)

# 0.11.0-rc.2

Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ travis-ci = { repository = "rust-lang-nursery/error-chain" }

[features]
default = ["backtrace", "example_generated"]
quickmain_log = ["log"]
example_generated = []

[dependencies]
backtrace = { version = "0.3", optional = true }
log = {version = "0.3", optional = true }
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@
#[cfg(feature = "backtrace")]
extern crate backtrace;

#[cfg(feature = "quickmain_log")]
#[macro_use]
extern crate log;

use std::error;
use std::iter::Iterator;
#[cfg(feature = "backtrace")]
Expand All @@ -559,6 +563,7 @@ mod error_chain;
#[macro_use]
mod quick_main;
pub use quick_main::ExitCode;
pub use quick_main::print_quickmain_error;
#[cfg(feature = "example_generated")]
pub mod example_generated;

Expand Down
22 changes: 18 additions & 4 deletions src/quick_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@
macro_rules! quick_main {
($main:expr) => {
fn main() {
use ::std::io::Write;

::std::process::exit(match $main() {
Ok(ret) => $crate::ExitCode::code(ret),
Err(ref e) => {
write!(&mut ::std::io::stderr(), "{}", $crate::ChainedError::display_chain(e))
.expect("Error writing to stderr");
{ $crate::print_quickmain_error(e); }

1
}
Expand All @@ -75,3 +72,20 @@ impl ExitCode for () {
0
}
}

/// When using `quick_main!`, prints the error if the program doesn't terminate successfully.
pub fn print_quickmain_error<K,E: ::ChainedError<ErrorKind=K>>(e: &E) {
print_error_helper(e);
}

#[cfg(not(feature = "quickmain_log"))]
fn print_error_helper<K,E: ::ChainedError<ErrorKind=K>>(e: &E) {
use ::std::io::Write;
write!(&mut ::std::io::stderr(), "{}", ::ChainedError::display_chain(e))
.expect("Error writing to stderr");
}

#[cfg(feature = "quickmain_log")]
fn print_error_helper<K,E: ::ChainedError<ErrorKind=K>>(e: &E) {
{ error!("{}", ::ChainedError::display_chain(e)) }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you do one function instead? It should work without the helper