This repository was archived by the owner on Aug 16, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +25
-4
lines changed Expand file tree Collapse file tree 3 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,9 @@ travis-ci = { repository = "rust-lang-nursery/error-chain" }
2020
2121[features ]
2222default = [" backtrace" , " example_generated" ]
23+ quickmain_log = [" log" ]
2324example_generated = []
2425
2526[dependencies ]
2627backtrace = { version = " 0.3" , optional = true }
28+ log = {version = " 0.3" , optional = true }
Original file line number Diff line number Diff line change 540540#[ cfg( feature = "backtrace" ) ]
541541extern crate backtrace;
542542
543+ #[ cfg( feature = "quickmain_log" ) ]
544+ #[ macro_use]
545+ extern crate log;
546+
543547use std:: error;
544548use std:: iter:: Iterator ;
545549#[ cfg( feature = "backtrace" ) ]
@@ -559,6 +563,7 @@ mod error_chain;
559563#[ macro_use]
560564mod quick_main;
561565pub use quick_main:: ExitCode ;
566+ pub use quick_main:: print_quickmain_error;
562567#[ cfg( feature = "example_generated" ) ]
563568pub mod example_generated;
564569
Original file line number Diff line number Diff line change 4242macro_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+ }
You can’t perform that action at this time.
0 commit comments