Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/xcl/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn host_main(boot_path: &Path) -> i32 {
crate::log::Level::parse(&boot.config.log_level),
boot.config.log_to_stderr,
);
crate::log::log_panics();
crate::xlog_info!("XCL {} – host for '{}'", env!("CARGO_PKG_VERSION"), boot.title);
crate::xlog_info!("game: {} (read-only folder: {})", boot.exe_path.display(), boot.game_root.display());
crate::xlog_info!("XCL data: {}", boot.data_dir.display());
Expand Down
27 changes: 27 additions & 0 deletions crates/xcl/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ pub fn flush() {
}
}

/// A panic goes to the log as well as to stderr.
pub fn log_panics() {
let print = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
write(Level::Error, format_args!("thread '{}' {}", std::thread::current().name().unwrap_or(""), info.to_string().replace('\n', " ")));
print(info);
}));
}

#[macro_export]
macro_rules! xlog_error { ($($a:tt)*) => { $crate::log::write($crate::log::Level::Error, format_args!($($a)*)) } }
#[macro_export]
Expand All @@ -107,3 +116,21 @@ macro_rules! xlog_info { ($($a:tt)*) => { $crate::log::write($crate::log::Level:
macro_rules! xlog_debug { ($($a:tt)*) => { if $crate::log::enabled($crate::log::Level::Debug) { $crate::log::write($crate::log::Level::Debug, format_args!($($a)*)) } } }
#[macro_export]
macro_rules! xlog_trace { ($($a:tt)*) => { if $crate::log::enabled($crate::log::Level::Trace) { $crate::log::write($crate::log::Level::Trace, format_args!($($a)*)) } } }

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn a_panic_is_in_the_log() {
let path = std::env::temp_dir().join(format!("xcl-log-panics-{}.log", std::process::id()));
*FILE.lock().unwrap() = Some(File::create(&path).unwrap());
log_panics();
let died = std::thread::Builder::new().name("xcl-dies".into()).spawn(|| panic!("gone for the run")).unwrap().join();
*FILE.lock().unwrap() = None;
let log = std::fs::read_to_string(&path).unwrap();
let _ = std::fs::remove_file(&path);
assert!(died.is_err());
assert!(log.contains("thread 'xcl-dies' panicked at ") && log.contains("gone for the run"), "the log: {log:?}");
}
}
Loading