1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

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
This commit is contained in:
Emil Ernerfeldt
2022-02-01 12:27:39 +01:00
committed by GitHub
parent 1f03f53dc0
commit c6ac1827f6
28 changed files with 194 additions and 107 deletions

View File

@@ -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"]

View File

@@ -14,10 +14,10 @@ fn create_painter(canvas_id: &str) -> Result<Box<dyn Painter>, 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<dyn epi::App>) -> Result<AppRunnerRef, Js
// Make sure panics are logged using `console.error`.
console_error_panic_hook::set_once();
// Redirect tracing to console.log and friends:
tracing_wasm::set_as_global_default();
let mut runner = AppRunner::new(canvas_id, app)?;
runner.warm_up()?;
start_runner(runner)

View File

@@ -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");
tracing::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.");
tracing::debug!("WebGL1 selected.");
let gl1_ctx = gl1_ctx
.dyn_into::<web_sys::WebGlRenderingContext>()
.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::<web_sys::WebGl2RenderingContext>()

View File

@@ -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<JsValue>) {
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<JsValue>) {
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<JsValue>) {
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);
}
}
};

View File

@@ -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);
}
}
}

View File

@@ -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 {
""

View File

@@ -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);
}
}