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        let hub = $crate::Hub::current();
59        if hub.client().map_or(false, |c| c.options().debug) {
60            eprint!("[sentry] ");
61            eprintln!($($arg)*);
62        }
63    }}
64}
65
66/// Panics in debug builds and logs through `sentry_debug!` in non-debug builds.
67#[macro_export]
68#[doc(hidden)]
69macro_rules! debug_panic_or_log {
70    ($($arg:tt)*) => {{
71        #[cfg(debug_assertions)]
72        panic!($($arg)*);
73
74        #[cfg(not(debug_assertions))]
75        $crate::sentry_debug!($($arg)*);
76    }};
77}
78
79/// If the condition is false, panics in debug builds and logs in non-debug builds.
80#[macro_export]
81#[doc(hidden)]
82macro_rules! debug_assert_or_log {
83    ($cond:expr $(,)?) => {{
84        let condition = $cond;
85        if !condition {
86            $crate::debug_panic_or_log!("assertion failed: {}", stringify!($cond));
87        }
88    }};
89    ($cond:expr, $($arg:tt)+) => {{
90        let condition = $cond;
91        if !condition {
92            $crate::debug_panic_or_log!($($arg)+);
93        }
94    }};
95}
96
97#[allow(unused_macros)]
98macro_rules! minimal_unreachable {
99    () => {
100        panic!(
101            "this code should not be reachable. It's stubbed out for minimal usage. \
102             If you get this error this is a bug in the sentry minimal support"
103        );
104    };
105}
106
107#[cfg(test)]
108mod tests {
109    #[test]
110    fn debug_assert_or_log_does_not_panic_when_condition_holds() {
111        crate::debug_assert_or_log!(2 + 2 == 4, "should not panic");
112    }
113
114    #[test]
115    #[cfg(debug_assertions)]
116    #[should_panic(expected = "assertion failed: 1 == 2")]
117    fn debug_assert_or_log_panics_with_default_message_when_condition_fails() {
118        crate::debug_assert_or_log!(1 == 2);
119    }
120
121    #[test]
122    #[cfg(debug_assertions)]
123    #[should_panic(expected = "custom invariant message")]
124    fn debug_assert_or_log_panics_with_custom_message_when_condition_fails() {
125        crate::debug_assert_or_log!(false, "custom invariant message");
126    }
127
128    #[test]
129    #[cfg(not(debug_assertions))]
130    fn no_panic_without_debug_assertions() {
131        crate::debug_assert_or_log!(false, "should not panic");
132    }
133}