From a75a4fc12e2c3175fa55fb6404d2704506648925 Mon Sep 17 00:00:00 2001 From: rusterize Date: Fri, 3 Nov 2017 21:01:50 -0400 Subject: [PATCH 1/7] add feature to use the log crate --- Cargo.toml | 1 + examples/all.rs | 3 ++ examples/chain_err.rs | 3 ++ examples/doc.rs | 3 ++ examples/quickstart.rs | 3 ++ examples/size.rs | 3 ++ src/bin/has_backtrace.rs | 5 ++ src/error_chain.rs | 46 ++++++++++++++++- src/lib.rs | 41 +++++++++++++++ src/log_ext.rs | 106 +++++++++++++++++++++++++++++++++++++++ src/quick_main.rs | 4 ++ tests/log_ext.rs | 79 +++++++++++++++++++++++++++++ tests/quick_main.rs | 3 ++ tests/tests.rs | 4 ++ 14 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 src/log_ext.rs create mode 100644 tests/log_ext.rs diff --git a/Cargo.toml b/Cargo.toml index 34122badb..6b0dbcb08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,4 @@ example_generated = [] [dependencies] backtrace = { version = "0.3.3", optional = true } +log = { version = "0.3", optional = true } diff --git a/examples/all.rs b/examples/all.rs index ccc3ab703..efb5cbfb3 100644 --- a/examples/all.rs +++ b/examples/all.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "log")] +#[macro_use] +extern crate log; #[macro_use] extern crate error_chain; diff --git a/examples/chain_err.rs b/examples/chain_err.rs index bd8effdaf..fc16e9c3b 100644 --- a/examples/chain_err.rs +++ b/examples/chain_err.rs @@ -1,6 +1,9 @@ //! Demonstrates usage of `Error::caused` method. This method enables chaining errors //! like `ResultExt::chain_err` but doesn't require the presence of a `Result` wrapper. +#[cfg(feature = "log")] +#[macro_use] +extern crate log; #[macro_use] extern crate error_chain; diff --git a/examples/doc.rs b/examples/doc.rs index 999ac9cef..ffc8e6f3b 100644 --- a/examples/doc.rs +++ b/examples/doc.rs @@ -2,6 +2,9 @@ //! This module is used to check that all generated items are documented. +#[cfg(feature = "log")] +#[macro_use] +extern crate log; #[macro_use] extern crate error_chain; diff --git a/examples/quickstart.rs b/examples/quickstart.rs index 2e3e2b5d3..1cd846c01 100644 --- a/examples/quickstart.rs +++ b/examples/quickstart.rs @@ -6,6 +6,9 @@ // Import the macro. Don't forget to add `error-chain` in your // `Cargo.toml`! +#[cfg(feature = "log")] +#[macro_use] +extern crate log; #[macro_use] extern crate error_chain; diff --git a/examples/size.rs b/examples/size.rs index 3e180684e..7a2846b43 100644 --- a/examples/size.rs +++ b/examples/size.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "log")] +#[macro_use] +extern crate log; #[macro_use] extern crate error_chain; diff --git a/src/bin/has_backtrace.rs b/src/bin/has_backtrace.rs index c5dac058a..dc8904605 100644 --- a/src/bin/has_backtrace.rs +++ b/src/bin/has_backtrace.rs @@ -2,9 +2,14 @@ //! Used by tests to make sure backtraces are available when they should be. Should not be used //! outside of the tests. +#[cfg(feature = "log")] +#[macro_use] +extern crate log; #[macro_use] extern crate error_chain; + + error_chain! { errors { MyError diff --git a/src/error_chain.rs b/src/error_chain.rs index 9b95a77d3..b1174e4e2 100644 --- a/src/error_chain.rs +++ b/src/error_chain.rs @@ -10,15 +10,17 @@ macro_rules! impl_error_chain_processed { impl_error_chain_processed! { types { Error, ErrorKind, ResultExt, Result; + result_log_ext = ResultLogExt; } $( $rest )* } }; - // With `Result` wrapper. + // with result wrapper and logext ( types { $error_name:ident, $error_kind_name:ident, $result_ext_name:ident, $result_name:ident; + result_log_ext = $result_log_ext_name:ident; } $( $rest: tt )* ) => { @@ -26,6 +28,44 @@ macro_rules! impl_error_chain_processed { types { $error_name, $error_kind_name, $result_ext_name; + result_log_ext = $result_log_ext_name; + } + $( $rest )* + } + /// Convenient wrapper around `std::Result`. + #[allow(unused)] + pub type $result_name = ::std::result::Result; + }; + // without result wrapper and no logext + ( + types { + $error_name:ident, $error_kind_name:ident, + $result_ext_name:ident; + } + $( $rest: tt )* + ) => { + impl_error_chain_processed! { + types { + $error_name, $error_kind_name, + $result_ext_name; + result_log_ext = ResultLogExt3; + } + $( $rest )* + } + }; + // With `Result` wrapper no log ext + ( + types { + $error_name:ident, $error_kind_name:ident, + $result_ext_name:ident, $result_name:ident; + } + $( $rest: tt )* + ) => { + impl_error_chain_processed! { + types { + $error_name, $error_kind_name, + $result_ext_name; + result_log_ext = ResultLogExti2; } $( $rest )* } @@ -38,6 +78,7 @@ macro_rules! impl_error_chain_processed { types { $error_name:ident, $error_kind_name:ident, $result_ext_name:ident; + result_log_ext = $result_log_ext_name:ident; } links { @@ -342,6 +383,9 @@ macro_rules! impl_error_chain_processed { } + #[cfg(feature = "log")] + impl_result_log_ext!{ $result_log_ext_name , $error_name } + }; } diff --git a/src/lib.rs b/src/lib.rs index ac185c643..7d12ad11a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,6 +101,8 @@ //! define an `errors` module and inside it call [`error_chain!`]: //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! mod other_error { //! error_chain! {} @@ -182,6 +184,8 @@ //! Introducing new error chains, with a string message: //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} //! # error_chain! {} @@ -193,6 +197,8 @@ //! Introducing new error chains, with an [`ErrorKind`]: //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} //! error_chain! { @@ -216,6 +222,8 @@ //! So the below is equivalent to the previous: //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} //! # error_chain! { errors { FooError } } @@ -237,6 +245,8 @@ //! With [`bail!`] the previous examples look like: //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} //! # error_chain! { errors { FooError } } @@ -264,6 +274,8 @@ //! To extend the error chain: //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} //! # error_chain! {} @@ -290,6 +302,8 @@ //! To chain an error directly, use [`with_chain`]: //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} //! # error_chain! {} @@ -306,6 +320,8 @@ //! To convert an error from another error chain to this error chain: //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} //! # mod other { error_chain! {} } @@ -334,6 +350,8 @@ //! making dispatching on error kinds relatively compact: //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() { //! error_chain! { @@ -356,6 +374,8 @@ //! Chained errors are also matched with (relatively) compact syntax //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! mod utils { //! error_chain! { @@ -390,6 +410,8 @@ //! of causing errors. For reporting purposes, this information can be accessed as follows. //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! use error_chain::ChainedError; // for e.display_chain() //! @@ -466,6 +488,8 @@ //! within your own project. //! //! ``` +//! # #[cfg(feature = "log")] +//! # #[macro_use] extern crate log; //! # #[macro_use] //! # extern crate error_chain; //! # mod errors { @@ -540,6 +564,13 @@ use std::error; use std::iter::Iterator; use std::fmt; +#[cfg(feature = "log")] +#[macro_use] +extern crate log; + +#[macro_use] +mod log_ext; + #[macro_use] mod impl_error_chain_kind; #[macro_use] @@ -692,6 +723,8 @@ impl State { /// `bail!(expr)` is equivalent to writing. /// /// ``` +/// # #[cfg(feature = "log")] +/// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # error_chain! { } /// # fn main() { } @@ -704,6 +737,8 @@ impl State { /// And as shorthand it takes a formatting string a la `println!`: /// /// ``` +/// # #[cfg(feature = "log")] +/// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # error_chain! { } /// # fn main() { } @@ -718,6 +753,8 @@ impl State { /// Bailing on a custom error: /// /// ``` +/// # #[cfg(feature = "log")] +/// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # fn main() {} /// error_chain! { @@ -738,6 +775,8 @@ impl State { /// Bailing on a formatted string: /// /// ``` +/// # #[cfg(feature = "log")] +/// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # fn main() {} /// error_chain! { } @@ -770,6 +809,8 @@ macro_rules! bail { /// As an example, `ensure!(condition, "error code: {}", errcode)` is equivalent to /// /// ``` +/// # #[cfg(feature = "log")] +/// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # error_chain! { } /// # fn main() { } diff --git a/src/log_ext.rs b/src/log_ext.rs new file mode 100644 index 000000000..9586a209e --- /dev/null +++ b/src/log_ext.rs @@ -0,0 +1,106 @@ +#[cfg(feature = "log")] +#[macro_use] +mod impl_log_ext { +#[macro_export] +macro_rules! impl_result_log_ext { + ( $result_log_ext_name:ident, $error_name:ident ) => ( + /// Extend chained errors to be able to log them on the spot using the log crate. + /// See [`loge`], [`logw`], [`logi`], [`logd`], [`logt`] functions. + pub trait $result_log_ext_name { + /// Log error using the log crate `error!` macro. + fn loge(self) -> Self; + /// Log error using the log crate `warn!` macro. + fn logw(self) -> Self; + /// Log error using the log crate `info!` macro. + fn logi(self) -> Self; + /// Log error using the log crate `debug!` macro. + fn logd(self) -> Self; + /// Log error using the log crate `trace!` macro. + fn logt(self) -> Self; + } + + impl $result_log_ext_name for ::std::result::Result + { + impl_make_log_fn_for_result!( loge, error, Error); + impl_make_log_fn_for_result!( logw, warn, Warn); + impl_make_log_fn_for_result!( logi, info, Info); + impl_make_log_fn_for_result!( logd, debug, Debug); + impl_make_log_fn_for_result!( logt, trace, Trace); + } + + impl $result_log_ext_name for $error_name + { + impl_make_log_fn_for_chained_error!( loge, error, Error); + impl_make_log_fn_for_chained_error!( logw, warn, Warn); + impl_make_log_fn_for_chained_error!( logi, info, Info); + impl_make_log_fn_for_chained_error!( logd, debug, Debug); + impl_make_log_fn_for_chained_error!( logt, trace, Trace); + } + + ) +} + +/// Internal macro used to implement the logX() functions +/// It logs the causes of the error using the specified log crate level: +/// For example: +/// +/// `log_causes!(err,info)` +// #[cfg(feature = "log")] +#[macro_export] +macro_rules! impl_log_causes { + ($e:expr, $level:ident) => ( + for c in $e.iter().skip(1) { + $level!(" caused by: {}", c); + } + + if let Some(backtrace) = $e.backtrace() { + $level!("backtrace: {:?}", backtrace); + } + ) +} + +/// Internal macro used to implement the logX() functions +/// It generates a function that logs the error and its causes +/// using the specified log crate level. +/// 1st argument -$name: Function name +/// 2nd argument -$level: log macro to use (error, warn, info, debug, trace) +/// 3nd argument -$lvlchk: Do not execute the code +/// if logging is not enabled for this level +/// (Error, Warnm, Indo, Debug, Trace) +/// +/// For example: +/// +/// `log_error!(err,info,Info)` +#[cfg(feature = "log")] +#[macro_export] +macro_rules! impl_make_log_fn_for_result { + ($name:ident, $level:ident, $lvlchk:ident) => ( + fn $name(self) -> Self { + use log; + if let Err(ref e) = self { + if log_enabled!(log::LogLevel::$lvlchk) { + $level!("{}", e); + impl_log_causes!(e,$level); + } + }; + self + } + ) +} + +/// Internal implementation macro for logging the chained error type +#[cfg(feature = "log")] +#[macro_export] +macro_rules! impl_make_log_fn_for_chained_error { + ($name:ident, $level:ident, $lvlchk:ident) => ( + fn $name(self) -> Self { + use log; + if log_enabled!(log::LogLevel::$lvlchk) { + $level!("{}", self); + impl_log_causes!(self,$level); + }; + self + } + ) +} +} diff --git a/src/quick_main.rs b/src/quick_main.rs index f81e7d704..faee476ac 100644 --- a/src/quick_main.rs +++ b/src/quick_main.rs @@ -38,6 +38,10 @@ /// Err("error".into()) /// } /// ``` +#[cfg(features = "log")] +#[macro_use] +extern crate log; + #[macro_export] macro_rules! quick_main { ($main:expr) => { diff --git a/tests/log_ext.rs b/tests/log_ext.rs new file mode 100644 index 000000000..ffd7f30bb --- /dev/null +++ b/tests/log_ext.rs @@ -0,0 +1,79 @@ +#![allow(dead_code)] + +#[cfg(feature = "log")] +#[macro_use] +extern crate log; + +#[cfg(feature = "log")] +#[macro_use] +extern crate error_chain; + +#[cfg(feature = "log")] +#[cfg(test)] +mod log_ext_tests { + + #[test] + fn logext_macro_call_for_error() { + macro_rules! check { + ( $what:ident , $fun:expr ) => ( + match $what { + Error(ErrorKind::Msg(_), ..) => (), + _ => panic!("{} did not return an error: {:?}",$fun, $what) + } + ) + } + error_chain! { + errors { + Test + } + } + let msg1 = "My test error"; + let msg2 = "My test warn"; + let msg3 = "My test info"; + let msg4 = "My test debug"; + let msg5 = "My test trace"; + let base = || Error::from(ErrorKind::Test); + let erre = base().chain_err(|| msg1).loge(); + let errw = base().chain_err(|| msg2).logw(); + let erri = base().chain_err(|| msg3).logi(); + let errd = base().chain_err(|| msg4).logd(); + let errt = base().chain_err(|| msg5).logt(); + + check!(erre,"loge"); + check!(errw,"logw"); + check!(erri,"logi"); + check!(errd,"logd"); + check!(errt,"logt"); + } + + #[test] + fn logext_macro_call_for_result() { + macro_rules! check { + ( $what:ident , $fun:expr) => ( + match $what { + Err(Error(..)) => (), + _ => panic!("{} did not return a result type!",$fun) + } + ) + } + error_chain! { + errors { + Test + } + + } + + let base: fn() -> Result<()> = || Err( Error::from(ErrorKind::Test) ); + + let rese = base().chain_err(|| "My test error").loge(); + let resw = base().chain_err(|| "My test warn").logw(); + let resi = base().chain_err(|| "My test info").logi(); + let resd = base().chain_err(|| "My test debug").logd(); + let rest = base().chain_err(|| "My test trace").logt(); + check!(rese,"loge"); + check!(resw,"logw"); + check!(resi,"logi"); + check!(resd,"logd"); + check!(rest,"logt"); + } +} diff --git a/tests/quick_main.rs b/tests/quick_main.rs index 4ada3b4e0..25c00d9dc 100644 --- a/tests/quick_main.rs +++ b/tests/quick_main.rs @@ -1,4 +1,7 @@ #![allow(dead_code)] +#[cfg(feature = "log")] +#[macro_use] +extern crate log; #[macro_use] extern crate error_chain; diff --git a/tests/tests.rs b/tests/tests.rs index 0e38e5448..5661a3a5e 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,5 +1,9 @@ #![allow(dead_code)] +#[cfg(feature = "log")] +#[macro_use] +extern crate log; + #[macro_use] extern crate error_chain; From 38ad82251c0618f83fd97642ee98344af2195686 Mon Sep 17 00:00:00 2001 From: rusterize Date: Fri, 3 Nov 2017 21:03:11 -0400 Subject: [PATCH 2/7] ignore rusty-tags.vi --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 98171f1fe..c1dc5c38e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ target/ -Cargo.lock \ No newline at end of file +Cargo.lock +rusty-tags.vi From 7b97d3fdc6acbe24c549a9ae5f82e73a491a9f57 Mon Sep 17 00:00:00 2001 From: rusterize Date: Fri, 3 Nov 2017 22:59:11 -0400 Subject: [PATCH 3/7] change feature name to "logging" --- Cargo.toml | 1 + examples/all.rs | 2 +- examples/chain_err.rs | 2 +- examples/doc.rs | 2 +- examples/quickstart.rs | 2 +- examples/size.rs | 2 +- src/bin/has_backtrace.rs | 2 +- src/error_chain.rs | 2 +- src/lib.rs | 36 ++++++++++++++++++------------------ src/log_ext.rs | 8 ++++---- src/quick_main.rs | 2 +- tests/log_ext.rs | 6 +++--- tests/quick_main.rs | 2 +- tests/tests.rs | 2 +- 14 files changed, 36 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6b0dbcb08..212f8855f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ travis-ci = { repository = "rust-lang-nursery/error-chain" } [features] default = ["backtrace", "example_generated"] example_generated = [] +logging = ["log"] [dependencies] backtrace = { version = "0.3.3", optional = true } diff --git a/examples/all.rs b/examples/all.rs index efb5cbfb3..ef69dd2f1 100644 --- a/examples/all.rs +++ b/examples/all.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; #[macro_use] diff --git a/examples/chain_err.rs b/examples/chain_err.rs index fc16e9c3b..2e56b930c 100644 --- a/examples/chain_err.rs +++ b/examples/chain_err.rs @@ -1,7 +1,7 @@ //! Demonstrates usage of `Error::caused` method. This method enables chaining errors //! like `ResultExt::chain_err` but doesn't require the presence of a `Result` wrapper. -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; #[macro_use] diff --git a/examples/doc.rs b/examples/doc.rs index ffc8e6f3b..70ea8c8d8 100644 --- a/examples/doc.rs +++ b/examples/doc.rs @@ -2,7 +2,7 @@ //! This module is used to check that all generated items are documented. -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; #[macro_use] diff --git a/examples/quickstart.rs b/examples/quickstart.rs index 1cd846c01..899236192 100644 --- a/examples/quickstart.rs +++ b/examples/quickstart.rs @@ -6,7 +6,7 @@ // Import the macro. Don't forget to add `error-chain` in your // `Cargo.toml`! -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; #[macro_use] diff --git a/examples/size.rs b/examples/size.rs index 7a2846b43..486eb49a0 100644 --- a/examples/size.rs +++ b/examples/size.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; #[macro_use] diff --git a/src/bin/has_backtrace.rs b/src/bin/has_backtrace.rs index dc8904605..484a6345e 100644 --- a/src/bin/has_backtrace.rs +++ b/src/bin/has_backtrace.rs @@ -2,7 +2,7 @@ //! Used by tests to make sure backtraces are available when they should be. Should not be used //! outside of the tests. -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; #[macro_use] diff --git a/src/error_chain.rs b/src/error_chain.rs index b1174e4e2..5923784e6 100644 --- a/src/error_chain.rs +++ b/src/error_chain.rs @@ -383,7 +383,7 @@ macro_rules! impl_error_chain_processed { } - #[cfg(feature = "log")] + #[cfg(feature = "logging")] impl_result_log_ext!{ $result_log_ext_name , $error_name } }; diff --git a/src/lib.rs b/src/lib.rs index 7d12ad11a..05c828bd3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,7 +101,7 @@ //! define an `errors` module and inside it call [`error_chain!`]: //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! mod other_error { @@ -184,7 +184,7 @@ //! Introducing new error chains, with a string message: //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -197,7 +197,7 @@ //! Introducing new error chains, with an [`ErrorKind`]: //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -222,7 +222,7 @@ //! So the below is equivalent to the previous: //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -245,7 +245,7 @@ //! With [`bail!`] the previous examples look like: //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -274,7 +274,7 @@ //! To extend the error chain: //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -302,7 +302,7 @@ //! To chain an error directly, use [`with_chain`]: //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -320,7 +320,7 @@ //! To convert an error from another error chain to this error chain: //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -350,7 +350,7 @@ //! making dispatching on error kinds relatively compact: //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() { @@ -374,7 +374,7 @@ //! Chained errors are also matched with (relatively) compact syntax //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! mod utils { @@ -410,7 +410,7 @@ //! of causing errors. For reporting purposes, this information can be accessed as follows. //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! use error_chain::ChainedError; // for e.display_chain() @@ -488,7 +488,7 @@ //! within your own project. //! //! ``` -//! # #[cfg(feature = "log")] +//! # #[cfg(feature = "logging")] //! # #[macro_use] extern crate log; //! # #[macro_use] //! # extern crate error_chain; @@ -564,7 +564,7 @@ use std::error; use std::iter::Iterator; use std::fmt; -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; @@ -723,7 +723,7 @@ impl State { /// `bail!(expr)` is equivalent to writing. /// /// ``` -/// # #[cfg(feature = "log")] +/// # #[cfg(feature = "logging")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # error_chain! { } @@ -737,7 +737,7 @@ impl State { /// And as shorthand it takes a formatting string a la `println!`: /// /// ``` -/// # #[cfg(feature = "log")] +/// # #[cfg(feature = "logging")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # error_chain! { } @@ -753,7 +753,7 @@ impl State { /// Bailing on a custom error: /// /// ``` -/// # #[cfg(feature = "log")] +/// # #[cfg(feature = "logging")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # fn main() {} @@ -775,7 +775,7 @@ impl State { /// Bailing on a formatted string: /// /// ``` -/// # #[cfg(feature = "log")] +/// # #[cfg(feature = "logging")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # fn main() {} @@ -809,7 +809,7 @@ macro_rules! bail { /// As an example, `ensure!(condition, "error code: {}", errcode)` is equivalent to /// /// ``` -/// # #[cfg(feature = "log")] +/// # #[cfg(feature = "logging")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # error_chain! { } diff --git a/src/log_ext.rs b/src/log_ext.rs index 9586a209e..a61ddbb3e 100644 --- a/src/log_ext.rs +++ b/src/log_ext.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] mod impl_log_ext { #[macro_export] @@ -45,7 +45,7 @@ macro_rules! impl_result_log_ext { /// For example: /// /// `log_causes!(err,info)` -// #[cfg(feature = "log")] +// #[cfg(feature = "logging")] #[macro_export] macro_rules! impl_log_causes { ($e:expr, $level:ident) => ( @@ -71,7 +71,7 @@ macro_rules! impl_log_causes { /// For example: /// /// `log_error!(err,info,Info)` -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_export] macro_rules! impl_make_log_fn_for_result { ($name:ident, $level:ident, $lvlchk:ident) => ( @@ -89,7 +89,7 @@ macro_rules! impl_make_log_fn_for_result { } /// Internal implementation macro for logging the chained error type -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_export] macro_rules! impl_make_log_fn_for_chained_error { ($name:ident, $level:ident, $lvlchk:ident) => ( diff --git a/src/quick_main.rs b/src/quick_main.rs index faee476ac..d6005d419 100644 --- a/src/quick_main.rs +++ b/src/quick_main.rs @@ -38,7 +38,7 @@ /// Err("error".into()) /// } /// ``` -#[cfg(features = "log")] +#[cfg(features = "logging")] #[macro_use] extern crate log; diff --git a/tests/log_ext.rs b/tests/log_ext.rs index ffd7f30bb..26212c43f 100644 --- a/tests/log_ext.rs +++ b/tests/log_ext.rs @@ -1,14 +1,14 @@ #![allow(dead_code)] -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate error_chain; -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[cfg(test)] mod log_ext_tests { diff --git a/tests/quick_main.rs b/tests/quick_main.rs index 25c00d9dc..4c3d9b3f6 100644 --- a/tests/quick_main.rs +++ b/tests/quick_main.rs @@ -1,5 +1,5 @@ #![allow(dead_code)] -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; #[macro_use] diff --git a/tests/tests.rs b/tests/tests.rs index 5661a3a5e..67654c2a7 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -#[cfg(feature = "log")] +#[cfg(feature = "logging")] #[macro_use] extern crate log; From efcfc7dab10c786336ccc39b57b742e9caa8a856 Mon Sep 17 00:00:00 2001 From: rusterize Date: Fri, 3 Nov 2017 23:10:44 -0400 Subject: [PATCH 4/7] Revert 'change feature name to "logging"' This reverts commit 7b97d3fdc6acbe24c549a9ae5f82e73a491a9f57. --- Cargo.toml | 1 - examples/all.rs | 2 +- examples/chain_err.rs | 2 +- examples/doc.rs | 2 +- examples/quickstart.rs | 2 +- examples/size.rs | 2 +- src/bin/has_backtrace.rs | 2 +- src/error_chain.rs | 2 +- src/lib.rs | 36 ++++++++++++++++++------------------ src/log_ext.rs | 8 ++++---- src/quick_main.rs | 2 +- tests/log_ext.rs | 6 +++--- tests/quick_main.rs | 2 +- tests/tests.rs | 2 +- 14 files changed, 35 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 212f8855f..6b0dbcb08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,6 @@ travis-ci = { repository = "rust-lang-nursery/error-chain" } [features] default = ["backtrace", "example_generated"] example_generated = [] -logging = ["log"] [dependencies] backtrace = { version = "0.3.3", optional = true } diff --git a/examples/all.rs b/examples/all.rs index ef69dd2f1..efb5cbfb3 100644 --- a/examples/all.rs +++ b/examples/all.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; #[macro_use] diff --git a/examples/chain_err.rs b/examples/chain_err.rs index 2e56b930c..fc16e9c3b 100644 --- a/examples/chain_err.rs +++ b/examples/chain_err.rs @@ -1,7 +1,7 @@ //! Demonstrates usage of `Error::caused` method. This method enables chaining errors //! like `ResultExt::chain_err` but doesn't require the presence of a `Result` wrapper. -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; #[macro_use] diff --git a/examples/doc.rs b/examples/doc.rs index 70ea8c8d8..ffc8e6f3b 100644 --- a/examples/doc.rs +++ b/examples/doc.rs @@ -2,7 +2,7 @@ //! This module is used to check that all generated items are documented. -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; #[macro_use] diff --git a/examples/quickstart.rs b/examples/quickstart.rs index 899236192..1cd846c01 100644 --- a/examples/quickstart.rs +++ b/examples/quickstart.rs @@ -6,7 +6,7 @@ // Import the macro. Don't forget to add `error-chain` in your // `Cargo.toml`! -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; #[macro_use] diff --git a/examples/size.rs b/examples/size.rs index 486eb49a0..7a2846b43 100644 --- a/examples/size.rs +++ b/examples/size.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; #[macro_use] diff --git a/src/bin/has_backtrace.rs b/src/bin/has_backtrace.rs index 484a6345e..dc8904605 100644 --- a/src/bin/has_backtrace.rs +++ b/src/bin/has_backtrace.rs @@ -2,7 +2,7 @@ //! Used by tests to make sure backtraces are available when they should be. Should not be used //! outside of the tests. -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; #[macro_use] diff --git a/src/error_chain.rs b/src/error_chain.rs index 5923784e6..b1174e4e2 100644 --- a/src/error_chain.rs +++ b/src/error_chain.rs @@ -383,7 +383,7 @@ macro_rules! impl_error_chain_processed { } - #[cfg(feature = "logging")] + #[cfg(feature = "log")] impl_result_log_ext!{ $result_log_ext_name , $error_name } }; diff --git a/src/lib.rs b/src/lib.rs index 05c828bd3..7d12ad11a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,7 +101,7 @@ //! define an `errors` module and inside it call [`error_chain!`]: //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! mod other_error { @@ -184,7 +184,7 @@ //! Introducing new error chains, with a string message: //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -197,7 +197,7 @@ //! Introducing new error chains, with an [`ErrorKind`]: //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -222,7 +222,7 @@ //! So the below is equivalent to the previous: //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -245,7 +245,7 @@ //! With [`bail!`] the previous examples look like: //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -274,7 +274,7 @@ //! To extend the error chain: //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -302,7 +302,7 @@ //! To chain an error directly, use [`with_chain`]: //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -320,7 +320,7 @@ //! To convert an error from another error chain to this error chain: //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() {} @@ -350,7 +350,7 @@ //! making dispatching on error kinds relatively compact: //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! # fn main() { @@ -374,7 +374,7 @@ //! Chained errors are also matched with (relatively) compact syntax //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! mod utils { @@ -410,7 +410,7 @@ //! of causing errors. For reporting purposes, this information can be accessed as follows. //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] extern crate error_chain; //! use error_chain::ChainedError; // for e.display_chain() @@ -488,7 +488,7 @@ //! within your own project. //! //! ``` -//! # #[cfg(feature = "logging")] +//! # #[cfg(feature = "log")] //! # #[macro_use] extern crate log; //! # #[macro_use] //! # extern crate error_chain; @@ -564,7 +564,7 @@ use std::error; use std::iter::Iterator; use std::fmt; -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; @@ -723,7 +723,7 @@ impl State { /// `bail!(expr)` is equivalent to writing. /// /// ``` -/// # #[cfg(feature = "logging")] +/// # #[cfg(feature = "log")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # error_chain! { } @@ -737,7 +737,7 @@ impl State { /// And as shorthand it takes a formatting string a la `println!`: /// /// ``` -/// # #[cfg(feature = "logging")] +/// # #[cfg(feature = "log")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # error_chain! { } @@ -753,7 +753,7 @@ impl State { /// Bailing on a custom error: /// /// ``` -/// # #[cfg(feature = "logging")] +/// # #[cfg(feature = "log")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # fn main() {} @@ -775,7 +775,7 @@ impl State { /// Bailing on a formatted string: /// /// ``` -/// # #[cfg(feature = "logging")] +/// # #[cfg(feature = "log")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # fn main() {} @@ -809,7 +809,7 @@ macro_rules! bail { /// As an example, `ensure!(condition, "error code: {}", errcode)` is equivalent to /// /// ``` -/// # #[cfg(feature = "logging")] +/// # #[cfg(feature = "log")] /// # #[macro_use] extern crate log; /// # #[macro_use] extern crate error_chain; /// # error_chain! { } diff --git a/src/log_ext.rs b/src/log_ext.rs index a61ddbb3e..9586a209e 100644 --- a/src/log_ext.rs +++ b/src/log_ext.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] mod impl_log_ext { #[macro_export] @@ -45,7 +45,7 @@ macro_rules! impl_result_log_ext { /// For example: /// /// `log_causes!(err,info)` -// #[cfg(feature = "logging")] +// #[cfg(feature = "log")] #[macro_export] macro_rules! impl_log_causes { ($e:expr, $level:ident) => ( @@ -71,7 +71,7 @@ macro_rules! impl_log_causes { /// For example: /// /// `log_error!(err,info,Info)` -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_export] macro_rules! impl_make_log_fn_for_result { ($name:ident, $level:ident, $lvlchk:ident) => ( @@ -89,7 +89,7 @@ macro_rules! impl_make_log_fn_for_result { } /// Internal implementation macro for logging the chained error type -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_export] macro_rules! impl_make_log_fn_for_chained_error { ($name:ident, $level:ident, $lvlchk:ident) => ( diff --git a/src/quick_main.rs b/src/quick_main.rs index d6005d419..faee476ac 100644 --- a/src/quick_main.rs +++ b/src/quick_main.rs @@ -38,7 +38,7 @@ /// Err("error".into()) /// } /// ``` -#[cfg(features = "logging")] +#[cfg(features = "log")] #[macro_use] extern crate log; diff --git a/tests/log_ext.rs b/tests/log_ext.rs index 26212c43f..ffd7f30bb 100644 --- a/tests/log_ext.rs +++ b/tests/log_ext.rs @@ -1,14 +1,14 @@ #![allow(dead_code)] -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate error_chain; -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[cfg(test)] mod log_ext_tests { diff --git a/tests/quick_main.rs b/tests/quick_main.rs index 4c3d9b3f6..25c00d9dc 100644 --- a/tests/quick_main.rs +++ b/tests/quick_main.rs @@ -1,5 +1,5 @@ #![allow(dead_code)] -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; #[macro_use] diff --git a/tests/tests.rs b/tests/tests.rs index 67654c2a7..5661a3a5e 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -#[cfg(feature = "logging")] +#[cfg(feature = "log")] #[macro_use] extern crate log; From 70620c2b55ad0653b2a2ae4707be4abb451c6463 Mon Sep 17 00:00:00 2001 From: rusterize Date: Sat, 4 Nov 2017 01:29:40 -0400 Subject: [PATCH 5/7] add log feature test to travis --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6bbf37eb3..aa9cc3654 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,10 +37,10 @@ env: - TRAVIS_CARGO_NIGHTLY_FEATURE="" - RUSTFLAGS="-D warnings" matrix: - - FEATURES=--features=backtrace + - FEATURES=--features=backtrace,log - FEATURES=--no-default-features matrix: exclude: - - env: FEATURES=--features=backtrace + - env: FEATURES=--features=backtrace,log rust: 1.10.0 From 27b20661c081dc556bc2c7aa453f0de873c98dab Mon Sep 17 00:00:00 2001 From: rusterize Date: Sat, 4 Nov 2017 01:39:34 -0400 Subject: [PATCH 6/7] travis: separate backtrace from log tests --- .travis.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index aa9cc3654..360f49569 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,10 +37,13 @@ env: - TRAVIS_CARGO_NIGHTLY_FEATURE="" - RUSTFLAGS="-D warnings" matrix: - - FEATURES=--features=backtrace,log + - FEATURES=--features=backtrace + - FEATURES=--features=log - FEATURES=--no-default-features matrix: exclude: - - env: FEATURES=--features=backtrace,log + - env: FEATURES=--features=backtrace + rust: 1.10.0 + - env: FEATURES=--features=log rust: 1.10.0 From 475aa96d50ac3ab5c246337270fc33c35115d45d Mon Sep 17 00:00:00 2001 From: rusterize Date: Sat, 4 Nov 2017 02:03:13 -0400 Subject: [PATCH 7/7] fix log test to support rust 1.14 --- tests/log_ext.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/log_ext.rs b/tests/log_ext.rs index ffd7f30bb..8bce8cfd2 100644 --- a/tests/log_ext.rs +++ b/tests/log_ext.rs @@ -32,7 +32,7 @@ mod log_ext_tests { let msg3 = "My test info"; let msg4 = "My test debug"; let msg5 = "My test trace"; - let base = || Error::from(ErrorKind::Test); + fn base() -> Error { Error::from(ErrorKind::Test) } let erre = base().chain_err(|| msg1).loge(); let errw = base().chain_err(|| msg2).logw(); let erri = base().chain_err(|| msg3).logi(); @@ -63,7 +63,7 @@ mod log_ext_tests { } - let base: fn() -> Result<()> = || Err( Error::from(ErrorKind::Test) ); + fn base() -> Result<()> { Err( Error::from(ErrorKind::Test) ) } let rese = base().chain_err(|| "My test error").loge(); let resw = base().chain_err(|| "My test warn").logw();