mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2025-12-23 10:11:54 +00:00
Add loging implementation for wasm (#56)
This commit is contained in:
parent
b5cd8327dd
commit
593572a4e8
5 changed files with 43 additions and 1 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -70,6 +70,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"console_error_panic_hook",
|
||||
"graphite-editor-core",
|
||||
"log",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-test",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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<Editor> = 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) {
|
||||
|
|
|
|||
|
|
@ -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) {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue