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

log feature - support use of the log crate #239

Open
wants to merge 7 commits 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*~
target/
Cargo.lock
Cargo.lock
rusty-tags.vi
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ env:
- RUSTFLAGS="-D warnings"
matrix:
- FEATURES=--features=backtrace
- FEATURES=--features=log
- FEATURES=--no-default-features

matrix:
exclude:
- env: FEATURES=--features=backtrace
rust: 1.10.0
- env: FEATURES=--features=log
rust: 1.10.0
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ example_generated = []

[dependencies]
backtrace = { version = "0.3.3", optional = true }
log = { version = "0.3", optional = true }
3 changes: 3 additions & 0 deletions examples/all.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "log")]
#[macro_use]
extern crate log;
#[macro_use]
extern crate error_chain;

Expand Down
3 changes: 3 additions & 0 deletions examples/chain_err.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
3 changes: 3 additions & 0 deletions examples/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions examples/quickstart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions examples/size.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "log")]
#[macro_use]
extern crate log;
#[macro_use]
extern crate error_chain;

Expand Down
5 changes: 5 additions & 0 deletions src/bin/has_backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 45 additions & 1 deletion src/error_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,62 @@ 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 )*
) => {
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<T> = ::std::result::Result<T, $error_name>;
};
// 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 )*
}
Expand All @@ -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 {
Expand Down Expand Up @@ -342,6 +383,9 @@ macro_rules! impl_error_chain_processed {
}


#[cfg(feature = "log")]
impl_result_log_ext!{ $result_log_ext_name , $error_name }

};
}

Expand Down
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {}
Expand Down Expand Up @@ -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! {}
Expand All @@ -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! {
Expand All @@ -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 } }
Expand All @@ -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 } }
Expand Down Expand Up @@ -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! {}
Expand All @@ -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! {}
Expand All @@ -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! {} }
Expand Down Expand Up @@ -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! {
Expand All @@ -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! {
Expand Down Expand Up @@ -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()
//!
Expand Down Expand Up @@ -466,6 +488,8 @@
//! within your own project.
//!
//! ```
//! # #[cfg(feature = "log")]
//! # #[macro_use] extern crate log;
//! # #[macro_use]
//! # extern crate error_chain;
//! # mod errors {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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() { }
Expand All @@ -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() { }
Expand All @@ -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! {
Expand All @@ -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! { }
Expand Down Expand Up @@ -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() { }
Expand Down
Loading