From e75ef3f9e6ccc691e6c9fa0d471e47b19c732194 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 1 Dec 2018 13:14:54 -0800 Subject: [PATCH] making an easy way to record and dump notes at the end of verbose setup logging, without being tied to a Timer --- abstutil/Cargo.toml | 1 + abstutil/src/lib.rs | 4 ++++ abstutil/src/notes.rs | 20 ++++++++++++++++++++ abstutil/src/time.rs | 2 ++ map_model/src/traffic_signals.rs | 4 ++-- 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 abstutil/src/notes.rs diff --git a/abstutil/Cargo.toml b/abstutil/Cargo.toml index ce74fd608a..e2b00f6321 100644 --- a/abstutil/Cargo.toml +++ b/abstutil/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Dustin Carlino "] [dependencies] +lazy_static = "1.1.0" log = "0.4.5" multimap = "0.4.0" rand = { version = "0.5.1", features = ["serde1"] } diff --git a/abstutil/src/lib.rs b/abstutil/src/lib.rs index 61f1e15847..29197e9a62 100644 --- a/abstutil/src/lib.rs +++ b/abstutil/src/lib.rs @@ -1,3 +1,5 @@ +#[macro_use] +extern crate lazy_static; extern crate log; extern crate multimap; extern crate rand; @@ -13,6 +15,7 @@ mod collections; mod error; mod io; mod logs; +mod notes; mod random; mod time; @@ -25,6 +28,7 @@ pub use io::{ write_json, FileWithProgress, }; pub use logs::{format_log_record, LogAdapter}; +pub use notes::note; pub use random::{fork_rng, WeightedUsizeChoice}; pub use time::{elapsed_seconds, Timer}; diff --git a/abstutil/src/notes.rs b/abstutil/src/notes.rs new file mode 100644 index 0000000000..9ef87d9094 --- /dev/null +++ b/abstutil/src/notes.rs @@ -0,0 +1,20 @@ +use std::sync::Mutex; + +lazy_static! { + static ref NOTES: Mutex> = Mutex::new(Vec::new()); +} + +pub fn note(msg: String) { + NOTES.lock().unwrap().push(msg); +} + +pub fn dump_notes() { + let mut notes = NOTES.lock().unwrap(); + + // TODO log or println? + for msg in notes.iter() { + println!("{}", msg); + } + + notes.clear(); +} diff --git a/abstutil/src/time.rs b/abstutil/src/time.rs index e48490e36a..d96f8a9c3d 100644 --- a/abstutil/src/time.rs +++ b/abstutil/src/time.rs @@ -1,3 +1,4 @@ +use notes; use std::io::{stdout, Write}; use std::time::Instant; use PROGRESS_FREQUENCY_SECONDS; @@ -114,6 +115,7 @@ impl Timer { } println!(""); } + notes::dump_notes(); } pub fn start(&mut self, name: &str) { diff --git a/map_model/src/traffic_signals.rs b/map_model/src/traffic_signals.rs index 65c689983c..9777f4e00e 100644 --- a/map_model/src/traffic_signals.rs +++ b/map_model/src/traffic_signals.rs @@ -1,4 +1,4 @@ -use abstutil::Error; +use abstutil::{note, Error}; use dimensioned::si; use std; use std::collections::BTreeSet; @@ -195,7 +195,7 @@ fn smart_assignment(map: &Map, i: IntersectionID) -> ControlTrafficSignal { return ts; } Err(err) => { - error!("For {}: {}", i, err); + note(format!("For {}: {}", i, err)); } } }