diff --git a/Cargo.lock b/Cargo.lock index ccf75d182..32bc947c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,6 +70,7 @@ version = "0.1.0" dependencies = [ "console_error_panic_hook", "graphite-editor-core", + "log", "wasm-bindgen", "wasm-bindgen-test", ] diff --git a/client/web/wasm/Cargo.toml b/client/web/wasm/Cargo.toml index 5c5e5de45..d7d8ea256 100644 --- a/client/web/wasm/Cargo.toml +++ b/client/web/wasm/Cargo.toml @@ -19,6 +19,7 @@ default = ["console_error_panic_hook"] console_error_panic_hook = { version = "0.1.6", optional = true } editor-core = { path = "../../../core/editor", package = "graphite-editor-core" } wasm-bindgen = "0.2.72" +log = "0.4" [dev-dependencies] wasm-bindgen-test = "0.3.22" diff --git a/client/web/wasm/src/lib.rs b/client/web/wasm/src/lib.rs index b29fba464..5c81cb0ab 100644 --- a/client/web/wasm/src/lib.rs +++ b/client/web/wasm/src/lib.rs @@ -4,16 +4,20 @@ pub mod utils; pub mod window; pub mod wrappers; -use editor_core::{events::Response, Callback, Editor}; +use editor_core::{events::Response, Editor}; use std::cell::RefCell; +use utils::WasmLog; use wasm_bindgen::prelude::*; // the thread_local macro provides a way to initialize static variables with non-constant functions thread_local! { pub static EDITOR_STATE: RefCell = RefCell::new(Editor::new(Box::new(handle_response))) } +static LOGGER: WasmLog = WasmLog; #[wasm_bindgen(start)] pub fn init() { utils::set_panic_hook(); + log::set_logger(&LOGGER).expect("Failed to set logger"); + log::set_max_level(log::LevelFilter::Debug); } fn handle_response(response: Response) { diff --git a/client/web/wasm/src/utils.rs b/client/web/wasm/src/utils.rs index 6c8519d18..113be6c7b 100644 --- a/client/web/wasm/src/utils.rs +++ b/client/web/wasm/src/utils.rs @@ -1,3 +1,4 @@ +use wasm_bindgen::prelude::*; pub fn set_panic_hook() { // When the `console_error_panic_hook` feature is enabled, we can call the // `set_panic_hook` function at least once during initialization, and then @@ -8,3 +9,37 @@ pub fn set_panic_hook() { #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); } + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn debug(msg: &str, format: &str); + #[wasm_bindgen(js_namespace = console)] + fn info(msg: &str, format: &str); + #[wasm_bindgen(js_namespace = console)] + fn warn(msg: &str, format: &str); + #[wasm_bindgen(js_namespace = console)] + fn error(msg: &str, format: &str); +} + +#[derive(Default)] +pub struct WasmLog; + +impl log::Log for WasmLog { + fn enabled(&self, metadata: &log::Metadata) -> bool { + metadata.level() <= log::Level::Info + } + + fn log(&self, record: &log::Record) { + let (log, name, color): (fn(&str, &str), &str, &str) = match record.level() { + log::Level::Trace => (debug, "trace", "color:plum"), + log::Level::Debug => (debug, "debug", "color:plum"), + log::Level::Warn => (warn, "warn", "color:#1b8"), + log::Level::Info => (info, "info", "color:#fa2"), + log::Level::Error => (error, "error", "color:red"), + }; + let msg = &format!("{}", format_args!("%c{}%c\t{}", name, record.args())); + log(msg, color) + } + fn flush(&self) {} +} diff --git a/core/editor/src/dispatcher/mod.rs b/core/editor/src/dispatcher/mod.rs index d3aeec02d..f4adc089d 100644 --- a/core/editor/src/dispatcher/mod.rs +++ b/core/editor/src/dispatcher/mod.rs @@ -10,6 +10,7 @@ pub struct Dispatcher { impl Dispatcher { pub fn handle_event(&self, state: &mut EditorState, event: Event) -> Result<(), EditorError> { + log::trace!("{:?}", event); match event { Event::SelectTool(tool_type) => { state.tools.active_tool = tool_type;