diff --git a/abstutil/Cargo.toml b/abstutil/Cargo.toml index 6a1360c12e..9102f30ef7 100644 --- a/abstutil/Cargo.toml +++ b/abstutil/Cargo.toml @@ -4,7 +4,9 @@ version = "0.1.0" authors = ["Dustin Carlino "] [dependencies] +log = "0.4.5" multimap = "0.4.0" serde = "1.0" serde_cbor = "0.8.2" serde_json = "1.0" +yansi = "0.4.0" diff --git a/abstutil/src/lib.rs b/abstutil/src/lib.rs index 615f785ff5..0b3257bb8e 100644 --- a/abstutil/src/lib.rs +++ b/abstutil/src/lib.rs @@ -1,12 +1,15 @@ +extern crate log; extern crate multimap; extern crate serde; extern crate serde_cbor; extern crate serde_json; +extern crate yansi; mod abst_multimap; mod clone; mod error; mod io; +mod logs; mod time; pub use abst_multimap::MultiMap; @@ -17,4 +20,5 @@ pub use io::{ read_json, save_object, serialize_btreemap, serialize_multimap, to_json, write_binary, write_json, }; +pub use logs::{format_log_record, LogAdapter}; pub use time::elapsed_seconds; diff --git a/abstutil/src/logs.rs b/abstutil/src/logs.rs new file mode 100644 index 0000000000..80abb1d6e9 --- /dev/null +++ b/abstutil/src/logs.rs @@ -0,0 +1,30 @@ +use log::{Log, Metadata, Record}; +use yansi::Paint; + +pub struct LogAdapter; + +impl Log for LogAdapter { + fn enabled(&self, _metadata: &Metadata) -> bool { + true + } + + fn log(&self, record: &Record) { + println!("{}", format_log_record(record)); + } + + fn flush(&self) {} +} + +pub fn format_log_record(record: &Record) -> String { + format!( + "[{}] [{}] {}", + Paint::white(record.level()), + match record.target() { + "UI" => Paint::red("UI"), + "sim" => Paint::green("sim"), + "map" => Paint::blue("map"), + x => Paint::cyan(x), + }, + record.args() + ) +} diff --git a/editor/Cargo.toml b/editor/Cargo.toml index 3ccbf23d06..9c78ac1e7d 100644 --- a/editor/Cargo.toml +++ b/editor/Cargo.toml @@ -28,4 +28,3 @@ sim = { path = "../sim" } structopt = "0.2" strum = "0.9.0" strum_macros = "0.9.0" -yansi = "0.4.0" diff --git a/editor/src/main.rs b/editor/src/main.rs index c9875163a1..50e913dbf6 100644 --- a/editor/src/main.rs +++ b/editor/src/main.rs @@ -30,7 +30,6 @@ extern crate structopt; extern crate strum; #[macro_use] extern crate strum_macros; -extern crate yansi; #[macro_use] mod macros; diff --git a/editor/src/plugins/logs.rs b/editor/src/plugins/logs.rs index 6fdfd172ed..ff0b00eeb2 100644 --- a/editor/src/plugins/logs.rs +++ b/editor/src/plugins/logs.rs @@ -1,3 +1,4 @@ +use abstutil::format_log_record; use ezgui::{Canvas, GfxCtx, LogScroller, UserInput}; use log; use log::{LevelFilter, Log, Metadata, Record}; @@ -62,20 +63,8 @@ impl Log for LogAdapter { } fn log(&self, record: &Record) { - use yansi::Paint; + println!("{}", format_log_record(record)); - let line = format!( - "[{}] [{}] {}", - Paint::white(record.level()), - match record.target() { - "UI" => Paint::red("UI"), - "sim" => Paint::green("sim"), - "map" => Paint::blue("map"), - x => Paint::cyan(x), - }, - record.args() - ); - println!("{}", line); // TODO could handle newlines here LOGGER.lock().unwrap().add_line(&format!( "[{}] [{}] {}", diff --git a/headless/Cargo.toml b/headless/Cargo.toml index 1f48ae9da4..376ac52a66 100644 --- a/headless/Cargo.toml +++ b/headless/Cargo.toml @@ -12,4 +12,3 @@ map_model = { path = "../map_model" } pretty_assertions = "0.5.1" sim = { path = "../sim" } structopt = "0.2" -yansi = "0.4.0" diff --git a/headless/src/main.rs b/headless/src/main.rs index f34d41f7c5..eced520b85 100644 --- a/headless/src/main.rs +++ b/headless/src/main.rs @@ -8,9 +8,9 @@ extern crate map_model; extern crate sim; #[macro_use] extern crate structopt; -extern crate yansi; -use log::{LevelFilter, Log, Metadata, Record}; +use abstutil::LogAdapter; +use log::LevelFilter; use sim::SimFlags; use structopt::StructOpt; @@ -78,31 +78,3 @@ fn main() { sim::save_backtraces("call_graph.json"); println!("{:?}", sim.get_score()); } - -// TODO This is copied from editor; dedupe how? -struct LogAdapter; - -impl Log for LogAdapter { - fn enabled(&self, _metadata: &Metadata) -> bool { - true - } - - fn log(&self, record: &Record) { - use yansi::Paint; - - let line = format!( - "[{}] [{}] {}", - Paint::white(record.level()), - match record.target() { - "UI" => Paint::red("UI"), - "sim" => Paint::green("sim"), - "map" => Paint::blue("map"), - x => Paint::cyan(x), - }, - record.args() - ); - println!("{}", line); - } - - fn flush(&self) {} -}