use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};
use tracing::Id;
use tracing_core::dispatcher;
use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt};
type LazyMutex<T> = Lazy<Arc<Mutex<T>>>;
mod attribute;
mod layer;
mod log;
mod record;
mod report;
pub use layer::Layer;
pub use record::{Record, RecordValue};
pub use report::{Filter, Records, Report, Span};
static INIT: Lazy<()> = Lazy::new(|| {
if dispatcher::has_been_set() {
dispatcher::get_default(|dispatcher| {
assert!(dispatcher.is::<Layer>(), "A tracing global subscriber has already been set by an other crate than test-span, cannot proceed");
})
} else {
let dispatcher = tracing_subscriber::registry().with(Layer {});
dispatcher
.try_init()
.expect("couldn't set test-span subscriber as a default")
}
});
pub fn init() {
Lazy::force(&INIT);
}
pub fn get_all_logs(filter: &Filter) -> Records {
let logs = layer::ALL_LOGS.lock().unwrap().clone();
Records::new(logs.all_records_for_filter(filter))
}
pub fn get_telemetry_for_root(root_id: &Id, filter: &Filter) -> (Span, Records) {
let report = Report::from_root(root_id.into_u64());
(report.spans(filter), report.logs(filter))
}
pub fn get_spans_for_root(root_id: &Id, filter: &Filter) -> Span {
Report::from_root(root_id.into_u64()).spans(filter)
}
pub fn get_logs_for_root(root_id: &Id, filter: &Filter) -> Records {
Report::from_root(root_id.into_u64()).logs(filter)
}
pub mod prelude {
pub use crate::{get_all_logs, get_logs_for_root, get_spans_for_root, get_telemetry_for_root};
pub use test_span_macro::test_span;
}
pub mod reexports {
pub use daggy;
pub use serde;
pub use tracing;
pub use tracing_futures;
pub use tracing_subscriber;
}
#[cfg(test)]
mod test_span_doesnt_panic_tests {
use super::*;
#[test]
fn init_with_already_set_test_span_global_subscriber_doesnt_panic() {
tracing_subscriber::registry()
.with(Layer {})
.try_init()
.unwrap();
init();
}
}