This repository was archived by the owner on Aug 16, 2021. It is now read-only.
File tree 3 files changed +25
-4
lines changed
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" }
20
20
21
21
[features ]
22
22
default = [" backtrace" , " example_generated" ]
23
+ quickmain_log = [" log" ]
23
24
example_generated = []
24
25
25
26
[dependencies ]
26
27
backtrace = { version = " 0.3" , optional = true }
28
+ log = {version = " 0.3" , optional = true }
Original file line number Diff line number Diff line change 540
540
#[ cfg( feature = "backtrace" ) ]
541
541
extern crate backtrace;
542
542
543
+ #[ cfg( feature = "quickmain_log" ) ]
544
+ #[ macro_use]
545
+ extern crate log;
546
+
543
547
use std:: error;
544
548
use std:: iter:: Iterator ;
545
549
#[ cfg( feature = "backtrace" ) ]
@@ -559,6 +563,7 @@ mod error_chain;
559
563
#[ macro_use]
560
564
mod quick_main;
561
565
pub use quick_main:: ExitCode ;
566
+ pub use quick_main:: print_quickmain_error;
562
567
#[ cfg( feature = "example_generated" ) ]
563
568
pub mod example_generated;
564
569
Original file line number Diff line number Diff line change 42
42
macro_rules! quick_main {
43
43
( $main: expr) => {
44
44
fn main( ) {
45
- use :: std:: io:: Write ;
46
-
47
45
:: std:: process:: exit( match $main( ) {
48
46
Ok ( ret) => $crate:: ExitCode :: code( ret) ,
49
47
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) ; }
52
49
53
50
1
54
51
}
@@ -75,3 +72,20 @@ impl ExitCode for () {
75
72
0
76
73
}
77
74
}
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