|
|
@@ -20,11 +20,14 @@ extern crate derive_more;
|
|
|
#[macro_use]
|
|
|
extern crate num_derive;
|
|
|
|
|
|
+extern crate backtrace;
|
|
|
+
|
|
|
use std::{
|
|
|
fs::create_dir_all,
|
|
|
path::Path,
|
|
|
process::{exit, Command},
|
|
|
str::FromStr,
|
|
|
+ panic, thread, fmt // For panic logging
|
|
|
};
|
|
|
|
|
|
#[macro_use]
|
|
|
@@ -42,6 +45,16 @@ pub use error::{Error, MapResult};
|
|
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
+// Used for catching panics and log them to file instead of stderr
|
|
|
+use backtrace::Backtrace;
|
|
|
+struct Shim(Backtrace);
|
|
|
+
|
|
|
+impl fmt::Debug for Shim {
|
|
|
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
+ write!(fmt, "\n{:?}", self.0)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
#[structopt(name = "bitwarden_rs", about = "A Bitwarden API server written in Rust")]
|
|
|
struct Opt {
|
|
|
@@ -142,6 +155,44 @@ fn init_logging(level: log::LevelFilter) -> Result<(), fern::InitError> {
|
|
|
|
|
|
logger.apply()?;
|
|
|
|
|
|
+ // Catch panics and log them instead of default output to StdErr
|
|
|
+ panic::set_hook(Box::new(|info| {
|
|
|
+ let backtrace = Backtrace::new();
|
|
|
+
|
|
|
+ let thread = thread::current();
|
|
|
+ let thread = thread.name().unwrap_or("unnamed");
|
|
|
+
|
|
|
+ let msg = match info.payload().downcast_ref::<&'static str>() {
|
|
|
+ Some(s) => *s,
|
|
|
+ None => match info.payload().downcast_ref::<String>() {
|
|
|
+ Some(s) => &**s,
|
|
|
+ None => "Box<Any>",
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+ match info.location() {
|
|
|
+ Some(location) => {
|
|
|
+ error!(
|
|
|
+ target: "panic", "thread '{}' panicked at '{}': {}:{}{:?}",
|
|
|
+ thread,
|
|
|
+ msg,
|
|
|
+ location.file(),
|
|
|
+ location.line(),
|
|
|
+ Shim(backtrace)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ None => {
|
|
|
+ error!(
|
|
|
+ target: "panic",
|
|
|
+ "thread '{}' panicked at '{}'{:?}",
|
|
|
+ thread,
|
|
|
+ msg,
|
|
|
+ Shim(backtrace)
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }));
|
|
|
+
|
|
|
Ok(())
|
|
|
}
|
|
|
|