mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-25 05:48:11 +08:00
Add loging implementation for wasm (#56)
This commit is contained in:
Generated
+1
@@ -70,6 +70,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"console_error_panic_hook",
|
"console_error_panic_hook",
|
||||||
"graphite-editor-core",
|
"graphite-editor-core",
|
||||||
|
"log",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"wasm-bindgen-test",
|
"wasm-bindgen-test",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ default = ["console_error_panic_hook"]
|
|||||||
console_error_panic_hook = { version = "0.1.6", optional = true }
|
console_error_panic_hook = { version = "0.1.6", optional = true }
|
||||||
editor-core = { path = "../../../core/editor", package = "graphite-editor-core" }
|
editor-core = { path = "../../../core/editor", package = "graphite-editor-core" }
|
||||||
wasm-bindgen = "0.2.72"
|
wasm-bindgen = "0.2.72"
|
||||||
|
log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
wasm-bindgen-test = "0.3.22"
|
wasm-bindgen-test = "0.3.22"
|
||||||
|
|||||||
@@ -4,16 +4,20 @@ pub mod utils;
|
|||||||
pub mod window;
|
pub mod window;
|
||||||
pub mod wrappers;
|
pub mod wrappers;
|
||||||
|
|
||||||
use editor_core::{events::Response, Callback, Editor};
|
use editor_core::{events::Response, Editor};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use utils::WasmLog;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
// the thread_local macro provides a way to initialize static variables with non-constant functions
|
// the thread_local macro provides a way to initialize static variables with non-constant functions
|
||||||
thread_local! { pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new(Box::new(handle_response))) }
|
thread_local! { pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new(Box::new(handle_response))) }
|
||||||
|
static LOGGER: WasmLog = WasmLog;
|
||||||
|
|
||||||
#[wasm_bindgen(start)]
|
#[wasm_bindgen(start)]
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
utils::set_panic_hook();
|
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) {
|
fn handle_response(response: Response) {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use wasm_bindgen::prelude::*;
|
||||||
pub fn set_panic_hook() {
|
pub fn set_panic_hook() {
|
||||||
// When the `console_error_panic_hook` feature is enabled, we can call the
|
// When the `console_error_panic_hook` feature is enabled, we can call the
|
||||||
// `set_panic_hook` function at least once during initialization, and then
|
// `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")]
|
#[cfg(feature = "console_error_panic_hook")]
|
||||||
console_error_panic_hook::set_once();
|
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) {}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ pub struct Dispatcher {
|
|||||||
|
|
||||||
impl Dispatcher {
|
impl Dispatcher {
|
||||||
pub fn handle_event(&self, state: &mut EditorState, event: Event) -> Result<(), EditorError> {
|
pub fn handle_event(&self, state: &mut EditorState, event: Event) -> Result<(), EditorError> {
|
||||||
|
log::trace!("{:?}", event);
|
||||||
match event {
|
match event {
|
||||||
Event::SelectTool(tool_type) => {
|
Event::SelectTool(tool_type) => {
|
||||||
state.tools.active_tool = tool_type;
|
state.tools.active_tool = tool_type;
|
||||||
|
|||||||
Reference in New Issue
Block a user