ion-rust icon indicating copy to clipboard operation
ion-rust copied to clipboard

Does IonWriter::flush() flush() the underlying file?

Open barries opened this issue 9 months ago • 1 comments

Our app is logging from a panic hook, and sometimes the data makes it to disk, sometimes it doesn't. In the below code, both closed!!! and flushed!!! are printed, but the data does not make it to disk.

Should IonWriter::flush() call flush() or even sync() on the underlying file?

If not, it would be great if it would flush() and allow calling code to sync() the underlying file.

Thanks!

#[track_caller]
fn log_panic_info(panic_info: &panic::PanicHookInfo<'_>) {
    if let Some(mutex) = LOG.get() {
        if let Ok(mut log) = mutex.lock() {
            {
                let mut entry_writer = EntryWriter::new(&mut log, EntryKind::Panic);

                let mut panic_info_writer = entry_writer.ion_list_writer.struct_writer().expect("panic_info error: panic_info_writer creation failed");
                if let Some(payload) = panic_info.payload().downcast_ref::<&str>() {
                    let _ = panic_info_writer.write("payload", payload).map_err(|err| eprintln!("{err:?} at {}:{} in {}", file!(), line!(), module_path!()));
                } else if let Some(payload) = panic_info.payload().downcast_ref::<String>() {
                    let _ = panic_info_writer.write("payload", payload.as_str()).map_err(|err| eprintln!("{err:?} at {}:{} in {}", file!(), line!(), module_path!()));
                } else {
                    let _ = panic_info_writer.write("payload", format!("{:?}", panic_info.payload()).as_str()).map_err(|err| eprintln!("{err:?} at {}:{} in {}", file!(), line!(), module_path!()));
                }

                ...

                let _ = panic_info_writer.close().map_err(|err| eprintln!("{err:?} at {}:{} in {}", file!(), line!(), module_path!()));
                let _ = entry_writer.ion_list_writer.close().map_err(|err| eprintln!("{err:?} at {}:{} in {}", file!(), line!(), module_path!()));
                eprintln!("closed!!!");
            }
            let _ = log.ion_writer.flush();
            eprintln!("flushed!!!");
        }
    }
}

barries avatar Mar 19 '25 17:03 barries

By my understanding flush() should flush the underlying Write impl, see the implementation in 1.0 and 1.1, but my familiarity with ion-rs is less than most who contribute to it.

Can you share a bit more about how your writer is set up? Perhaps an SSCCE?

jobarr-amzn avatar Mar 24 '25 21:03 jobarr-amzn