Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New fatal macro #1

Merged
merged 15 commits into from
Apr 24, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## 0.2.0 (TBD)
* Add new `fatal!` macro, which works just like `error!` plus `panic!` (#1)

* Add a `std` feature pass-through for the _log_ crate's `std` feature. We
use it for tests here.
Expand Down
244 changes: 244 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ travis-ci = { repository="dekellum/tao-log" }
[dependencies]
log = { version = ">= 0.4.6, < 0.5" }

[dev-dependencies]
parking_lot = { version = ">= 0.7.1, < 0.8" }

[build-dependencies]
version_check = { version=">=0.1.5, <0.2" }

Expand All @@ -34,3 +37,7 @@ std = [ "log/std" ]
[[test]]
name = "log_v"
harness = false

[[test]]
name = "fatal"
harness = true
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
//! above `debug!` statement does not log, and the cost of formatting the
//! message string is avoided.
//!
//! To these formatted logging macros, _tao-log_ adds a
//! [`fatal!`](macro.fatal.html) macro, which logs at the _error_ level, and
//! then uses the same message to `panic!`.
//!
//! ### Testing for output
//!
//! The [`log_enabled!`](macro.log_enabled.html) macro may be used to
Expand Down
45 changes: 45 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@
// licenses, and is:
// Copyright Ⓒ 2015 The Rust Project Developers

/// Log a message at the error level, flush the logger, and then use the same
/// message to panic.
///
/// This will duplicate the message, once via the registered logger, then
/// again via stderr for the panic (default handler). Since this is a fatal
/// and presumably serious condition, potential duplication is of less concern
/// than the risk of missing the message. This will always `panic!`, even if
/// no logger is configured, or if error level messages aren't logged.
///
/// # Example
///
/// ```rust,should_panic
/// # use std::time::{Duration, Instant};
/// use tao_log::fatal;
///
/// # let td = Duration::new(0, 100_000_000);
/// fatal!("shields compromised, core breach in {:?}!", td);
/// // ^1 -- error level message: shields compromised, core breach in 100ms!
/// // ^2 -- panic: shields compromised, core breach in 100ms!
/// ```
#[macro_export]
macro_rules! fatal {
(target: $target:expr, $($arg:tt)+) => (
$crate::__tao_fatal!($target, $($arg)+)
);
($($arg:tt)+) => (
$crate::__tao_fatal!(module_path!(), $($arg)+)
);
}

/// Log an expression and its value at any specified level.
///
/// Logs with the optional or default (module path of use) target, specified
Expand Down Expand Up @@ -79,6 +109,21 @@ macro_rules! tracev {
($($arg:tt)+) => ($crate::__tao_logv!($crate::log::Level::Trace, $($arg)+))
}

// Helper macro for `fatal!`
#[doc(hidden)]
#[macro_export]
macro_rules! __tao_fatal {
($target:expr, $($arg:tt)+) => (
match format_args!($($arg)+) {
args => {
$crate::error!(target: $target, "{}", args);
$crate::log::logger().flush();
panic!("{}", args);
}
}
);
}

// Helper macro for the -v macros, handling the permutations of optional
// parameters. Note: The required level parameter is first here for
// convenience of internal use with variable-args.
Expand Down
12 changes: 12 additions & 0 deletions test_2015/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ fn test_2015_logv_macros() {
warnv!("prefix", "{:?}", v);
assert!(errorv!(v));
}

#[test]
#[should_panic]
fn test_2015_fatal_static_msg() {
fatal!("static fatal msg");
}

#[test]
#[should_panic]
fn test_2015_fatal_format_msg() {
fatal!("fmt {}", "failing");
}
Loading