Skip to content

Commit

Permalink
config: ensure panics are printed to the logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mahkoh committed Mar 5, 2024
1 parent 68b205d commit 8e7dc5f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jay-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
20 changes: 20 additions & 0 deletions jay-config/src/_private/logging.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
17 changes: 12 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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)),
Expand All @@ -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)))
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/config/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8e7dc5f

Please sign in to comment.