From e2e4f0f698b95ca979e78ac12fa2395635321dd3 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sat, 25 May 2019 23:47:02 +0200 Subject: Add colored logging with fern --- WebInterface/wasm/asm-paint/Cargo.toml | 20 +++++++++++++- WebInterface/wasm/asm-paint/index.html | 1 + WebInterface/wasm/asm-paint/src/client_logger.rs | 34 ++++++++++++++++++++++++ WebInterface/wasm/asm-paint/src/lib.rs | 20 +++++--------- 4 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 WebInterface/wasm/asm-paint/src/client_logger.rs diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index c0a7c68..e8f5acb 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "asm-paint-rs" version = "0.1.0" -authors = ["Dennis Kobert "] +authors = [ + "natrixaeria", + "Dennis Kobert " +] edition = "2018" [lib] @@ -9,3 +12,18 @@ crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2" +log = "0.4" +fern = "0.5" + +[dependencies.web-sys] +version = "0.3" +features = [ + 'Document', + 'Element', + 'HtmlCanvasElement', + 'WebGlBuffer', + 'WebGlRenderingContext', + 'WebGlProgram', + 'WebGlShader', + 'Window' +] diff --git a/WebInterface/wasm/asm-paint/index.html b/WebInterface/wasm/asm-paint/index.html index 46be213..631122d 100644 --- a/WebInterface/wasm/asm-paint/index.html +++ b/WebInterface/wasm/asm-paint/index.html @@ -6,5 +6,6 @@ + diff --git a/WebInterface/wasm/asm-paint/src/client_logger.rs b/WebInterface/wasm/asm-paint/src/client_logger.rs new file mode 100644 index 0000000..a8765c6 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/client_logger.rs @@ -0,0 +1,34 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace=console, js_name=log)] + fn __console_log_colored2(f: &str, c1: &str, c2: &str); +} + +fn log(rec: &log::Record) { + __console_log_colored2(&format!("{}", rec.args()), + &format!("color: {}", match rec.level() { + log::Level::Trace => "violet", + log::Level::Debug => "blue", + log::Level::Info => "green", + log::Level::Warn => "orange", + log::Level::Error => "red" + }), ""); +} + +pub fn init_logger() { + fern::Dispatch::new().format(|out, message, record|{ + out.finish(format_args!( + "%c{}%c {} > {}", + record.level(), + record.target(), + message + ) + ) + }) + .level(log::LevelFilter::Debug) + .chain(fern::Output::call(log)) + .apply().unwrap(); +} + diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index b6a5840..fea9d0f 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -1,21 +1,13 @@ -use wasm_bindgen::prelude::*; - -macro_rules! console_log { - ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) -} +mod client_logger; -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(js_namespace = console)] - fn log(s: &str); +use wasm_bindgen::prelude::*; - #[wasm_bindgen(js_namespace = document)] - fn write(s: &str); -} +#[macro_use] +extern crate log; #[wasm_bindgen(start)] pub fn entry() { - console_log!("hello {} wasm", 44); + client_logger::init_logger(); - write("gooo"); + info!("{}", 42); } -- cgit v1.2.3-54-g00ecf