human-panic icon indicating copy to clipboard operation
human-panic copied to clipboard

Custom path to save a panic report

Open rucoder opened this issue 1 year ago • 2 comments

Currently Report::persist uses env::temp_dir() to get a base folder to save a panic report

    /// Write a file to disk.
    pub fn persist(&self) -> Result<PathBuf, Box<dyn Error + 'static>> {
        let uuid = Uuid::new_v4().hyphenated().to_string();
        let tmp_dir = env::temp_dir();
        let file_name = format!("report-{}.toml", &uuid);
        let file_path = Path::new(&tmp_dir).join(file_name);
        let toml = self.serialize().expect("only using toml-compatible types");
        std::fs::write(&file_path, toml.as_bytes())?;
        Ok(file_path)
    }

However on some platforms TMPDIR may not be defined or be RO (yes, weird but true) . Sure TMPDIR may be overwritten when starting application however it would be nice to configure a base dir either as a parameter to ::handle_dump (breaking change) of through Metadata object

rucoder avatar Jan 28 '25 00:01 rucoder

If TMPDIR is not accessible, how do you plan to use a custom path to workaround the issue? I'm wondering if there is something to be generalized with that.

epage avatar Jan 28 '25 19:01 epage

right now I'm doing it like this

        // FIXME: set TMPDIR to value of EVE_MONITOR_LOG_DIR before calling handle_dump
        // or panic report won't be saved on EVE
        // we can remove it when human-panic is fixed
        // see https://github.com/rust-cli/human-panic/issues/167
        let _ = std::env::var("EVE_MONITOR_LOG_DIR").and_then(|log_dir| {
            std::env::set_var("TMPDIR", log_dir);
            Ok(())
        });

however saving panic report into /tmp is not the best idea -- it won't survive a reboot and the panic report is lost. We could

  1. introduce a new field Option<PathBuf> in Metadata since this is the only parameter which can be changed when calling handle_dump, other changes will break API.
  2. or define a standard environment variable e.g. PANIC_DUMP_PATH and use this one if it is set or fallback to TMPDIR if it is not

rucoder avatar Jul 28 '25 08:07 rucoder