use std::sync::Arc;
use sentry_core::sentry_debug;
#[cfg(feature = "release-health")]
use sentry_core::SessionMode;
use crate::defaults::apply_defaults;
use crate::{Client, ClientOptions, Hub};
#[must_use = "when the init guard is dropped the send queue is flushed and the \
transport will be shut down and no further events can be sent."]
pub struct ClientInitGuard(Arc<Client>);
impl std::ops::Deref for ClientInitGuard {
type Target = Client;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ClientInitGuard {
pub fn is_enabled(&self) -> bool {
self.0.is_enabled()
}
}
impl Drop for ClientInitGuard {
fn drop(&mut self) {
if self.is_enabled() {
sentry_debug!("dropping client guard -> disposing client");
} else {
sentry_debug!("dropping client guard (no client to dispose)");
}
#[cfg(feature = "release-health")]
crate::end_session();
self.0.close(None);
}
}
pub fn init<C>(opts: C) -> ClientInitGuard
where
C: Into<ClientOptions>,
{
let opts = apply_defaults(opts.into());
#[cfg(feature = "release-health")]
let auto_session_tracking = opts.auto_session_tracking;
#[cfg(feature = "release-health")]
let session_mode = opts.session_mode;
let client = Arc::new(Client::from(opts));
Hub::with(|hub| hub.bind_client(Some(client.clone())));
if let Some(dsn) = client.dsn() {
sentry_debug!("enabled sentry client for DSN {}", dsn);
} else {
sentry_debug!("initialized disabled sentry client due to disabled or invalid DSN");
}
#[cfg(feature = "release-health")]
if auto_session_tracking && session_mode == SessionMode::Application {
crate::start_session()
}
ClientInitGuard(client)
}