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

Commit cb05386

Browse files
committed
Added support for quickmain to use log crate.
* Added crate feature `quickmain_log` When `quickmain_log` is enabled, the `quick_main!` entry point writes the terminating error chain to `log::error!` instead of stdout.
1 parent c9b3757 commit cb05386

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ travis-ci = { repository = "rust-lang-nursery/error-chain" }
2020

2121
[features]
2222
default = ["backtrace", "example_generated"]
23+
quickmain_log = ["log"]
2324
example_generated = []
2425

2526
[dependencies]
2627
backtrace = { version = "0.3", optional = true }
28+
log = {version = "0.3", optional = true }

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,10 @@
540540
#[cfg(feature = "backtrace")]
541541
extern crate backtrace;
542542

543+
#[cfg(feature = "quickmain_log")]
544+
#[macro_use]
545+
extern crate log;
546+
543547
use std::error;
544548
use std::iter::Iterator;
545549
#[cfg(feature = "backtrace")]
@@ -559,6 +563,7 @@ mod error_chain;
559563
#[macro_use]
560564
mod quick_main;
561565
pub use quick_main::ExitCode;
566+
pub use quick_main::print_quickmain_error;
562567
#[cfg(feature = "example_generated")]
563568
pub mod example_generated;
564569

src/quick_main.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@
4242
macro_rules! quick_main {
4343
($main:expr) => {
4444
fn main() {
45-
use ::std::io::Write;
46-
4745
::std::process::exit(match $main() {
4846
Ok(ret) => $crate::ExitCode::code(ret),
4947
Err(ref e) => {
50-
write!(&mut ::std::io::stderr(), "{}", $crate::ChainedError::display_chain(e))
51-
.expect("Error writing to stderr");
48+
{ $crate::print_quickmain_error(e); }
5249

5350
1
5451
}
@@ -75,3 +72,20 @@ impl ExitCode for () {
7572
0
7673
}
7774
}
75+
76+
/// When using `quick_main!`, prints the error if the program doesn't terminate successfully.
77+
pub fn print_quickmain_error<K,E: ::ChainedError<ErrorKind=K>>(e: &E) {
78+
print_error_helper(e);
79+
}
80+
81+
#[cfg(not(feature = "quickmain_log"))]
82+
fn print_error_helper<K,E: ::ChainedError<ErrorKind=K>>(e: &E) {
83+
use ::std::io::Write;
84+
write!(&mut ::std::io::stderr(), "{}", ::ChainedError::display_chain(e))
85+
.expect("Error writing to stderr");
86+
}
87+
88+
#[cfg(feature = "quickmain_log")]
89+
fn print_error_helper<K,E: ::ChainedError<ErrorKind=K>>(e: &E) {
90+
{ error!("{}", ::ChainedError::display_chain(e)) }
91+
}

0 commit comments

Comments
 (0)