Skip to main content

sentry_core/
macros.rs

1/// Returns the intended release for Sentry as an `Option<Cow<'static, str>>`.
2///
3/// This can be used with `ClientOptions` to set the release name.  It uses
4/// the information supplied by cargo to calculate a release.
5///
6/// # Examples
7///
8/// ```
9/// # #[macro_use] extern crate sentry;
10/// # fn main() {
11/// let _sentry = sentry::init(sentry::ClientOptions {
12///     release: sentry::release_name!(),
13///     ..Default::default()
14/// });
15/// # }
16/// ```
17#[macro_export]
18macro_rules! release_name {
19    () => {{
20        use std::sync::Once;
21        static mut INIT: Once = Once::new();
22        static mut RELEASE: Option<String> = None;
23        unsafe {
24            INIT.call_once(|| {
25                RELEASE = option_env!("CARGO_PKG_NAME").and_then(|name| {
26                    option_env!("CARGO_PKG_VERSION").map(|version| format!("{}@{}", name, version))
27                });
28            });
29            RELEASE.as_ref().map(|x| {
30                let release: &'static str = ::std::mem::transmute(x.as_str());
31                ::std::borrow::Cow::Borrowed(release)
32            })
33        }
34    }};
35}
36
37// TODO: temporarily exported for use in `sentry` crate
38#[macro_export]
39#[doc(hidden)]
40macro_rules! with_client_impl {
41    ($body:block) => {
42        #[cfg(feature = "client")]
43        {
44            $body
45        }
46        #[cfg(not(feature = "client"))]
47        {
48            Default::default()
49        }
50    };
51}
52
53// TODO: temporarily exported for use in `sentry` crate
54#[macro_export]
55#[doc(hidden)]
56macro_rules! sentry_debug {
57    ($($arg:tt)*) => {
58        $crate::Hub::with(|hub| {
59            if hub.client().map_or(false, |c| c.options().debug) {
60                eprint!("[sentry] ");
61                eprintln!($($arg)*);
62            }
63        });
64    }
65}
66
67/// Panics in debug builds and logs through `sentry_debug!` in non-debug builds.
68#[macro_export]
69#[doc(hidden)]
70macro_rules! debug_panic_or_log {
71    ($($arg:tt)*) => {{
72        #[cfg(debug_assertions)]
73        panic!($($arg)*);
74
75        #[cfg(not(debug_assertions))]
76        $crate::sentry_debug!($($arg)*);
77    }};
78}
79
80/// If the condition is false, panics in debug builds and logs in non-debug builds.
81#[macro_export]
82#[doc(hidden)]
83macro_rules! debug_assert_or_log {
84    ($cond:expr $(,)?) => {{
85        let condition = $cond;
86        if !condition {
87            $crate::debug_panic_or_log!("assertion failed: {}", stringify!($cond));
88        }
89    }};
90    ($cond:expr, $($arg:tt)+) => {{
91        let condition = $cond;
92        if !condition {
93            $crate::debug_panic_or_log!($($arg)+);
94        }
95    }};
96}
97
98#[allow(unused_macros)]
99macro_rules! minimal_unreachable {
100    () => {
101        panic!(
102            "this code should not be reachable. It's stubbed out for minimal usage. \
103             If you get this error this is a bug in the sentry minimal support"
104        );
105    };
106}
107
108#[cfg(test)]
109mod tests {
110    #[test]
111    fn debug_assert_or_log_does_not_panic_when_condition_holds() {
112        crate::debug_assert_or_log!(2 + 2 == 4, "should not panic");
113    }
114
115    #[test]
116    #[cfg(debug_assertions)]
117    #[should_panic(expected = "assertion failed: 1 == 2")]
118    fn debug_assert_or_log_panics_with_default_message_when_condition_fails() {
119        crate::debug_assert_or_log!(1 == 2);
120    }
121
122    #[test]
123    #[cfg(debug_assertions)]
124    #[should_panic(expected = "custom invariant message")]
125    fn debug_assert_or_log_panics_with_custom_message_when_condition_fails() {
126        crate::debug_assert_or_log!(false, "custom invariant message");
127    }
128
129    #[test]
130    #[cfg(not(debug_assertions))]
131    fn no_panic_without_debug_assertions() {
132        crate::debug_assert_or_log!(false, "should not panic");
133    }
134}