1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use api::protocol::Event;

// public api from other crates
pub use sentry_types::protocol::v7 as protocol;
pub use sentry_types::protocol::v7::{Breadcrumb, Level, User};
pub use sentry_types::{
    ChronoParseError, DateTime, DebugId, Dsn, ParseDebugIdError, TimeZone, Utc, Uuid, UuidVariant,
    UuidVersion,
};

// public exports from this crate
#[cfg(feature = "with_client_implementation")]
pub use client::{init, Client, ClientOptions};
pub use hub::Hub;
pub use scope::Scope;

use hub::IntoBreadcrumbs;

/// Useful internals.
///
/// This module contains types that users of the create typically do not
/// have to directly interface with directly.  These are often returned
/// from methods on other types.
pub mod internals {
    #[cfg(feature = "with_client_implementation")]
    pub use client::{ClientInitGuard, IntoDsn};
    pub use hub::IntoBreadcrumbs;
    pub use scope::ScopeGuard;
    pub use sentry_types::{Auth, DsnParseError, ProjectId, ProjectIdParseError, Scheme};
    #[cfg(feature = "with_client_implementation")]
    pub use transport::{DefaultTransportFactory, HttpTransport, Transport, TransportFactory};
}

/// Captures an event on the currently active client if any.
///
/// The event must already be assembled.  Typically code would instead use
/// the utility methods like `capture_exception`.  The return value is the
/// event ID.  In case Sentry is disabled the return value will be the nil
/// UUID (`Uuid::nil`).
///
/// # Example
///
/// ```
/// use sentry::protocol::{Event, Level};
///
/// sentry::capture_event(Event {
///     message: Some("Hello World!".into()),
///     level: Level::Info,
///     ..Default::default()
/// });
/// ```
#[allow(unused_variables)]
pub fn capture_event(event: Event<'static>) -> Uuid {
    with_client_impl! {{
        Hub::with(|hub| hub.capture_event(event))
    }}
}

/// Captures an arbitrary message.
///
/// This creates an event form the given message and sends it to the current hub.
#[allow(unused_variables)]
pub fn capture_message(msg: &str, level: Level) -> Uuid {
    with_client_impl! {{
        Hub::with_active(|hub| {
            hub.capture_message(msg, level)
        })
    }}
}

/// Records a breadcrumb by calling a function.
///
/// The total number of breadcrumbs that can be recorded are limited by the
/// configuration on the client.  This function accepts any object that
/// implements `IntoBreadcrumbs` which is implemented for a varienty of
/// common types.  For efficiency reasons you can also pass a closure returning
/// a breadcrumb in which case the closure is only called if the client is
/// enabled.
///
/// The most common implementations that can be passed:
///
/// * `Breadcrumb`: to record a breadcrumb
/// * `Vec<Breadcrumb>`: to record more than one breadcrumb in one go.
/// * `Option<Breadcrumb>`: to record a breadcrumb or not
/// * additionally all of these can also be returned from an `FnOnce()`
///
/// # Example
///
/// ```
/// use sentry::protocol::{Breadcrumb, Map};
///
/// sentry::add_breadcrumb(|| Breadcrumb {
///     ty: "http".into(),
///     category: Some("request".into()),
///     data: {
///         let mut map = Map::new();
///         map.insert("method".into(), "GET".into());
///         map.insert("url".into(), "https://example.com/".into());
///         map
///     },
///     ..Default::default()
/// });
/// ```
#[allow(unused_variables)]
pub fn add_breadcrumb<B: IntoBreadcrumbs>(breadcrumb: B) {
    with_client_impl! {{
        Hub::with_active(|hub| {
            hub.add_breadcrumb(breadcrumb)
        })
    }}
}

/// Invokes a function that can modify the current scope.
///
/// The function is passed a mutable reference to the `Scope` so that modifications
/// can be performed.  Because there might currently not be a scope or client active
/// it's possible that the callback might not be called at all.  As a result of this
/// the return value of this closure must have a default that is returned in such
/// cases.
///
/// # Example
///
/// ```rust
/// sentry::configure_scope(|scope| {
///     scope.set_user(Some(sentry::User {
///         username: Some("john_doe".into()),
///         ..Default::default()
///     }));
/// });
/// ```
///
/// # Panics
///
/// While the scope is being configured accessing scope related functionality is
/// not permitted.  In this case a wide range of panics will be raised.  It's
/// unsafe to call into `sentry::bind_client` or similar functions from within
/// the callback as a result of this.
#[allow(unused_variables)]
pub fn configure_scope<F, R>(f: F) -> R
where
    R: Default,
    F: FnOnce(&mut Scope) -> R,
{
    with_client_impl! {{
        Hub::with_active(|hub| {
            hub.configure_scope(f)
        })
    }}
}

/// Temporarily pushes a scope for a single call optionally reconfiguring it.
///
/// This function takes two arguments: the first is a callback that is passed
/// a scope and can reconfigure it.  The second is callback that then executes
/// in the context of that scope.
///
/// This is useful when extra data should be send with a single capture call
/// for instance a different level or tags:
///
/// ```rust,ignore
/// use sentry::{with_scope, Level};
/// use sentry::integrations::failure::capture_error;
///
/// with_scope(
///     |scope| scope.set_level(Level::Warning),
///     || capture_error(err)
/// );
/// ```
pub fn with_scope<C, F, R>(scope_config: C, callback: F) -> R
where
    C: FnOnce(&mut Scope),
    F: FnOnce() -> R,
{
    #[cfg(with_client_impl)]
    {
        Hub::with(|hub| {
            if hub.is_active_and_usage_safe() {
                hub.with_scope(scope_config, callback)
            } else {
                callback()
            }
        })
    }
    #[cfg(not(with_client_impl))]
    {
        let _scope_config = scope_config;
        callback()
    }
}

/// Returns the last event ID captured.
pub fn last_event_id() -> Option<Uuid> {
    with_client_impl! {{
        Hub::with_active(|hub| {
            hub.last_event_id()
        })
    }}
}