1
0
Fork 0
forked from wry/wry

config: ensure panics are printed to the logs

This commit is contained in:
Julian Orth 2024-03-05 22:41:32 +01:00
parent d4d76c0ef3
commit d231021afc
5 changed files with 41 additions and 5 deletions

1
Cargo.lock generated
View file

@ -539,6 +539,7 @@ dependencies = [
name = "jay-config"
version = "0.1.0"
dependencies = [
"backtrace",
"bincode",
"futures-util",
"log",

View file

@ -12,3 +12,4 @@ log = "0.4.14"
futures-util = { version = "0.3.30", features = ["io"] }
uapi = "0.2.10"
thiserror = "1.0.57"
backtrace = "0.3.69"

View file

@ -1,11 +1,31 @@
use {
crate::logging::LogLevel,
backtrace::Backtrace,
log::{Level, LevelFilter, Log, Metadata, Record},
};
pub fn init() {
let _ = log::set_logger(&Logger);
log::set_max_level(LevelFilter::Trace);
std::panic::set_hook(Box::new(|p| {
if let Some(loc) = p.location() {
log::error!(
"Panic at {} line {} column {}",
loc.file(),
loc.line(),
loc.column()
);
} else {
log::error!("Panic at unknown location");
}
if let Some(msg) = p.payload().downcast_ref::<&str>() {
log::error!("Message: {}", msg);
}
if let Some(msg) = p.payload().downcast_ref::<String>() {
log::error!("Message: {}", msg);
}
log::error!("Backtrace:\n{:?}", Backtrace::new());
}));
}
struct Logger;

View file

@ -156,9 +156,15 @@ unsafe extern "C" fn default_client_init(
}
impl ConfigProxy {
fn new(lib: Option<Library>, entry: &ConfigEntry, state: &Rc<State>) -> Self {
fn new(
lib: Option<Library>,
entry: &ConfigEntry,
state: &Rc<State>,
path: Option<String>,
) -> Self {
let version = entry.version.min(VERSION);
let data = Rc::new(ConfigProxyHandler {
path,
client_data: Cell::new(ptr::null()),
dropped: Cell::new(false),
_lib: lib,
@ -207,12 +213,12 @@ impl ConfigProxy {
unref: jay_config::_private::client::unref,
handle_msg: jay_config::_private::client::handle_msg,
};
Self::new(None, &entry, state)
Self::new(None, &entry, state, None)
}
#[cfg(feature = "it")]
pub fn for_test(state: &Rc<State>) -> Self {
Self::new(None, &TEST_CONFIG_ENTRY, state)
Self::new(None, &TEST_CONFIG_ENTRY, state, None)
}
pub fn from_config_dir(state: &Rc<State>) -> Result<Self, ConfigError> {
@ -251,7 +257,7 @@ impl ConfigProxy {
if let Err(e) = std::fs::copy(path, &copy) {
return Err(ConfigError::CopyConfigFile(e));
}
let _unlink = UnlinkOnDrop(&copy);
let unlink = UnlinkOnDrop(&copy);
let lib = match Library::new(&copy) {
Ok(l) => l,
Err(e) => return Err(ConfigError::CouldNotLoadLibrary(e)),
@ -261,7 +267,8 @@ impl ConfigProxy {
Ok(e) => *e,
Err(e) => return Err(ConfigError::LibraryDoesNotContainEntry(e)),
};
Ok(Self::new(Some(lib), entry, state))
mem::forget(unlink);
Ok(Self::new(Some(lib), entry, state, Some(copy)))
}
}

View file

@ -55,6 +55,7 @@ use {
};
pub(super) struct ConfigProxyHandler {
pub path: Option<String>,
pub client_data: Cell<*const u8>,
pub dropped: Cell<bool>,
pub _lib: Option<Library>,
@ -100,6 +101,12 @@ impl ConfigProxyHandler {
self.timers_by_id.clear();
self.pollables.clear();
if let Some(path) = &self.path {
if let Err(e) = uapi::unlink(path.as_str()) {
log::error!("Could not unlink {}: {}", path, ErrorFmt(OsError(e.0)));
}
}
}
pub fn send(&self, msg: &ServerMessage) {