tokio-rs / tracing

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

Unexpected side-effect in Dispatch::new #2976

Open JakkuSakura opened 5 months ago

JakkuSakura commented 5 months 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 5 months ago

Above "close" was misclicked