Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Commit

Permalink
Improve logging, introduce new panic hook, add better error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed Mar 17, 2020
1 parent e01bb91 commit c3dfec4
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
## Unreleased Changes
* **Breaking**: Moved script execution to `remodel run` to make room for new subcommands.
* If you previously used `remodel foo.lua`, use `remodel run foo.lua` now.
* Added `--verbose` and `-v` flags for setting verbosity.
* Added `json.fromString` and `json.toString` for encoding/decoding JSON
* Added `remodel.isFile` and `remodel.isDir`.
* Added support for reading the auth cookie through the `REMODEL_AUTH` environment variable.
* Added support for Remodel looking for scripts in the `.remodel` folder of a project
* `remodel run foo` will now run `.remodel/foo.lua` if it exists.
* Added (experimental) support for building Rojo projects through `rojo.buildProject`.
* This is behind the `unstable_rojo_api` Cargo feature and is not enabled by default.
* Improved logging and error reporting across the board.

## 0.6.1 (2019-12-11)
* Upgraded reflection database and dependencies.
Expand Down
22 changes: 15 additions & 7 deletions Cargo.lock

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

13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ default = []
# Enables the unstable Rojo API for Remodel
unstable_rojo_api = ["rojo"]

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
anyhow = "1.0.27"
backtrace = "0.3.45"
log = "0.4.8"
rbx_binary = "0.4.1"
rbx_dom_weak = "1.10.0"
rbx_xml = "0.11.2"
rbx_reflection = "3.2.399"
rbx_xml = "0.11.2"
reqwest = "0.9.20"
rlua = "0.17.0"
structopt = "0.3.11"
serde = "1.0.104"
serde_json = "1.0.44"
structopt = "0.3.11"

rojo = { git = "https://github.com/rojo-rbx/rojo.git", rev = "build-api", optional = true }

[target.'cfg(windows)'.dependencies]
Expand Down
106 changes: 85 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ mod value;
mod rojo_api;

use std::{
error::Error,
fs,
env, fs,
io::{self, Read},
panic,
path::{Path, PathBuf},
process,
};

use backtrace::Backtrace;
use rlua::{Lua, MultiValue, ToLua};
use structopt::StructOpt;

Expand All @@ -27,22 +29,23 @@ use crate::{
about = env!("CARGO_PKG_DESCRIPTION"),
author = env!("CARGO_PKG_AUTHORS"),
)]
pub struct Options {
struct Options {
#[structopt(subcommand)]
subcommand: Subcommand,

/// Enables more verbose logging.
///
/// Can be specified up to 3 times to increase verbosity.
#[structopt(long("verbose"), short, global(true), parse(from_occurrences))]
verbosity: u8,

/// The .ROBLOSECURITY cookie to use for authenticating to the Roblox API.
///
/// Remodel will attempt to use an existing session from Roblox Studio on
/// Windows if it is installed and you are logged in.
///
/// Can also be passed via the REMODEL_AUTH environment variable.
#[structopt(
long = "auth",
env = "REMODEL_AUTH",
hide_env_values = true,
global = true
)]
#[structopt(long("auth"), env("REMODEL_AUTH"), hide_env_values(true), global(true))]
auth_cookie: Option<String>,
}

Expand All @@ -62,13 +65,21 @@ enum Subcommand {
},
}

fn start() -> Result<(), Box<dyn Error>> {
env_logger::from_env(env_logger::Env::default().default_filter_or("warn")).init();
fn main() {
let options = Options::from_args();
initialize_logger(options.verbosity);
install_panic_hook();

let opt = Options::from_args();
let auth_cookie = opt.auth_cookie.or_else(get_auth_cookie);
if let Err(err) = run(options) {
log::error!("{:?}", err);
process::exit(1);
}
}

fn run(options: Options) -> Result<(), anyhow::Error> {
let auth_cookie = options.auth_cookie.or_else(get_auth_cookie);

match opt.subcommand {
match options.subcommand {
Subcommand::Run { script, args } => {
let (contents, chunk_name) = load_script(&script)?;
let lua = Lua::new();
Expand Down Expand Up @@ -160,12 +171,65 @@ fn load_script(script: &str) -> io::Result<(String, String)> {
}
}

fn main() {
match start() {
Ok(_) => {}
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
fn initialize_logger(verbosity: u8) {
let log_filter = match verbosity {
0 => "info",
1 => "info,remodel=debug",
2 => "info,remodel=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();
}

fn install_panic_hook() {
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!("Remodel crashed!");
log::error!("This may be a Remodel bug.");
log::error!("");
log::error!(
"Please consider filing an issue: {}/issues",
env!("CARGO_PKG_REPOSITORY")
);
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);
}));
}

0 comments on commit c3dfec4

Please sign in to comment.