#[cfg(feature = "error-tracking")]
use std::error::Error as StdError;
use std::sync::OnceLock;
#[cfg(feature = "error-tracking")]
use crate::error_tracking::CaptureExceptionOptions;
use crate::{client, Client, ClientOptions, Error, Event};
static GLOBAL_CLIENT: OnceLock<Client> = OnceLock::new();
static GLOBAL_DISABLE: OnceLock<bool> = OnceLock::new();
#[cfg(feature = "async-client")]
pub async fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
if is_disabled() {
return Ok(());
}
let client = client(options).await;
GLOBAL_CLIENT
.set(client)
.map_err(|_| Error::AlreadyInitialized)
}
#[cfg(not(feature = "async-client"))]
pub fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
if is_disabled() {
return Ok(());
}
let client = client(options);
GLOBAL_CLIENT
.set(client)
.map_err(|_| Error::AlreadyInitialized)
}
pub fn disable() {
let _ = GLOBAL_DISABLE.set(true);
}
pub fn is_disabled() -> bool {
*GLOBAL_DISABLE.get().unwrap_or(&false)
}
pub fn capture(event: Event) {
if let Some(client) = GLOBAL_CLIENT.get() {
client.capture(event);
}
}
#[cfg(feature = "async-client")]
pub async fn flush() {
if let Some(client) = GLOBAL_CLIENT.get() {
client.flush().await;
}
}
#[cfg(feature = "async-client")]
pub async fn shutdown() {
if let Some(client) = GLOBAL_CLIENT.get() {
client.shutdown().await;
}
}
#[cfg(all(feature = "async-client", feature = "error-tracking"))]
pub async fn capture_exception<E>(error: &E) -> Result<(), Error>
where
E: StdError + ?Sized,
{
let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
client.capture_exception(error).await
}
#[cfg(all(feature = "async-client", feature = "error-tracking"))]
pub async fn capture_exception_with<E>(
error: &E,
options: CaptureExceptionOptions,
) -> Result<(), Error>
where
E: StdError + ?Sized,
{
let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
client.capture_exception_with(error, options).await
}
#[cfg(not(feature = "async-client"))]
pub fn flush() {
if let Some(client) = GLOBAL_CLIENT.get() {
client.flush();
}
}
#[cfg(not(feature = "async-client"))]
pub fn shutdown() {
if let Some(client) = GLOBAL_CLIENT.get() {
client.shutdown();
}
}
#[cfg(all(not(feature = "async-client"), feature = "error-tracking"))]
pub fn capture_exception<E>(error: &E) -> Result<(), Error>
where
E: StdError + ?Sized,
{
let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
client.capture_exception(error)
}
#[cfg(all(not(feature = "async-client"), feature = "error-tracking"))]
pub fn capture_exception_with<E>(error: &E, options: CaptureExceptionOptions) -> Result<(), Error>
where
E: StdError + ?Sized,
{
let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
client.capture_exception_with(error, options)
}