consolidate the panic/unwind code

This commit is contained in:
Dustin Carlino 2018-11-12 11:12:14 -08:00
parent f1d82a4c97
commit f577cc9948
3 changed files with 26 additions and 26 deletions

View File

@ -4,7 +4,6 @@ use objects::SIM;
use piston::input::Key;
use sim::{Benchmark, ScoreSummary, TIMESTEP};
use std::mem;
use std::panic;
use std::time::{Duration, Instant};
use ui::{PerMapUI, PluginsPerMap};
@ -82,10 +81,10 @@ impl SimController {
self.last_step = Some(Instant::now());
self.benchmark = Some(primary.sim.start_benchmark());
} else if input.unimportant_key_pressed(Key::M, SIM, "run one step") {
step_sim("Primary", primary);
primary.sim.step(&primary.map, &primary.control_map);
primary.recalculate_current_selection = true;
if let Some((s, _)) = secondary {
step_sim("Secondary", s);
s.sim.step(&s.map, &s.control_map);
}
}
}
@ -117,10 +116,10 @@ impl SimController {
// TODO https://gafferongames.com/post/fix_your_timestep/
let dt_s = elapsed_seconds(tick);
if dt_s >= TIMESTEP.value_unsafe / self.desired_speed {
step_sim("Primary", primary);
primary.sim.step(&primary.map, &primary.control_map);
primary.recalculate_current_selection = true;
if let Some((s, _)) = secondary {
step_sim("Secondary", s);
s.sim.step(&s.map, &s.control_map);
}
self.last_step = Some(Instant::now());
}
@ -200,15 +199,3 @@ fn summarize(txt: &mut Text, summary: ScoreSummary) {
));
txt.add_line(format!(" {} total", summary.total_driving_trip_time));
}
fn step_sim(name: &str, ui: &mut PerMapUI) {
if let Err(err) = panic::catch_unwind(panic::AssertUnwindSafe(|| {
ui.sim.step(&ui.map, &ui.control_map);
})) {
error!(
"{} sim failed at {} while processing {:?}",
name, ui.sim.time, ui.sim.current_agent_for_debugging
);
panic::resume_unwind(err);
}
}

View File

@ -53,7 +53,13 @@ impl GUI for UI {
})) {
Ok(()) => {}
Err(err) => {
error!("UI broke. Sim time is {}", self.primary.sim.time);
error!("********************************************************************************");
error!("UI broke! Primary sim:");
self.primary.sim.dump_before_abort();
if let Some((s, _)) = &self.secondary {
error!("Secondary sim:");
s.sim.dump_before_abort();
}
self.save_editor_state();
panic::resume_unwind(err);
}

View File

@ -55,7 +55,7 @@ pub struct Sim {
// This should only be Some in the middle of step(). The caller of step() can grab this if
// step() panics.
pub current_agent_for_debugging: Option<AgentID>,
current_agent_for_debugging: Option<AgentID>,
}
impl Sim {
@ -129,17 +129,24 @@ impl Sim {
}
}
pub fn dump_before_abort(&self) {
error!("********************************************************************************");
error!(
"At {} while processing {:?}",
self.time, self.current_agent_for_debugging
);
// TODO Most recent before current time
if let Ok(s) = self.find_most_recent_savestate() {
error!("Debug from {}", s);
}
}
pub fn step(&mut self, map: &Map, control_map: &ControlMap) -> Vec<Event> {
match self.inner_step(map, control_map) {
Ok(events) => events,
Err(e) => {
error!(
"\nAt {} while processing {:?}:{}",
self.time, self.current_agent_for_debugging, e
);
if let Ok(s) = self.find_most_recent_savestate() {
error!("Debug from {}", s);
}
self.dump_before_abort();
error!("{}", e);
process::exit(1);
}
}