tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.14k stars 671 forks source link

Unexpected side-effect in Dispatch::new #2976

Open JakkuSakura opened 1 month ago

JakkuSakura commented 1 month ago

Dispatch::new is not side-effect-free.

impl Dispatch {
    /// Returns a `Dispatch` that forwards to the given [`Subscriber`].
    ///
    /// [`Subscriber`]: super::subscriber::Subscriber
    pub fn new<S>(subscriber: S) -> Self
    where
        S: Subscriber + Send + Sync + 'static,
    {
        let me = Dispatch {
            subscriber: Kind::Scoped(Arc::new(subscriber)),
        };
        callsite::register_dispatch(&me);
        me
    }
}

It should be moved to a new function like register_dispatch and get called in dispatcher::set_global_default and similar functions Not hidden in self.into() like in tracing_susbcriber

        dispatcher::set_global_default(self.into()).map_err(TryInitError::new)?;

https://github.com/tokio-rs/tracing/blame/690a9a68098aa1eb86d1c5c4aad085ef12245bca/tracing-core/src/dispatch.rs#L545

https://github.com/JakkuSakura/tracing-shared-rs/issues/2

JakkuSakura commented 1 month ago

Above "close" was misclicked