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

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