panics now also write a log to ~/.local/shed/log/panic.log

This commit is contained in:
2026-03-04 19:39:53 -05:00
parent 39a33e5759
commit 79cb34246b
2 changed files with 20 additions and 3 deletions

View File

@@ -34,6 +34,7 @@ use crate::libsh::sys::TTY_FILENO;
use crate::libsh::utils::AutoCmdVecUtils;
use crate::parse::execute::exec_input;
use crate::prelude::*;
use crate::procio::IoMode;
use crate::readline::term::{LineWriter, RawModeGuard, raw_mode};
use crate::readline::{Prompt, ReadlineEvent, ShedVi};
use crate::signal::{GOT_SIGWINCH, JOB_DONE, QUIT_CODE, check_signals, sig_setup, signals_pending};
@@ -85,6 +86,21 @@ fn setup_panic_handler() {
}
});
let data_dir = env::var("XDG_DATA_HOME").unwrap_or_else(|_| {
let home = env::var("HOME").unwrap();
format!("{home}/.local/share")
});
let log_dir = Path::new(&data_dir).join("shed").join("log");
std::fs::create_dir_all(&log_dir).unwrap();
let log_file_path = log_dir.join("panic.log");
let mut log_file = parse::get_redir_file(parse::RedirType::Output, log_file_path).unwrap();
let panic_info_raw = info.to_string();
log_file.write_all(panic_info_raw.as_bytes()).unwrap();
let backtrace = std::backtrace::Backtrace::force_capture();
log_file.write_all(format!("\nBacktrace:\n{:?}", backtrace).as_bytes()).unwrap();
default_panic_hook(info);
}));
}

View File

@@ -1736,18 +1736,19 @@ fn node_is_punctuated(tokens: &[Tk]) -> bool {
.is_some_and(|tk| matches!(tk.class, TkRule::Sep))
}
pub fn get_redir_file(class: RedirType, path: PathBuf) -> ShResult<File> {
pub fn get_redir_file<P: AsRef<Path>>(class: RedirType, path: P) -> ShResult<File> {
let path = path.as_ref();
let result = match class {
RedirType::Input => OpenOptions::new().read(true).open(Path::new(&path)),
RedirType::Output => OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(Path::new(&path)),
.open(path),
RedirType::Append => OpenOptions::new()
.create(true)
.append(true)
.open(Path::new(&path)),
.open(path),
_ => unimplemented!(),
};
Ok(result?)