Skip to content

Commit

Permalink
Merge pull request #26 from hatoo/exit-when-panic
Browse files Browse the repository at this point in the history
Exit when panic
  • Loading branch information
hatoo committed Mar 17, 2020
2 parents 191c118 + f63ff0a commit c133f24
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ float-ord = "0.2.0"
byte-unit = "3.0.3"
tui = { version = "0.8.0", default-features = false, features = ["crossterm"] }
crossterm = "0.14.0"
libc = "0.2.67"

[dev-dependencies]
assert_cmd = "0.12.0"
Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ async fn main() -> anyhow::Result<()> {
Ok(()) = tokio::signal::ctrl_c() => {
// User pressed ctrl-c.
let _ = printer::print(&mut std::io::stdout(),&all, start.elapsed());
std::process::exit(0);
std::process::exit(libc::EXIT_SUCCESS);
}
}
}
Expand Down Expand Up @@ -269,6 +269,15 @@ async fn main() -> anyhow::Result<()> {
basic_auth,
};

std::panic::set_hook(Box::new(|info| {
use crossterm::ExecutableCommand;
let _ = std::io::stdout().execute(crossterm::terminal::LeaveAlternateScreen);
let _ = crossterm::terminal::disable_raw_mode();
let _ = std::io::stdout().execute(crossterm::cursor::Show);
eprintln!("{}", info);
std::process::exit(libc::EXIT_FAILURE);
}));

let task_generator = || async { tx.send(req.clone().request().await) };
if let Some(ParseDuration(duration)) = opts.duration.take() {
if let Some(qps) = opts.query_per_second.take() {
Expand All @@ -292,7 +301,7 @@ async fn main() -> anyhow::Result<()> {
if cfg!(target_os = "macos") {
// On macos, it takes too long time in end of execution for many `-c`.
// So call exit to quit immediately.
std::process::exit(0);
std::process::exit(libc::EXIT_SUCCESS);
} else {
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl Monitor {
crossterm::terminal::enable_raw_mode()?;
let mut stdout = io::stdout();
stdout.execute(crossterm::terminal::EnterAlternateScreen)?;
stdout.execute(crossterm::cursor::Hide)?;

let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;

// Return this when ends to application print summary
let mut all: Vec<anyhow::Result<RequestResult>> = Vec::new();
Expand Down Expand Up @@ -240,10 +240,10 @@ impl Monitor {
}) => {
std::io::stdout().execute(crossterm::terminal::LeaveAlternateScreen)?;
crossterm::terminal::disable_raw_mode()?;
terminal.show_cursor()?;
std::io::stdout().execute(crossterm::cursor::Show)?;
let _ =
crate::printer::print(&mut std::io::stdout(), &all, now - self.start);
std::process::exit(0);
std::process::exit(libc::EXIT_SUCCESS);
}
_ => (),
}
Expand All @@ -254,7 +254,7 @@ impl Monitor {

std::io::stdout().execute(crossterm::terminal::LeaveAlternateScreen)?;
crossterm::terminal::disable_raw_mode()?;
terminal.show_cursor()?;
std::io::stdout().execute(crossterm::cursor::Show)?;
Ok(all)
}
}

0 comments on commit c133f24

Please sign in to comment.