Files
rojo/src/bin.rs
Lucien Greathouse eb8964e1d1 Improve panic messaging and process behavior.
- Replaced main() to use custom panic hook
- Updated log formatting
- Switched to panic=abort, since we don't need to unwind now.
- Process will now abort if any thread panics.
2020-03-12 15:42:56 -07:00

90 lines
2.9 KiB
Rust

use std::{env, error::Error, panic, process};
use backtrace::Backtrace;
use structopt::StructOpt;
use librojo::cli::{self, Options, Subcommand};
fn run(subcommand: Subcommand) -> Result<(), Box<dyn Error>> {
match subcommand {
Subcommand::Init(init_options) => cli::init(init_options)?,
Subcommand::Serve(serve_options) => cli::serve(serve_options)?,
Subcommand::Build(build_options) => cli::build(build_options)?,
Subcommand::Upload(upload_options) => cli::upload(upload_options)?,
}
Ok(())
}
fn main() {
panic::set_hook(Box::new(|panic_info| {
// PanicInfo's payload is usually a &'static str or String.
// See: https://doc.rust-lang.org/beta/std/panic/struct.PanicInfo.html#method.payload
let message = match panic_info.payload().downcast_ref::<&str>() {
Some(message) => message.to_string(),
None => match panic_info.payload().downcast_ref::<String>() {
Some(message) => message.clone(),
None => "<no message>".to_string(),
},
};
log::error!("Rojo crashed!");
log::error!("This is probably a Rojo bug.");
log::error!("");
log::error!("Please consider filing an issue: https://github.com/rojo-rbx/rojo/issues");
log::error!("");
log::error!("Details: {}", message);
if let Some(location) = panic_info.location() {
log::error!("in file {} on line {}", location.file(), location.line());
}
// When using the backtrace crate, we need to check the RUST_BACKTRACE
// environment variable ourselves. Once we switch to the (currently
// unstable) std::backtrace module, we won't need to do this anymore.
let should_backtrace = env::var("RUST_BACKTRACE")
.map(|var| var == "1")
.unwrap_or(false);
if should_backtrace {
eprintln!("{:?}", Backtrace::new());
} else {
eprintln!(
"note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace."
);
}
process::exit(1);
}));
let options = Options::from_args();
let log_filter = match options.verbosity {
0 => "warn",
1 => "warn,librojo=info",
2 => "warn,librojo=trace",
_ => "trace",
};
let log_env = env_logger::Env::default().default_filter_or(log_filter);
env_logger::Builder::from_env(log_env)
.format_module_path(false)
.format_timestamp(None)
// Indent following lines equal to the log level label, like `[ERROR] `
.format_indent(Some(8))
.init();
if let Err(err) = run(options.subcommand) {
log::error!("{}", err);
let mut current_err: &dyn Error = &*err;
while let Some(source) = current_err.source() {
log::error!(" caused by {}", err);
current_err = &*source;
}
process::exit(1);
}
}