From c6ac1827f6eb9ffe0e08d12ce9fbf6a7e65105fc Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 1 Feb 2022 12:27:39 +0100 Subject: [PATCH] Use tracing crate for logging (#1192) * egui_web: use tracing crate * egui_glow: use tracing crate * Log at the debug level * egui_demo_app: enable tracing to log to stdout * Use tracing in egui-winit * Add opt-in tracing support to egui --- CHANGELOG.md | 1 + Cargo.lock | 99 +++++++++++++++++++++++++++++++++ docs/index.html | 5 +- eframe/CHANGELOG.md | 1 + egui-winit/Cargo.toml | 3 +- egui-winit/src/clipboard.rs | 6 +- egui-winit/src/lib.rs | 4 +- egui-winit/src/screen_reader.rs | 8 +-- egui/Cargo.toml | 10 +++- egui/src/util/id_type_map.rs | 7 ++- egui_demo_app/Cargo.toml | 1 + egui_demo_app/src/main.rs | 3 + egui_glow/Cargo.toml | 1 + egui_glow/src/epi_backend.rs | 3 +- egui_glow/src/lib.rs | 5 +- egui_glow/src/misc_util.rs | 26 ++------- egui_glow/src/painter.rs | 10 ++-- egui_glow/src/shader_version.rs | 9 +-- egui_web/Cargo.toml | 9 ++- 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 | 9 +-- egui_web/src/webgl2.rs | 7 +-- epaint/src/texture_atlas.rs | 5 +- epi/Cargo.toml | 1 + epi/src/file_storage.rs | 9 +-- 28 files changed, 194 insertions(+), 107 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a707a3518c..8ed55452388 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 ([#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/Cargo.lock b/Cargo.lock index 8d14654b52d..6158dedff59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,6 +798,7 @@ dependencies = [ "nohash-hasher", "ron", "serde", + "tracing", ] [[package]] @@ -809,6 +810,7 @@ dependencies = [ "epi", "instant", "serde", + "tracing", "tts", "webbrowser", "winit", @@ -820,6 +822,7 @@ version = "0.16.0" dependencies = [ "eframe", "egui_demo_lib", + "tracing-subscriber", ] [[package]] @@ -864,6 +867,7 @@ dependencies = [ "glutin", "image", "memoffset", + "tracing", "wasm-bindgen", "web-sys", ] @@ -880,6 +884,8 @@ dependencies = [ "js-sys", "ron", "serde", + "tracing", + "tracing-wasm", "tts", "wasm-bindgen", "wasm-bindgen-futures", @@ -972,6 +978,7 @@ dependencies = [ "egui", "ron", "serde", + "tracing", ] [[package]] @@ -1845,6 +1852,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 +2212,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 +2438,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 +2501,74 @@ 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", + "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-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]] +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/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/eframe/CHANGELOG.md b/eframe/CHANGELOG.md index 45ed02fe4d3..42e78ccad3b 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` ([#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 b64a630b66e..8e6c88824ac 100644 --- a/egui-winit/Cargo.toml +++ b/egui-winit/Cargo.toml @@ -22,8 +22,9 @@ 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" 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/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/egui_demo_app/Cargo.toml b/egui_demo_app/Cargo.toml index 982faa5b6ba..861e5e08db5 100644 --- a/egui_demo_app/Cargo.toml +++ b/egui_demo_app/Cargo.toml @@ -17,6 +17,7 @@ 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" [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 diff --git a/egui_glow/Cargo.toml b/egui_glow/Cargo.toml index 1a5195459e3..9b307f1c55f 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 = "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_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/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..4547c26187e 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::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 1e461838648..0f9d5f44723 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::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 { - glow_print("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 { - glow_print("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, "") } }, @@ -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!" ); } diff --git a/egui_glow/src/shader_version.rs b/egui_glow/src/shader_version.rs index de3a339d1f3..ae4af65ed86 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::debug!( "Shader version: {:?} ({:?}).", - shader_version, shading_lang_string - )); + shader_version, + shading_lang_string + ); shader_version } diff --git a/egui_web/Cargo.toml b/egui_web/Cargo.toml index ee2659c6cb4..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" } @@ -36,11 +37,15 @@ epi = { version = "0.16.0", path = "../epi" } bytemuck = "1.7" console_error_panic_hook = "0.1.6" js-sys = "0.3" +tracing = "0.1" +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 de5bb5db730..fe4a2c28828 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::debug!("Using WebGL2 backend"); Ok(Box::new(webgl2_painter)) } else { - console_log("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)) } @@ -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::debug!("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::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?; - crate::console_log("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 3faff02ecde..781cf3ad65c 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 - -/// 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()); -} - -/// 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()); -} - -/// 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()); -} /// Current time in seconds (since undefined point in time) pub fn now_sec() -> f64 { @@ -259,7 +243,7 @@ pub fn load_memory(ctx: &egui::Context) { *ctx.memory() = memory; } Err(err) => { - console_error(format!("Failed to parse memory RON: {}", err)); + tracing::error!("Failed to parse memory RON: {}", err); } } } @@ -275,7 +259,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)); + tracing::error!("Failed to serialize memory as RON: {}", err); } } } @@ -315,7 +299,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)); + tracing::error!("Copy/cut action denied: {:?}", err); } }; wasm_bindgen_futures::spawn_local(future); @@ -575,12 +559,12 @@ fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { false }; - // console_log(format!( + // tracing::debug!( // "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 +764,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)); + tracing::error!("Unknown composition event type: {:?}", s); None } }; @@ -1128,7 +1112,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())); + tracing::debug!("Loading {:?} ({} bytes)…", name, file.size()); let future = wasm_bindgen_futures::JsFuture::from(file.array_buffer()); @@ -1137,11 +1121,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(format!( + tracing::debug!( "Loaded {:?} ({} bytes).", name, bytes.len() - )); + ); let mut runner_lock = runner_ref.0.lock(); runner_lock.input.raw.dropped_files.push( @@ -1155,7 +1139,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)); + tracing::error!("Failed to read file: {:?}", err); } } }; diff --git a/egui_web/src/screen_reader.rs b/egui_web/src/screen_reader.rs index e8f3ac95c75..11c36efdc3b 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::debug!("Initialized screen reader."); Some(screen_reader) } Err(err) => { - crate::console_warn(format!("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(format!("Speaking: {:?}", text)); + tracing::debug!("Speaking: {:?}", text); let interrupt = true; if let Err(err) = tts.speak(text, interrupt) { - crate::console_warn(format!("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 6c2d4d86a9e..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 } @@ -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::debug!("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..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 } @@ -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); } } 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 } }