From de14e5829b39e7651fed30cd9e399251746755fc Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 00:06:51 +0100 Subject: [PATCH 1/9] egui_web: make console_log and friends macros --- egui_web/src/backend.rs | 4 +-- egui_web/src/glow_wrapping.rs | 8 +++--- egui_web/src/lib.rs | 50 ++++++++++++++++++++--------------- egui_web/src/screen_reader.rs | 8 +++--- egui_web/src/webgl1.rs | 2 +- 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index de5bb5db730..80573ad9f15 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -14,10 +14,10 @@ fn create_painter(canvas_id: &str) -> Result, JsValue> { #[cfg(all(feature = "webgl", not(feature = "glow")))] if let Ok(webgl2_painter) = webgl2::WebGl2Painter::new(canvas_id) { - console_log("Using WebGL2 backend"); + console_log!("Using WebGL2 backend"); Ok(Box::new(webgl2_painter)) } else { - console_log("Falling back to WebGL1 backend"); + console_log!("Falling back to WebGL1 backend"); let webgl1_painter = webgl1::WebGlPainter::new(canvas_id)?; Ok(Box::new(webgl1_painter)) } diff --git a/egui_web/src/glow_wrapping.rs b/egui_web/src/glow_wrapping.rs index 3e27d56a392..b1cdff45075 100644 --- a/egui_web/src/glow_wrapping.rs +++ b/egui_web/src/glow_wrapping.rs @@ -93,7 +93,7 @@ fn init_glow_context_from_canvas( init_webgl2(canvas).or_else(|| init_webgl1(canvas)) } else { // Trying WebGl1 first (useful for testing). - crate::console_warn("Looking for WebGL1 first"); + crate::console_warn!("Looking for WebGL1 first"); init_webgl1(canvas).or_else(|| init_webgl2(canvas)) }; @@ -110,14 +110,14 @@ fn init_webgl1(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static st .expect("Failed to query about WebGL2 context"); let gl1_ctx = gl1_ctx?; - crate::console_log("WebGL1 selected."); + crate::console_log!("WebGL1 selected."); let gl1_ctx = gl1_ctx .dyn_into::() .unwrap(); let shader_prefix = if crate::webgl1_requires_brightening(&gl1_ctx) { - crate::console_log("Enabling webkitGTK brightening workaround."); + crate::console_log!("Enabling webkitGTK brightening workaround."); "#define APPLY_BRIGHTENING_GAMMA" } else { "" @@ -134,7 +134,7 @@ fn init_webgl2(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static st .expect("Failed to query about WebGL2 context"); let gl2_ctx = gl2_ctx?; - crate::console_log("WebGL2 selected."); + crate::console_log!("WebGL2 selected."); let gl2_ctx = gl2_ctx .dyn_into::() diff --git a/egui_web/src/lib.rs b/egui_web/src/lib.rs index 3faff02ecde..60138266d78 100644 --- a/egui_web/src/lib.rs +++ b/egui_web/src/lib.rs @@ -42,21 +42,31 @@ static AGENT_ID: &str = "egui_text_agent"; // ---------------------------------------------------------------------------- // Helpers to hide some of the verbosity of web_sys -/// Log some text to the developer console (`console.log(…)` in JS) -pub fn console_log(s: impl Into) { - web_sys::console::log_1(&s.into()); +#[macro_export] +macro_rules! console_log { + ($($t:tt)*) => (web_sys::console::log_1(&format!($($t)*).into())) } -/// Log a warning to the developer console (`console.warn(…)` in JS) -pub fn console_warn(s: impl Into) { - web_sys::console::warn_1(&s.into()); +#[macro_export] +macro_rules! console_warn { + ($($t:tt)*) => (web_sys::console::warn_1(&format!($($t)*).into())) } -/// Log an error to the developer console (`console.error(…)` in JS) -pub fn console_error(s: impl Into) { - web_sys::console::error_1(&s.into()); +#[macro_export] +macro_rules! console_error { + ($($t:tt)*) => (web_sys::console::error_1(&format!($($t)*).into())) } +pub fn string_from_js_value(s: wasm_bindgen::JsValue) -> String { + s.as_string().unwrap_or(format!("{:#?}", s)) +} + +pub fn string_from_js_string(s: js_sys::JsString) -> String { + s.as_string().unwrap_or(format!("{:#?}", s)) +} + +// ---------------------------------------------------------------------------- + /// Current time in seconds (since undefined point in time) pub fn now_sec() -> f64 { web_sys::window() @@ -259,7 +269,7 @@ pub fn load_memory(ctx: &egui::Context) { *ctx.memory() = memory; } Err(err) => { - console_error(format!("Failed to parse memory RON: {}", err)); + console_error!("Failed to parse memory RON: {}", err); } } } @@ -275,7 +285,7 @@ pub fn save_memory(ctx: &egui::Context) { local_storage_set("egui_memory_ron", &ron); } Err(err) => { - console_error(format!("Failed to serialize memory as RON: {}", err)); + console_error!("Failed to serialize memory as RON: {}", err); } } } @@ -315,7 +325,7 @@ pub fn set_clipboard_text(s: &str) { let future = wasm_bindgen_futures::JsFuture::from(promise); let future = async move { if let Err(err) = future.await { - console_error(format!("Copy/cut action denied: {:?}", err)); + console_error!("Copy/cut action denied: {:?}", err); } }; wasm_bindgen_futures::spawn_local(future); @@ -575,12 +585,12 @@ fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { false }; - // console_log(format!( + // console_log!( // "On key-down {:?}, egui_wants_keyboard: {}, prevent_default: {}", // event.key().as_str(), // egui_wants_keyboard, // prevent_default - // )); + // ); if prevent_default { event.prevent_default(); @@ -780,7 +790,7 @@ fn install_text_agent(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { } "compositionupdate" => event.data().map(egui::Event::CompositionUpdate), s => { - console_error(format!("Unknown composition event type: {:?}", s)); + console_error!("Unknown composition event type: {:?}", s); None } }; @@ -1128,7 +1138,7 @@ fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { let last_modified = std::time::UNIX_EPOCH + std::time::Duration::from_millis(file.last_modified() as u64); - console_log(format!("Loading {:?} ({} bytes)…", name, file.size())); + console_log!("Loading {:?} ({} bytes)…", name, file.size()); let future = wasm_bindgen_futures::JsFuture::from(file.array_buffer()); @@ -1137,11 +1147,7 @@ fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { match future.await { Ok(array_buffer) => { let bytes = js_sys::Uint8Array::new(&array_buffer).to_vec(); - console_log(format!( - "Loaded {:?} ({} bytes).", - name, - bytes.len() - )); + console_log!("Loaded {:?} ({} bytes).", name, bytes.len()); let mut runner_lock = runner_ref.0.lock(); runner_lock.input.raw.dropped_files.push( @@ -1155,7 +1161,7 @@ fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { runner_lock.needs_repaint.set_true(); } Err(err) => { - console_error(format!("Failed to read file: {:?}", err)); + console_error!("Failed to read file: {:?}", err); } } }; diff --git a/egui_web/src/screen_reader.rs b/egui_web/src/screen_reader.rs index e8f3ac95c75..deaceffe11d 100644 --- a/egui_web/src/screen_reader.rs +++ b/egui_web/src/screen_reader.rs @@ -15,11 +15,11 @@ impl Default for ScreenReader { fn default() -> Self { let tts = match tts::Tts::default() { Ok(screen_reader) => { - crate::console_log("Initialized screen reader."); + crate::console_log!("Initialized screen reader."); Some(screen_reader) } Err(err) => { - crate::console_warn(format!("Failed to load screen reader: {}", err)); + crate::console_warn!("Failed to load screen reader: {}", err); None } }; @@ -37,10 +37,10 @@ impl ScreenReader { return; } if let Some(tts) = &mut self.tts { - crate::console_log(format!("Speaking: {:?}", text)); + crate::console_log!("Speaking: {:?}", text); let interrupt = true; if let Err(err) = tts.speak(text, interrupt) { - crate::console_warn(format!("Failed to read: {}", err)); + crate::console_warn!("Failed to read: {}", err); } } } diff --git a/egui_web/src/webgl1.rs b/egui_web/src/webgl1.rs index 6c2d4d86a9e..91f8facbb0f 100644 --- a/egui_web/src/webgl1.rs +++ b/egui_web/src/webgl1.rs @@ -499,7 +499,7 @@ impl PostProcess { gl.bind_framebuffer(Gl::FRAMEBUFFER, None); let shader_prefix = if crate::webgl1_requires_brightening(&gl) { - crate::console_log("Enabling webkitGTK brightening workaround"); + crate::console_log!("Enabling webkitGTK brightening workaround"); "#define APPLY_BRIGHTENING_GAMMA" } else { "" From bb89a5537b7934dcaf8be636403f28f34fd8d788 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 00:20:04 +0100 Subject: [PATCH 2/9] egui_web: use tracing crate --- Cargo.lock | 81 +++++++++++++++++++++++++++++++++++ egui_web/Cargo.toml | 8 +++- egui_web/src/backend.rs | 7 ++- egui_web/src/glow_wrapping.rs | 8 ++-- egui_web/src/lib.rs | 36 ++++++---------- egui_web/src/screen_reader.rs | 8 ++-- egui_web/src/webgl1.rs | 7 +-- egui_web/src/webgl2.rs | 5 +-- 8 files changed, 115 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8d14654b52d..66c98494759 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -880,6 +880,8 @@ dependencies = [ "js-sys", "ron", "serde", + "tracing", + "tracing-wasm", "tts", "wasm-bindgen", "wasm-bindgen-futures", @@ -1845,6 +1847,12 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +[[package]] +name = "pin-project-lite" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c" + [[package]] name = "pkg-config" version = "0.3.24" @@ -2199,6 +2207,15 @@ dependencies = [ "serde", ] +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + [[package]] name = "shared_library" version = "0.1.9" @@ -2416,6 +2433,15 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" +dependencies = [ + "once_cell", +] + [[package]] name = "time" version = "0.1.43" @@ -2470,6 +2496,61 @@ dependencies = [ "serde", ] +[[package]] +name = "tracing" +version = "0.1.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105" +dependencies = [ + "cfg-if 1.0.0", + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f480b8f81512e825f337ad51e94c1eb5d3bbdf2b363dcd01e2b19a9ffe3f8e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5312f325fe3588e277415f5a6cca1f4ccad0f248c4cd5a4bd33032d7286abc22" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", +] + +[[package]] +name = "tracing-wasm" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4575c663a174420fa2d78f4108ff68f65bf2fbb7dd89f33749b6e826b3626e07" +dependencies = [ + "tracing", + "tracing-subscriber", + "wasm-bindgen", +] + [[package]] name = "ttf-parser" version = "0.14.0" diff --git a/egui_web/Cargo.toml b/egui_web/Cargo.toml index ee2659c6cb4..0f7e01b45e7 100644 --- a/egui_web/Cargo.toml +++ b/egui_web/Cargo.toml @@ -36,11 +36,15 @@ epi = { version = "0.16.0", path = "../epi" } bytemuck = "1.7" console_error_panic_hook = "0.1.6" js-sys = "0.3" +tracing = { version = "0.1", features = ["log"] } +tracing-wasm = "0.2" +wasm-bindgen = "0.2" +wasm-bindgen-futures = "0.4" + +# Optional: ron = { version = "0.7", optional = true } serde = { version = "1", optional = true } tts = { version = "0.19", optional = true } # feature screen_reader -wasm-bindgen = "0.2" -wasm-bindgen-futures = "0.4" [features] default = ["default_fonts", "glow"] diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index 80573ad9f15..129956531f7 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -14,10 +14,10 @@ fn create_painter(canvas_id: &str) -> Result, JsValue> { #[cfg(all(feature = "webgl", not(feature = "glow")))] if let Ok(webgl2_painter) = webgl2::WebGl2Painter::new(canvas_id) { - console_log!("Using WebGL2 backend"); + tracing::info!("Using WebGL2 backend"); Ok(Box::new(webgl2_painter)) } else { - console_log!("Falling back to WebGL1 backend"); + tracing::info!("Falling back to WebGL1 backend"); let webgl1_painter = webgl1::WebGlPainter::new(canvas_id)?; Ok(Box::new(webgl1_painter)) } @@ -284,6 +284,9 @@ pub fn start(canvas_id: &str, app: Box) -> Result Option<(glow::Context, &'static st .expect("Failed to query about WebGL2 context"); let gl1_ctx = gl1_ctx?; - crate::console_log!("WebGL1 selected."); + tracing::info!("WebGL1 selected."); let gl1_ctx = gl1_ctx .dyn_into::() .unwrap(); let shader_prefix = if crate::webgl1_requires_brightening(&gl1_ctx) { - crate::console_log!("Enabling webkitGTK brightening workaround."); + tracing::info!("Enabling webkitGTK brightening workaround."); "#define APPLY_BRIGHTENING_GAMMA" } else { "" @@ -134,7 +134,7 @@ fn init_webgl2(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static st .expect("Failed to query about WebGL2 context"); let gl2_ctx = gl2_ctx?; - crate::console_log!("WebGL2 selected."); + tracing::info!("WebGL2 selected."); let gl2_ctx = gl2_ctx .dyn_into::() diff --git a/egui_web/src/lib.rs b/egui_web/src/lib.rs index 60138266d78..105ebf32b59 100644 --- a/egui_web/src/lib.rs +++ b/egui_web/src/lib.rs @@ -40,22 +40,6 @@ use wasm_bindgen::prelude::*; static AGENT_ID: &str = "egui_text_agent"; // ---------------------------------------------------------------------------- -// Helpers to hide some of the verbosity of web_sys - -#[macro_export] -macro_rules! console_log { - ($($t:tt)*) => (web_sys::console::log_1(&format!($($t)*).into())) -} - -#[macro_export] -macro_rules! console_warn { - ($($t:tt)*) => (web_sys::console::warn_1(&format!($($t)*).into())) -} - -#[macro_export] -macro_rules! console_error { - ($($t:tt)*) => (web_sys::console::error_1(&format!($($t)*).into())) -} pub fn string_from_js_value(s: wasm_bindgen::JsValue) -> String { s.as_string().unwrap_or(format!("{:#?}", s)) @@ -269,7 +253,7 @@ pub fn load_memory(ctx: &egui::Context) { *ctx.memory() = memory; } Err(err) => { - console_error!("Failed to parse memory RON: {}", err); + tracing::error!("Failed to parse memory RON: {}", err); } } } @@ -285,7 +269,7 @@ pub fn save_memory(ctx: &egui::Context) { local_storage_set("egui_memory_ron", &ron); } Err(err) => { - console_error!("Failed to serialize memory as RON: {}", err); + tracing::error!("Failed to serialize memory as RON: {}", err); } } } @@ -325,7 +309,7 @@ pub fn set_clipboard_text(s: &str) { let future = wasm_bindgen_futures::JsFuture::from(promise); let future = async move { if let Err(err) = future.await { - console_error!("Copy/cut action denied: {:?}", err); + tracing::error!("Copy/cut action denied: {:?}", err); } }; wasm_bindgen_futures::spawn_local(future); @@ -585,7 +569,7 @@ fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { false }; - // console_log!( + // tracing::info!( // "On key-down {:?}, egui_wants_keyboard: {}, prevent_default: {}", // event.key().as_str(), // egui_wants_keyboard, @@ -790,7 +774,7 @@ fn install_text_agent(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { } "compositionupdate" => event.data().map(egui::Event::CompositionUpdate), s => { - console_error!("Unknown composition event type: {:?}", s); + tracing::error!("Unknown composition event type: {:?}", s); None } }; @@ -1138,7 +1122,7 @@ fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { let last_modified = std::time::UNIX_EPOCH + std::time::Duration::from_millis(file.last_modified() as u64); - console_log!("Loading {:?} ({} bytes)…", name, file.size()); + tracing::info!("Loading {:?} ({} bytes)…", name, file.size()); let future = wasm_bindgen_futures::JsFuture::from(file.array_buffer()); @@ -1147,7 +1131,11 @@ fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { match future.await { Ok(array_buffer) => { let bytes = js_sys::Uint8Array::new(&array_buffer).to_vec(); - console_log!("Loaded {:?} ({} bytes).", name, bytes.len()); + tracing::info!( + "Loaded {:?} ({} bytes).", + name, + bytes.len() + ); let mut runner_lock = runner_ref.0.lock(); runner_lock.input.raw.dropped_files.push( @@ -1161,7 +1149,7 @@ fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { runner_lock.needs_repaint.set_true(); } Err(err) => { - console_error!("Failed to read file: {:?}", err); + tracing::error!("Failed to read file: {:?}", err); } } }; diff --git a/egui_web/src/screen_reader.rs b/egui_web/src/screen_reader.rs index deaceffe11d..4974e0e7959 100644 --- a/egui_web/src/screen_reader.rs +++ b/egui_web/src/screen_reader.rs @@ -15,11 +15,11 @@ impl Default for ScreenReader { fn default() -> Self { let tts = match tts::Tts::default() { Ok(screen_reader) => { - crate::console_log!("Initialized screen reader."); + tracing::info!("Initialized screen reader."); Some(screen_reader) } Err(err) => { - crate::console_warn!("Failed to load screen reader: {}", err); + tracing::warn!("Failed to load screen reader: {}", err); None } }; @@ -37,10 +37,10 @@ impl ScreenReader { return; } if let Some(tts) = &mut self.tts { - crate::console_log!("Speaking: {:?}", text); + tracing::info!("Speaking: {:?}", text); let interrupt = true; if let Err(err) = tts.speak(text, interrupt) { - crate::console_warn!("Failed to read: {}", err); + tracing::warn!("Failed to read: {}", err); } } } diff --git a/egui_web/src/webgl1.rs b/egui_web/src/webgl1.rs index 91f8facbb0f..74f86c9d450 100644 --- a/egui_web/src/webgl1.rs +++ b/egui_web/src/webgl1.rs @@ -431,10 +431,7 @@ impl crate::Painter for WebGlPainter { self.paint_mesh(&mesh)?; } } else { - crate::console_warn(format!( - "WebGL: Failed to find texture {:?}", - mesh.texture_id - )); + tracing::warn!("WebGL: Failed to find texture {:?}", mesh.texture_id); } } @@ -499,7 +496,7 @@ impl PostProcess { gl.bind_framebuffer(Gl::FRAMEBUFFER, None); let shader_prefix = if crate::webgl1_requires_brightening(&gl) { - crate::console_log!("Enabling webkitGTK brightening workaround"); + tracing::info!("Enabling webkitGTK brightening workaround"); "#define APPLY_BRIGHTENING_GAMMA" } else { "" diff --git a/egui_web/src/webgl2.rs b/egui_web/src/webgl2.rs index 848d355c2c0..5c8316ac03b 100644 --- a/egui_web/src/webgl2.rs +++ b/egui_web/src/webgl2.rs @@ -410,10 +410,7 @@ impl crate::Painter for WebGl2Painter { self.paint_mesh(&mesh)?; } } else { - crate::console_warn(format!( - "WebGL: Failed to find texture {:?}", - mesh.texture_id - )); + tracing::warn!("WebGL: Failed to find texture {:?}", mesh.texture_id); } } From 9f22d4472cd9d3dac079bea5874b40def15ec532 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 00:23:23 +0100 Subject: [PATCH 3/9] egui_glow: use tracing crate --- Cargo.lock | 1 + egui_glow/Cargo.toml | 1 + egui_glow/src/lib.rs | 5 +---- egui_glow/src/misc_util.rs | 26 ++++++-------------------- egui_glow/src/painter.rs | 8 ++++---- egui_glow/src/shader_version.rs | 9 +++++---- 6 files changed, 18 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 66c98494759..52468ca4a89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -864,6 +864,7 @@ dependencies = [ "glutin", "image", "memoffset", + "tracing", "wasm-bindgen", "web-sys", ] diff --git a/egui_glow/Cargo.toml b/egui_glow/Cargo.toml index 1a5195459e3..6f76413924d 100644 --- a/egui_glow/Cargo.toml +++ b/egui_glow/Cargo.toml @@ -32,6 +32,7 @@ epi = { version = "0.16.0", path = "../epi", optional = true } bytemuck = "1.7" glow = "0.11" memoffset = "0.6" +tracing = { version = "0.1", features = ["log"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] egui-winit = { version = "0.16.0", path = "../egui-winit", default-features = false, features = ["epi"], optional = true } diff --git a/egui_glow/src/lib.rs b/egui_glow/src/lib.rs index 39c493c2c88..84b912093a0 100644 --- a/egui_glow/src/lib.rs +++ b/egui_glow/src/lib.rs @@ -124,10 +124,7 @@ impl EguiGlow { pub fn new(window: &winit::window::Window, gl: &glow::Context) -> Self { let painter = crate::Painter::new(gl, None, "") .map_err(|error| { - crate::misc_util::glow_print_error(format!( - "error occurred in initializing painter:\n{}", - error - )); + tracing::error!("error occurred in initializing painter:\n{}", error); }) .unwrap(); diff --git a/egui_glow/src/misc_util.rs b/egui_glow/src/misc_util.rs index 83c736e2648..f3c1c6f0469 100644 --- a/egui_glow/src/misc_util.rs +++ b/egui_glow/src/misc_util.rs @@ -5,29 +5,15 @@ use std::option::Option::Some; pub fn check_for_gl_error(gl: &glow::Context, context: &str) { let error_code = unsafe { gl.get_error() }; if error_code != glow::NO_ERROR { - glow_print_error(format!( + tracing::error!( "GL error, at: '{}', code: {} (0x{:X})", - context, error_code, error_code - )); + context, + error_code, + error_code + ); } } -pub(crate) fn glow_print(s: impl std::fmt::Display) { - #[cfg(target_arch = "wasm32")] - web_sys::console::log_1(&format!("egui_glow: {}", s).into()); - - #[cfg(not(target_arch = "wasm32"))] - eprintln!("egui_glow: {}", s); -} - -pub(crate) fn glow_print_error(s: impl std::fmt::Display) { - #[cfg(target_arch = "wasm32")] - web_sys::console::error_1(&format!("egui_glow: {}", s).into()); - - #[cfg(not(target_arch = "wasm32"))] - eprintln!("egui_glow ERROR: {}", s); -} - pub(crate) unsafe fn compile_shader( gl: &glow::Context, shader_type: u32, @@ -130,7 +116,7 @@ pub(crate) fn supports_vao(gl: &glow::Context) -> bool { const OPENGL_ES_PREFIX: &str = "OpenGL ES "; let version_string = unsafe { gl.get_parameter_string(glow::VERSION) }; - glow_print(format!("GL version: {:?}.", version_string)); + tracing::info!("GL version: {:?}.", version_string); // Examples: // * "WebGL 2.0 (OpenGL ES 3.0 Chromium)" diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index 1e461838648..6e2c4d6e53d 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -9,7 +9,7 @@ use egui::{ use glow::HasContext; use memoffset::offset_of; -use crate::misc_util::{check_for_gl_error, compile_shader, glow_print, link_program}; +use crate::misc_util::{check_for_gl_error, compile_shader, link_program}; use crate::post_process::PostProcess; use crate::shader_version::ShaderVersion; use crate::vao_emulate; @@ -98,7 +98,7 @@ impl Painter { let shader_version = ShaderVersion::get(gl); let is_webgl_1 = shader_version == ShaderVersion::Es100; let header = shader_version.version(); - glow_print(format!("Shader header: {:?}.", header)); + tracing::info!("Shader header: {:?}.", header); let srgb_support = gl.supported_extensions().contains("EXT_sRGB"); let (post_process, srgb_support_define) = match (shader_version, srgb_support) { @@ -106,7 +106,7 @@ impl Painter { (ShaderVersion::Es300, _) | (ShaderVersion::Es100, true) => unsafe { // Add sRGB support marker for fragment shader if let Some([width, height]) = pp_fb_extent { - glow_print("WebGL with sRGB enabled. Turning on post processing for linear framebuffer blending."); + tracing::info!("WebGL with sRGB enabled. Turning on post processing for linear framebuffer blending."); // install post process to correct sRGB color: ( Some(PostProcess::new( @@ -120,7 +120,7 @@ impl Painter { "#define SRGB_SUPPORTED", ) } else { - glow_print("WebGL or OpenGL ES detected but PostProcess disabled because dimension is None"); + tracing::info!("WebGL or OpenGL ES detected but PostProcess disabled because dimension is None"); (None, "") } }, diff --git a/egui_glow/src/shader_version.rs b/egui_glow/src/shader_version.rs index de3a339d1f3..e3199198744 100644 --- a/egui_glow/src/shader_version.rs +++ b/egui_glow/src/shader_version.rs @@ -1,5 +1,5 @@ #![allow(unsafe_code)] -use crate::misc_util::glow_print; + use glow::HasContext; use std::convert::TryInto; @@ -17,10 +17,11 @@ impl ShaderVersion { let shading_lang_string = unsafe { gl.get_parameter_string(glow::SHADING_LANGUAGE_VERSION) }; let shader_version = Self::parse(&shading_lang_string); - glow_print(format!( + tracing::info!( "Shader version: {:?} ({:?}).", - shader_version, shading_lang_string - )); + shader_version, + shading_lang_string + ); shader_version } From 018c5ab1e1c40bdaceae9dabb42defe88a865916 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 00:32:11 +0100 Subject: [PATCH 4/9] Log at the debug level --- docs/index.html | 5 +++-- egui_glow/src/misc_util.rs | 2 +- egui_glow/src/painter.rs | 6 +++--- egui_glow/src/shader_version.rs | 2 +- egui_web/src/backend.rs | 4 ++-- egui_web/src/glow_wrapping.rs | 6 +++--- egui_web/src/lib.rs | 6 +++--- egui_web/src/screen_reader.rs | 4 ++-- egui_web/src/webgl1.rs | 2 +- 9 files changed, 19 insertions(+), 18 deletions(-) diff --git a/docs/index.html b/docs/index.html index c8f66fba2a2..6d3a5588ad0 100644 --- a/docs/index.html +++ b/docs/index.html @@ -122,17 +122,18 @@ // We'll defer our execution until the wasm is ready to go. // Here we tell bindgen the path to the wasm file so it can start // initialization and return to us a promise when it's done. + console.debug("loading wasm…"); wasm_bindgen("./egui_demo_app_bg.wasm") .then(on_wasm_loaded) .catch(on_wasm_error); function on_wasm_loaded() { - console.log("loaded wasm, starting egui app…"); + console.debug("wasm loaded. starting egui app…"); // This call installs a bunch of callbacks and then returns: wasm_bindgen.start("the_canvas_id"); - console.log("egui app started."); + console.debug("egui app started."); document.getElementById("center_text").remove(); } diff --git a/egui_glow/src/misc_util.rs b/egui_glow/src/misc_util.rs index f3c1c6f0469..4547c26187e 100644 --- a/egui_glow/src/misc_util.rs +++ b/egui_glow/src/misc_util.rs @@ -116,7 +116,7 @@ pub(crate) fn supports_vao(gl: &glow::Context) -> bool { const OPENGL_ES_PREFIX: &str = "OpenGL ES "; let version_string = unsafe { gl.get_parameter_string(glow::VERSION) }; - tracing::info!("GL version: {:?}.", version_string); + tracing::debug!("GL version: {:?}.", version_string); // Examples: // * "WebGL 2.0 (OpenGL ES 3.0 Chromium)" diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index 6e2c4d6e53d..607a57343d9 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -98,7 +98,7 @@ impl Painter { let shader_version = ShaderVersion::get(gl); let is_webgl_1 = shader_version == ShaderVersion::Es100; let header = shader_version.version(); - tracing::info!("Shader header: {:?}.", header); + tracing::debug!("Shader header: {:?}.", header); let srgb_support = gl.supported_extensions().contains("EXT_sRGB"); let (post_process, srgb_support_define) = match (shader_version, srgb_support) { @@ -106,7 +106,7 @@ impl Painter { (ShaderVersion::Es300, _) | (ShaderVersion::Es100, true) => unsafe { // Add sRGB support marker for fragment shader if let Some([width, height]) = pp_fb_extent { - tracing::info!("WebGL with sRGB enabled. Turning on post processing for linear framebuffer blending."); + tracing::debug!("WebGL with sRGB enabled. Turning on post processing for linear framebuffer blending."); // install post process to correct sRGB color: ( Some(PostProcess::new( @@ -120,7 +120,7 @@ impl Painter { "#define SRGB_SUPPORTED", ) } else { - tracing::info!("WebGL or OpenGL ES detected but PostProcess disabled because dimension is None"); + tracing::debug!("WebGL or OpenGL ES detected but PostProcess disabled because dimension is None"); (None, "") } }, diff --git a/egui_glow/src/shader_version.rs b/egui_glow/src/shader_version.rs index e3199198744..ae4af65ed86 100644 --- a/egui_glow/src/shader_version.rs +++ b/egui_glow/src/shader_version.rs @@ -17,7 +17,7 @@ impl ShaderVersion { let shading_lang_string = unsafe { gl.get_parameter_string(glow::SHADING_LANGUAGE_VERSION) }; let shader_version = Self::parse(&shading_lang_string); - tracing::info!( + tracing::debug!( "Shader version: {:?} ({:?}).", shader_version, shading_lang_string diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index 129956531f7..6cbbe953b6c 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -14,10 +14,10 @@ fn create_painter(canvas_id: &str) -> Result, JsValue> { #[cfg(all(feature = "webgl", not(feature = "glow")))] if let Ok(webgl2_painter) = webgl2::WebGl2Painter::new(canvas_id) { - tracing::info!("Using WebGL2 backend"); + tracing::debug!("Using WebGL2 backend"); Ok(Box::new(webgl2_painter)) } else { - tracing::info!("Falling back to WebGL1 backend"); + tracing::debug!("Falling back to WebGL1 backend"); let webgl1_painter = webgl1::WebGlPainter::new(canvas_id)?; Ok(Box::new(webgl1_painter)) } diff --git a/egui_web/src/glow_wrapping.rs b/egui_web/src/glow_wrapping.rs index 26f54f9d654..839b23e473b 100644 --- a/egui_web/src/glow_wrapping.rs +++ b/egui_web/src/glow_wrapping.rs @@ -110,14 +110,14 @@ fn init_webgl1(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static st .expect("Failed to query about WebGL2 context"); let gl1_ctx = gl1_ctx?; - tracing::info!("WebGL1 selected."); + tracing::debug!("WebGL1 selected."); let gl1_ctx = gl1_ctx .dyn_into::() .unwrap(); let shader_prefix = if crate::webgl1_requires_brightening(&gl1_ctx) { - tracing::info!("Enabling webkitGTK brightening workaround."); + tracing::debug!("Enabling webkitGTK brightening workaround."); "#define APPLY_BRIGHTENING_GAMMA" } else { "" @@ -134,7 +134,7 @@ fn init_webgl2(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static st .expect("Failed to query about WebGL2 context"); let gl2_ctx = gl2_ctx?; - tracing::info!("WebGL2 selected."); + tracing::debug!("WebGL2 selected."); let gl2_ctx = gl2_ctx .dyn_into::() diff --git a/egui_web/src/lib.rs b/egui_web/src/lib.rs index 105ebf32b59..c61a3294d68 100644 --- a/egui_web/src/lib.rs +++ b/egui_web/src/lib.rs @@ -569,7 +569,7 @@ fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { false }; - // tracing::info!( + // tracing::debug!( // "On key-down {:?}, egui_wants_keyboard: {}, prevent_default: {}", // event.key().as_str(), // egui_wants_keyboard, @@ -1122,7 +1122,7 @@ fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { let last_modified = std::time::UNIX_EPOCH + std::time::Duration::from_millis(file.last_modified() as u64); - tracing::info!("Loading {:?} ({} bytes)…", name, file.size()); + tracing::debug!("Loading {:?} ({} bytes)…", name, file.size()); let future = wasm_bindgen_futures::JsFuture::from(file.array_buffer()); @@ -1131,7 +1131,7 @@ fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { match future.await { Ok(array_buffer) => { let bytes = js_sys::Uint8Array::new(&array_buffer).to_vec(); - tracing::info!( + tracing::debug!( "Loaded {:?} ({} bytes).", name, bytes.len() diff --git a/egui_web/src/screen_reader.rs b/egui_web/src/screen_reader.rs index 4974e0e7959..11c36efdc3b 100644 --- a/egui_web/src/screen_reader.rs +++ b/egui_web/src/screen_reader.rs @@ -15,7 +15,7 @@ impl Default for ScreenReader { fn default() -> Self { let tts = match tts::Tts::default() { Ok(screen_reader) => { - tracing::info!("Initialized screen reader."); + tracing::debug!("Initialized screen reader."); Some(screen_reader) } Err(err) => { @@ -37,7 +37,7 @@ impl ScreenReader { return; } if let Some(tts) = &mut self.tts { - tracing::info!("Speaking: {:?}", text); + tracing::debug!("Speaking: {:?}", text); let interrupt = true; if let Err(err) = tts.speak(text, interrupt) { tracing::warn!("Failed to read: {}", err); diff --git a/egui_web/src/webgl1.rs b/egui_web/src/webgl1.rs index 74f86c9d450..9b076a92d7f 100644 --- a/egui_web/src/webgl1.rs +++ b/egui_web/src/webgl1.rs @@ -496,7 +496,7 @@ impl PostProcess { gl.bind_framebuffer(Gl::FRAMEBUFFER, None); let shader_prefix = if crate::webgl1_requires_brightening(&gl) { - tracing::info!("Enabling webkitGTK brightening workaround"); + tracing::debug!("Enabling webkitGTK brightening workaround"); "#define APPLY_BRIGHTENING_GAMMA" } else { "" From 7ac39f747dd21d34d162cd2026334850bf95ad7d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 00:33:41 +0100 Subject: [PATCH 5/9] egui_demo_app: enable tracing to log to stdout --- Cargo.lock | 16 ++++++++++++++++ egui_demo_app/Cargo.toml | 2 ++ egui_demo_app/src/main.rs | 3 +++ 3 files changed, 21 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 52468ca4a89..346b0825455 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -820,6 +820,8 @@ version = "0.16.0" dependencies = [ "eframe", "egui_demo_lib", + "tracing", + "tracing-subscriber", ] [[package]] @@ -2530,15 +2532,29 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "tracing-log" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6923477a48e41c1951f1999ef8bb5a3023eb723ceadafe78ffb65dc366761e3" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + [[package]] name = "tracing-subscriber" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5312f325fe3588e277415f5a6cca1f4ccad0f248c4cd5a4bd33032d7286abc22" dependencies = [ + "ansi_term", "sharded-slab", + "smallvec", "thread_local", "tracing-core", + "tracing-log", ] [[package]] diff --git a/egui_demo_app/Cargo.toml b/egui_demo_app/Cargo.toml index 982faa5b6ba..35f801e7ca7 100644 --- a/egui_demo_app/Cargo.toml +++ b/egui_demo_app/Cargo.toml @@ -17,6 +17,8 @@ eframe = { version = "0.16.0", path = "../eframe" } # eframe = { version = "0.16.0", path = "../eframe", default-features = false, features = ["default_fonts", "egui_glium"] } egui_demo_lib = { version = "0.16.0", path = "../egui_demo_lib", features = ["extra_debug_asserts"] } +tracing-subscriber = "0.3" +tracing = { version = "0.1", features = ["log"] } [features] default = ["persistence"] diff --git a/egui_demo_app/src/main.rs b/egui_demo_app/src/main.rs index 4a0731b6251..1ad429b9b4c 100644 --- a/egui_demo_app/src/main.rs +++ b/egui_demo_app/src/main.rs @@ -7,6 +7,9 @@ // When compiling natively: fn main() { + // Log to stdout (if you run with `RUST_LOG=debug`). + tracing_subscriber::fmt::init(); + let app = egui_demo_lib::WrapApp::default(); let options = eframe::NativeOptions { // Let's show off that we support transparent windows From 58399969e36647a104bee2ed006a7acd545227e0 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 00:37:22 +0100 Subject: [PATCH 6/9] Use tracing in egui-winit --- Cargo.lock | 1 + egui-winit/Cargo.toml | 1 + egui-winit/src/clipboard.rs | 6 +++--- egui-winit/src/lib.rs | 4 ++-- egui-winit/src/screen_reader.rs | 8 ++++---- egui_glow/src/epi_backend.rs | 3 +-- egui_glow/src/painter.rs | 2 +- 7 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 346b0825455..3a8adba9355 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -809,6 +809,7 @@ dependencies = [ "epi", "instant", "serde", + "tracing", "tts", "webbrowser", "winit", diff --git a/egui-winit/Cargo.toml b/egui-winit/Cargo.toml index b64a630b66e..741fc1ddef6 100644 --- a/egui-winit/Cargo.toml +++ b/egui-winit/Cargo.toml @@ -24,6 +24,7 @@ all-features = true [dependencies] egui = { version = "0.16.0", path = "../egui", default-features = false, features = ["single_threaded"] } instant = { version = "0.1", features = ["wasm-bindgen"] } +tracing = { version = "0.1", features = ["log"] } winit = "0.26.1" epi = { version = "0.16.0", path = "../epi", optional = true } diff --git a/egui-winit/src/clipboard.rs b/egui-winit/src/clipboard.rs index 4a70d47fc94..d6e29e33d9c 100644 --- a/egui-winit/src/clipboard.rs +++ b/egui-winit/src/clipboard.rs @@ -29,7 +29,7 @@ impl Clipboard { match clipboard.get_contents() { Ok(contents) => Some(contents), Err(err) => { - eprintln!("Paste error: {}", err); + tracing::error!("Paste error: {}", err); None } } @@ -46,7 +46,7 @@ impl Clipboard { if let Some(clipboard) = &mut self.copypasta { use copypasta::ClipboardProvider as _; if let Err(err) = clipboard.set_contents(text) { - eprintln!("Copy/Cut error: {}", err); + tracing::error!("Copy/Cut error: {}", err); } } @@ -62,7 +62,7 @@ fn init_copypasta() -> Option { match copypasta::ClipboardContext::new() { Ok(clipboard) => Some(clipboard), Err(err) => { - eprintln!("Failed to initialize clipboard: {}", err); + tracing::error!("Failed to initialize clipboard: {}", err); None } } diff --git a/egui-winit/src/lib.rs b/egui-winit/src/lib.rs index 4923601207a..6e19f435b80 100644 --- a/egui-winit/src/lib.rs +++ b/egui-winit/src/lib.rs @@ -577,12 +577,12 @@ impl State { fn open_url_in_browser(_url: &str) { #[cfg(feature = "webbrowser")] if let Err(err) = webbrowser::open(_url) { - eprintln!("Failed to open url: {}", err); + tracing::warn!("Failed to open url: {}", err); } #[cfg(not(feature = "webbrowser"))] { - eprintln!("Cannot open url - feature \"links\" not enabled."); + tracing::warn!("Cannot open url - feature \"links\" not enabled."); } } diff --git a/egui-winit/src/screen_reader.rs b/egui-winit/src/screen_reader.rs index 9397d2ea8b1..e3ac02b1aca 100644 --- a/egui-winit/src/screen_reader.rs +++ b/egui-winit/src/screen_reader.rs @@ -15,11 +15,11 @@ impl Default for ScreenReader { fn default() -> Self { let tts = match tts::Tts::default() { Ok(screen_reader) => { - eprintln!("Initialized screen reader."); + tracing::debug!("Initialized screen reader."); Some(screen_reader) } Err(err) => { - eprintln!("Failed to load screen reader: {}", err); + tracing::warn!("Failed to load screen reader: {}", err); None } }; @@ -38,10 +38,10 @@ impl ScreenReader { return; } if let Some(tts) = &mut self.tts { - eprintln!("Speaking: {:?}", text); + tracing::debug!("Speaking: {:?}", text); let interrupt = true; if let Err(err) = tts.speak(text, interrupt) { - eprintln!("Failed to read: {}", err); + tracing::warn!("Failed to read: {}", err); } } } diff --git a/egui_glow/src/epi_backend.rs b/egui_glow/src/epi_backend.rs index 0a9f381a671..fef06d10684 100644 --- a/egui_glow/src/epi_backend.rs +++ b/egui_glow/src/epi_backend.rs @@ -60,8 +60,7 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { ))); let mut painter = crate::Painter::new(&gl, None, "") - .map_err(|error| eprintln!("some OpenGL error occurred {}\n", error)) - .unwrap(); + .unwrap_or_else(|error| panic!("some OpenGL error occurred {}\n", error)); let mut integration = egui_winit::epi::EpiIntegration::new( "egui_glow", painter.max_texture_side(), diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index 607a57343d9..0f9d5f44723 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -582,7 +582,7 @@ pub fn clear(gl: &glow::Context, dimension: [u32; 2], clear_color: egui::Rgba) { impl Drop for Painter { fn drop(&mut self) { if !self.destroyed { - eprintln!( + tracing::warn!( "You forgot to call destroy() on the egui glow painter. Resources will leak!" ); } From 7a1976a2c2c27bf862e34cf130ee53fefcac0d20 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 00:46:49 +0100 Subject: [PATCH 7/9] don't use tracing-log compatability --- Cargo.lock | 2 -- egui-winit/Cargo.toml | 2 +- egui_demo_app/Cargo.toml | 1 - egui_glow/Cargo.toml | 2 +- egui_web/Cargo.toml | 2 +- 5 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a8adba9355..f4dbab88fdb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -821,7 +821,6 @@ version = "0.16.0" dependencies = [ "eframe", "egui_demo_lib", - "tracing", "tracing-subscriber", ] @@ -2507,7 +2506,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105" dependencies = [ "cfg-if 1.0.0", - "log", "pin-project-lite", "tracing-attributes", "tracing-core", diff --git a/egui-winit/Cargo.toml b/egui-winit/Cargo.toml index 741fc1ddef6..ca7ac9a7bfd 100644 --- a/egui-winit/Cargo.toml +++ b/egui-winit/Cargo.toml @@ -24,7 +24,7 @@ all-features = true [dependencies] egui = { version = "0.16.0", path = "../egui", default-features = false, features = ["single_threaded"] } instant = { version = "0.1", features = ["wasm-bindgen"] } -tracing = { version = "0.1", features = ["log"] } +tracing = "0.1" winit = "0.26.1" epi = { version = "0.16.0", path = "../epi", optional = true } diff --git a/egui_demo_app/Cargo.toml b/egui_demo_app/Cargo.toml index 35f801e7ca7..861e5e08db5 100644 --- a/egui_demo_app/Cargo.toml +++ b/egui_demo_app/Cargo.toml @@ -18,7 +18,6 @@ eframe = { version = "0.16.0", path = "../eframe" } egui_demo_lib = { version = "0.16.0", path = "../egui_demo_lib", features = ["extra_debug_asserts"] } tracing-subscriber = "0.3" -tracing = { version = "0.1", features = ["log"] } [features] default = ["persistence"] diff --git a/egui_glow/Cargo.toml b/egui_glow/Cargo.toml index 6f76413924d..9b307f1c55f 100644 --- a/egui_glow/Cargo.toml +++ b/egui_glow/Cargo.toml @@ -32,7 +32,7 @@ epi = { version = "0.16.0", path = "../epi", optional = true } bytemuck = "1.7" glow = "0.11" memoffset = "0.6" -tracing = { version = "0.1", features = ["log"] } +tracing = "0.1" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] egui-winit = { version = "0.16.0", path = "../egui-winit", default-features = false, features = ["epi"], optional = true } diff --git a/egui_web/Cargo.toml b/egui_web/Cargo.toml index 0f7e01b45e7..cc98b3b19c4 100644 --- a/egui_web/Cargo.toml +++ b/egui_web/Cargo.toml @@ -36,7 +36,7 @@ epi = { version = "0.16.0", path = "../epi" } bytemuck = "1.7" console_error_panic_hook = "0.1.6" js-sys = "0.3" -tracing = { version = "0.1", features = ["log"] } +tracing = "0.1" tracing-wasm = "0.2" wasm-bindgen = "0.2" wasm-bindgen-futures = "0.4" From ceea319c450e2df0b62168552dd2212a372d1aec Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 10:04:05 +0100 Subject: [PATCH 8/9] Add opt-in tracing support to egui --- CHANGELOG.md | 1 + Cargo.lock | 2 ++ eframe/CHANGELOG.md | 1 + egui/Cargo.toml | 10 +++++++--- egui/src/util/id_type_map.rs | 7 ++++--- epaint/src/texture_atlas.rs | 5 ++++- epi/Cargo.toml | 1 + epi/src/file_storage.rs | 9 +++++---- 8 files changed, 25 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a707a3518c..5fe0544a38c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w * Added `TextStyle::resolve`. * `Context::load_texture` to convert an image into a texture which can be displayed using e.g. `ui.image(texture, size)` ([#1110](https://github.com/emilk/egui/pull/1110)). * Added `Ui::add_visible` and `Ui::add_visible_ui`. +* Opt-in dependency on `tracing` crate for logging warnings. * Added `CollapsingHeader::icon` to override the default open/close icon using a custom function. ([1147](https://github.com/emilk/egui/pull/1147)). * Added `Plot::x_axis_formatter` and `Plot::y_axis_formatter` for custom axis labels ([#1130](https://github.com/emilk/egui/pull/1130)). * Added `ui.data()`, `ctx.data()`, `ctx.options()` and `ctx.tessellation_options()` ([#1175](https://github.com/emilk/egui/pull/1175)). diff --git a/Cargo.lock b/Cargo.lock index f4dbab88fdb..6158dedff59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,6 +798,7 @@ dependencies = [ "nohash-hasher", "ron", "serde", + "tracing", ] [[package]] @@ -977,6 +978,7 @@ dependencies = [ "egui", "ron", "serde", + "tracing", ] [[package]] diff --git a/eframe/CHANGELOG.md b/eframe/CHANGELOG.md index 45ed02fe4d3..6b614c1fce2 100644 --- a/eframe/CHANGELOG.md +++ b/eframe/CHANGELOG.md @@ -11,6 +11,7 @@ NOTE: [`egui_web`](../egui_web/CHANGELOG.md), [`egui-winit`](../egui-winit/CHANG * Fix horizontal scrolling direction on Linux. * Added `App::on_exit_event` ([#1038](https://github.com/emilk/egui/pull/1038)) * Shift-scroll will now result in horizontal scrolling on all platforms ([#1136](https://github.com/emilk/egui/pull/1136)). +* Log using the `tracing` crate. Log to stdout by adding `tracing_subscriber::fmt::init();` to your `main`. And run with `RUST_LOG=debug`. ## 0.16.0 - 2021-12-29 diff --git a/egui/Cargo.toml b/egui/Cargo.toml index 6f9ed8b2807..ea442f773d9 100644 --- a/egui/Cargo.toml +++ b/egui/Cargo.toml @@ -28,8 +28,12 @@ epaint = { version = "0.16.0", path = "../epaint", default-features = false } ahash = "0.7" nohash-hasher = "0.2" + +# Optional: ron = { version = "0.7", optional = true } serde = { version = "1", features = ["derive", "rc"], optional = true } +# egui doesn't log much, but when it does, it uses `tracing` +tracing = { version = "0.1", optional = true } [features] default = ["default_fonts", "single_threaded"] @@ -37,6 +41,9 @@ default = ["default_fonts", "single_threaded"] # add compatibility with https://crates.io/crates/cint cint = ["epaint/cint"] +# implement bytemuck on most types. +convert_bytemuck = ["epaint/convert_bytemuck"] + # If set, egui will use `include_bytes!` to bundle some fonts. # If you plan on specifying your own fonts you may disable this feature. default_fonts = ["epaint/default_fonts"] @@ -55,9 +62,6 @@ persistence = ["serde", "epaint/serialize", "ron"] # implement serde on most types. serialize = ["serde", "epaint/serialize"] -# implement bytemuck on most types. -convert_bytemuck = ["epaint/convert_bytemuck"] - # multi_threaded is only needed if you plan to use the same egui::Context # from multiple threads. It comes with a minor performance impact. single_threaded = ["epaint/single_threaded"] diff --git a/egui/src/util/id_type_map.rs b/egui/src/util/id_type_map.rs index 6123e8b3d5a..82ddf931bb8 100644 --- a/egui/src/util/id_type_map.rs +++ b/egui/src/util/id_type_map.rs @@ -275,11 +275,12 @@ impl Element { fn from_ron_str(ron: &str) -> Option { match ron::from_str::(ron) { Ok(value) => Some(value), - Err(err) => { - eprintln!( + Err(_err) => { + #[cfg(feature = "tracing")] + tracing::warn!( "egui: Failed to deserialize {} from memory: {}, ron error: {:?}", std::any::type_name::(), - err, + _err, ron ); None diff --git a/epaint/src/texture_atlas.rs b/epaint/src/texture_atlas.rs index 2d0a86b2aef..c31625f30d7 100644 --- a/epaint/src/texture_atlas.rs +++ b/epaint/src/texture_atlas.rs @@ -116,7 +116,10 @@ impl TextureAtlas { if required_height > self.max_height() { // This is a bad place to be - we need to start reusing space :/ - eprintln!("epaint texture atlas overflowed!"); + + #[cfg(feature = "tracing")] + tracing::wan!("epaint texture atlas overflowed!"); + self.cursor = (0, self.image.height() / 3); // Restart a bit down - the top of the atlas has too many important things in it self.overflowed = true; // this will signal the user that we need to recreate the texture atlas next frame. } else if resize_to_min_height(&mut self.image, required_height) { diff --git a/epi/Cargo.toml b/epi/Cargo.toml index 48f9f8cc26c..84c3146bf2a 100644 --- a/epi/Cargo.toml +++ b/epi/Cargo.toml @@ -25,6 +25,7 @@ all-features = true [dependencies] egui = { version = "0.16.0", path = "../egui", default-features = false, features = ["single_threaded"] } +tracing = "0.1" directories-next = { version = "2", optional = true } ron = { version = "0.7", optional = true } diff --git a/epi/src/file_storage.rs b/epi/src/file_storage.rs index 9b0c0c52efb..140c6cebf7e 100644 --- a/epi/src/file_storage.rs +++ b/epi/src/file_storage.rs @@ -29,16 +29,17 @@ impl FileStorage { if let Some(proj_dirs) = directories_next::ProjectDirs::from("", "", app_name) { let data_dir = proj_dirs.data_dir().to_path_buf(); if let Err(err) = std::fs::create_dir_all(&data_dir) { - eprintln!( + tracing::warn!( "Saving disabled: Failed to create app path at {:?}: {}", - data_dir, err + data_dir, + err ); None } else { Some(Self::from_ron_filepath(data_dir.join("app.ron"))) } } else { - eprintln!("Saving disabled: Failed to find path to data_dir."); + tracing::warn!("Saving disabled: Failed to find path to data_dir."); None } } @@ -79,7 +80,7 @@ where match ron::de::from_reader(reader) { Ok(value) => Some(value), Err(err) => { - eprintln!("ERROR: Failed to parse RON: {}", err); + tracing::warn!("Failed to parse RON: {}", err); None } } From 0b2a092806528048e1a90cce133a82680c57ef7e Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 10:13:22 +0100 Subject: [PATCH 9/9] polish --- CHANGELOG.md | 2 +- eframe/CHANGELOG.md | 2 +- egui-winit/Cargo.toml | 2 +- egui_web/Cargo.toml | 1 + egui_web/src/backend.rs | 2 +- egui_web/src/lib.rs | 10 ---------- egui_web/src/webgl1.rs | 2 +- egui_web/src/webgl2.rs | 2 +- 8 files changed, 7 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fe0544a38c..8ed55452388 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w * Added `TextStyle::resolve`. * `Context::load_texture` to convert an image into a texture which can be displayed using e.g. `ui.image(texture, size)` ([#1110](https://github.com/emilk/egui/pull/1110)). * Added `Ui::add_visible` and `Ui::add_visible_ui`. -* Opt-in dependency on `tracing` crate for logging warnings. +* Opt-in dependency on `tracing` crate for logging warnings ([#1192](https://github.com/emilk/egui/pull/1192)). * Added `CollapsingHeader::icon` to override the default open/close icon using a custom function. ([1147](https://github.com/emilk/egui/pull/1147)). * Added `Plot::x_axis_formatter` and `Plot::y_axis_formatter` for custom axis labels ([#1130](https://github.com/emilk/egui/pull/1130)). * Added `ui.data()`, `ctx.data()`, `ctx.options()` and `ctx.tessellation_options()` ([#1175](https://github.com/emilk/egui/pull/1175)). diff --git a/eframe/CHANGELOG.md b/eframe/CHANGELOG.md index 6b614c1fce2..42e78ccad3b 100644 --- a/eframe/CHANGELOG.md +++ b/eframe/CHANGELOG.md @@ -11,7 +11,7 @@ NOTE: [`egui_web`](../egui_web/CHANGELOG.md), [`egui-winit`](../egui-winit/CHANG * Fix horizontal scrolling direction on Linux. * Added `App::on_exit_event` ([#1038](https://github.com/emilk/egui/pull/1038)) * Shift-scroll will now result in horizontal scrolling on all platforms ([#1136](https://github.com/emilk/egui/pull/1136)). -* Log using the `tracing` crate. Log to stdout by adding `tracing_subscriber::fmt::init();` to your `main`. And run with `RUST_LOG=debug`. +* Log using the `tracing` crate. Log to stdout by adding `tracing_subscriber::fmt::init();` to your `main` ([#1192](https://github.com/emilk/egui/pull/1192)). ## 0.16.0 - 2021-12-29 diff --git a/egui-winit/Cargo.toml b/egui-winit/Cargo.toml index ca7ac9a7bfd..8e6c88824ac 100644 --- a/egui-winit/Cargo.toml +++ b/egui-winit/Cargo.toml @@ -22,7 +22,7 @@ include = [ all-features = true [dependencies] -egui = { version = "0.16.0", path = "../egui", default-features = false, features = ["single_threaded"] } +egui = { version = "0.16.0", path = "../egui", default-features = false, features = ["single_threaded", "tracing"] } instant = { version = "0.1", features = ["wasm-bindgen"] } tracing = "0.1" winit = "0.26.1" diff --git a/egui_web/Cargo.toml b/egui_web/Cargo.toml index cc98b3b19c4..d55bb8ccfbf 100644 --- a/egui_web/Cargo.toml +++ b/egui_web/Cargo.toml @@ -29,6 +29,7 @@ crate-type = ["cdylib", "rlib"] egui = { version = "0.16.0", path = "../egui", default-features = false, features = [ "convert_bytemuck", "single_threaded", + "tracing", ] } egui_glow = { version = "0.16.0",path = "../egui_glow", default-features = false, optional = true } epi = { version = "0.16.0", path = "../epi" } diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index 6cbbe953b6c..fe4a2c28828 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -284,7 +284,7 @@ pub fn start(canvas_id: &str, app: Box) -> Result String { - s.as_string().unwrap_or(format!("{:#?}", s)) -} - -pub fn string_from_js_string(s: js_sys::JsString) -> String { - s.as_string().unwrap_or(format!("{:#?}", s)) -} - -// ---------------------------------------------------------------------------- - /// Current time in seconds (since undefined point in time) pub fn now_sec() -> f64 { web_sys::window() diff --git a/egui_web/src/webgl1.rs b/egui_web/src/webgl1.rs index 9b076a92d7f..bfa3976249b 100644 --- a/egui_web/src/webgl1.rs +++ b/egui_web/src/webgl1.rs @@ -299,7 +299,7 @@ impl crate::Painter for WebGlPainter { } } - crate::console_error("Failed to query max texture size"); + tracing::error!("Failed to query max texture size"); 2048 } diff --git a/egui_web/src/webgl2.rs b/egui_web/src/webgl2.rs index 5c8316ac03b..c8ede7fd74d 100644 --- a/egui_web/src/webgl2.rs +++ b/egui_web/src/webgl2.rs @@ -283,7 +283,7 @@ impl crate::Painter for WebGl2Painter { } } - crate::console_error("Failed to query max texture size"); + tracing::error!("Failed to query max texture size"); 2048 }