Skip to main content

everything_plugin/
log.rs

1use std::error::Error;
2
3pub use tracing::{debug, error, info, trace, warn};
4
5/// A convenient function to initialize [`tracing`] with a default configuration.
6///
7/// Error if already inited.
8pub fn tracing_try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
9    #[cfg(not(feature = "tracing-appender"))]
10    let stderr = anstream::stderr;
11    #[cfg(feature = "tracing-appender")]
12    let stderr = {
13        let (non_blocking, guard) = tracing_appender::non_blocking(anstream::stderr());
14        std::mem::forget(guard);
15        non_blocking
16    };
17
18    tracing_subscriber::fmt()
19        .with_writer(stderr)
20        .with_max_level(tracing::Level::DEBUG)
21        .try_init()?;
22
23    #[cfg(debug_assertions)]
24    std::panic::set_hook(Box::new(|info| {
25        tracing_panic::panic_hook(info);
26        std::thread::sleep(std::time::Duration::from_secs(60));
27    }));
28
29    Ok(())
30}
31
32/// A convenient function to initialize [`tracing`] with a default configuration.
33///
34/// Panics if already inited.
35pub fn tracing_init() {
36    tracing_try_init().expect("Unable to install global subscriber")
37}