1use once_cell::sync::OnceCell;
2use tracing::dispatcher;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum LoggingMode {
7 Library,
9 Application,
11}
12
13pub struct SubscriberDetector;
15
16static DETECTED_MODE: OnceCell<LoggingMode> = OnceCell::new();
17
18impl SubscriberDetector {
19 #[inline]
23 pub fn has_subscriber() -> bool {
24 dispatcher::has_been_set()
25 }
26
27 pub fn detect_mode() -> LoggingMode {
29 *DETECTED_MODE.get_or_init(|| {
30 if Self::has_subscriber() {
31 LoggingMode::Library
32 } else {
33 LoggingMode::Application
34 }
35 })
36 }
37}