emit-rs / emit

Developer-first diagnostics for Rust applications
https://emit-rs.io
Apache License 2.0
128 stars 2 forks source link

Add a way to try flush emitter on panic #122

Closed KodrAus closed 1 month ago

KodrAus commented 1 month ago

The recommended pattern for setting up emit is currently:

fn main() {
    let rt = emit::setup().emit_to(..).init();

    ..

    rt.blocking_flush(Duration::from_secs(5));
}

If the main function panics, then there's no guarantee events will be flushed. Generally, I think you should avoid doing too much in Drop, but we should add a guard type you can use to try flush, even if the main function panics:

fn main() {
    let _ = emit::setup().emit_to(..).init().flush_on_drop(Duration::from_secs(5));

    ..
}

Alternatively, we could also make the type returned by init() try flush unconditionally on Drop, but it would be hard to pick the right duration to block for that works for everyone.