refactor log adaptor stuff

This commit is contained in:
Dustin Carlino 2018-10-21 11:25:26 -07:00
parent 68b0983436
commit def2f60a92
8 changed files with 40 additions and 46 deletions

View File

@ -4,7 +4,9 @@ version = "0.1.0"
authors = ["Dustin Carlino <dabreegster@gmail.com>"] authors = ["Dustin Carlino <dabreegster@gmail.com>"]
[dependencies] [dependencies]
log = "0.4.5"
multimap = "0.4.0" multimap = "0.4.0"
serde = "1.0" serde = "1.0"
serde_cbor = "0.8.2" serde_cbor = "0.8.2"
serde_json = "1.0" serde_json = "1.0"
yansi = "0.4.0"

View File

@ -1,12 +1,15 @@
extern crate log;
extern crate multimap; extern crate multimap;
extern crate serde; extern crate serde;
extern crate serde_cbor; extern crate serde_cbor;
extern crate serde_json; extern crate serde_json;
extern crate yansi;
mod abst_multimap; mod abst_multimap;
mod clone; mod clone;
mod error; mod error;
mod io; mod io;
mod logs;
mod time; mod time;
pub use abst_multimap::MultiMap; pub use abst_multimap::MultiMap;
@ -17,4 +20,5 @@ pub use io::{
read_json, save_object, serialize_btreemap, serialize_multimap, to_json, write_binary, read_json, save_object, serialize_btreemap, serialize_multimap, to_json, write_binary,
write_json, write_json,
}; };
pub use logs::{format_log_record, LogAdapter};
pub use time::elapsed_seconds; pub use time::elapsed_seconds;

30
abstutil/src/logs.rs Normal file
View File

@ -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()
)
}

View File

@ -28,4 +28,3 @@ sim = { path = "../sim" }
structopt = "0.2" structopt = "0.2"
strum = "0.9.0" strum = "0.9.0"
strum_macros = "0.9.0" strum_macros = "0.9.0"
yansi = "0.4.0"

View File

@ -30,7 +30,6 @@ extern crate structopt;
extern crate strum; extern crate strum;
#[macro_use] #[macro_use]
extern crate strum_macros; extern crate strum_macros;
extern crate yansi;
#[macro_use] #[macro_use]
mod macros; mod macros;

View File

@ -1,3 +1,4 @@
use abstutil::format_log_record;
use ezgui::{Canvas, GfxCtx, LogScroller, UserInput}; use ezgui::{Canvas, GfxCtx, LogScroller, UserInput};
use log; use log;
use log::{LevelFilter, Log, Metadata, Record}; use log::{LevelFilter, Log, Metadata, Record};
@ -62,20 +63,8 @@ impl Log for LogAdapter {
} }
fn log(&self, record: &Record) { 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 // TODO could handle newlines here
LOGGER.lock().unwrap().add_line(&format!( LOGGER.lock().unwrap().add_line(&format!(
"[{}] [{}] {}", "[{}] [{}] {}",

View File

@ -12,4 +12,3 @@ map_model = { path = "../map_model" }
pretty_assertions = "0.5.1" pretty_assertions = "0.5.1"
sim = { path = "../sim" } sim = { path = "../sim" }
structopt = "0.2" structopt = "0.2"
yansi = "0.4.0"

View File

@ -8,9 +8,9 @@ extern crate map_model;
extern crate sim; extern crate sim;
#[macro_use] #[macro_use]
extern crate structopt; extern crate structopt;
extern crate yansi;
use log::{LevelFilter, Log, Metadata, Record}; use abstutil::LogAdapter;
use log::LevelFilter;
use sim::SimFlags; use sim::SimFlags;
use structopt::StructOpt; use structopt::StructOpt;
@ -78,31 +78,3 @@ fn main() {
sim::save_backtraces("call_graph.json"); sim::save_backtraces("call_graph.json");
println!("{:?}", sim.get_score()); 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) {}
}