diff --git a/Cargo.toml b/Cargo.toml index d6126a3..2d882e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ lazy_static = "1.4.0" tar = "0.4.38" ruzstd = { git = "https://github.com/KillingSpark/zstd-rs.git" } serde_json = "1.0.79" +tracing = "0.1.32" [build-dependencies] shadow-rs = "0.11.0" @@ -51,3 +52,4 @@ wasm-bindgen = { version = "0.2.79", default-features = false, features = [ "std", ] } web-sys = "0.3.56" +tracing-wasm = "0.2.1" diff --git a/src/egui_app.rs b/src/egui_app.rs index 8915fab..b341ad3 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -1,5 +1,5 @@ use crate::function::{FunctionEntry, RiemannSum, EMPTY_FUNCTION_ENTRY}; -use crate::misc::{debug_log, log_helper, JsonFileOutput, SerdeValueHelper}; +use crate::misc::{JsonFileOutput, SerdeValueHelper}; use crate::parsing::{process_func_str, test_func}; use const_format::formatc; @@ -91,7 +91,7 @@ lazy_static::lazy_static! { static ref ASSETS: Assets = { let start = instant::Instant::now(); - log_helper("Loading assets..."); + tracing::info!("Loading assets..."); let mut tar_file_data = Vec::new(); let _ = ruzstd::StreamingDecoder::new(&mut include_bytes!("../assets.tar.zst").as_slice()).expect("failed to decompress assets").read_to_end(&mut tar_file_data).expect("failed to read assets"); @@ -106,7 +106,7 @@ lazy_static::lazy_static! { let mut text_data: Option = None; - log_helper("Reading assets..."); + tracing::info!("Reading assets..."); // Iterate through all entries in the tarball for file in tar_archive.entries().unwrap() { let mut file = file.unwrap(); @@ -115,7 +115,7 @@ lazy_static::lazy_static! { let path = file.header().path().unwrap(); let path_string = path.to_string_lossy(); - debug_log(&format!("Loading file: {}", path_string)); + tracing::debug!("Loading file: {}", path_string); // Match the file extention if path_string.ends_with(".ttf") { @@ -151,7 +151,7 @@ lazy_static::lazy_static! { } } - log_helper(&format!("Done loading assets! Took: {:?}", start.elapsed())); + tracing::info!("Done loading assets! Took: {:?}", start.elapsed()); let mut font_data: BTreeMap = BTreeMap::new(); let mut families = BTreeMap::new(); @@ -340,7 +340,7 @@ impl MathApp { #[cfg(target_arch = "wasm32")] stop_loading(); - log_helper("egui app initialized."); + tracing::info!("egui app initialized."); Self::default() // initialize `MathApp` } diff --git a/src/function.rs b/src/function.rs index c2fefa0..d570998 100644 --- a/src/function.rs +++ b/src/function.rs @@ -2,7 +2,7 @@ use crate::function_output::FunctionOutput; #[allow(unused_imports)] -use crate::misc::{debug_log, newtons_method, SteppedVector}; +use crate::misc::{newtons_method, SteppedVector}; use crate::egui_app::{DEFAULT_FUNCION, DEFAULT_RIEMANN}; use crate::parsing::BackingFunction; diff --git a/src/lib.rs b/src/lib.rs index 7c93be3..b0fa33a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,6 @@ mod parsing; cfg_if::cfg_if! { if #[cfg(target_arch = "wasm32")] { - use misc::log_helper; use wasm_bindgen::prelude::*; #[global_allocator] @@ -17,16 +16,17 @@ cfg_if::cfg_if! { #[wasm_bindgen(start)] pub fn start() -> Result<(), wasm_bindgen::JsValue> { - log_helper("Initializing..."); + tracing::info!("Initializing..."); // Used in order to hook into `panic!()` to log in the browser's console - log_helper("Initializing panic hooks..."); - std::panic::set_hook(Box::new(console_error_panic_hook::hook)); - log_helper("Initialized panic hooks!"); + tracing::info!("Initializing panic hooks..."); + console_error_panic_hook::set_once(); + tracing_wasm::set_as_global_default(); + tracing::info!("Initialized panic hooks!"); - log_helper("Finished initializing!"); + tracing::info!("Finished initializing!"); - log_helper("Starting App..."); + tracing::info!("Starting App..."); eframe::start_web("canvas", Box::new(|cc| Box::new(egui_app::MathApp::new(cc)))) } } diff --git a/src/misc.rs b/src/misc.rs index a954b39..d48759b 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -1,47 +1,5 @@ use std::ops::Range; -// Handles logging based on if the target is wasm (or not) and if -// `debug_assertions` is enabled or not -cfg_if::cfg_if! { - if #[cfg(target_arch = "wasm32")] { - use wasm_bindgen::prelude::*; - #[wasm_bindgen] - extern "C" { - // `console.log(...)` - #[wasm_bindgen(js_namespace = console)] - fn log(s: &str); - } - - /// Used for logging normal messages - #[allow(dead_code)] - pub fn log_helper(s: &str) { - log(s); - } - - /// Used for debug messages, only does anything if `debug_assertions` is enabled - #[allow(dead_code)] - #[allow(unused_variables)] - pub fn debug_log(s: &str) { - #[cfg(debug_assertions)] - log(s); - } - } else { - /// Used for logging normal messages - #[allow(dead_code)] - pub fn log_helper(s: &str) { - println!("{}", s); - } - - /// Used for debug messages, only does anything if `debug_assertions` is enabled - #[allow(dead_code)] - #[allow(unused_variables)] - pub fn debug_log(s: &str) { - #[cfg(debug_assertions)] - println!("{}", s); - } - } -} - /// `SteppedVector` is used in order to efficiently sort through an ordered /// `Vec` Used in order to speedup the processing of cached data when /// moving horizontally without zoom in `FunctionEntry`. Before this struct, the @@ -106,7 +64,7 @@ impl From> for SteppedVector { // length of data let data_length = data.len(); // length of data subtracted by 1 (represents the maximum index value) - let data_i_length = data.len() - 1; + let data_i_length = data_length - 1; // Ensure data is of correct length if data_length < 2 { @@ -118,12 +76,14 @@ impl From> for SteppedVector { // if min is bigger than max, sort the input data if min > max { + tracing::debug!("SteppedVector: min is larger than max, sorting."); data.sort_unstable_by(|a, b| b.partial_cmp(a).unwrap()); max = data[0]; min = data[data_i_length]; } - let step = (max - min).abs() / (data_i_length as f64); // Calculate the step between elements + // Calculate the step between elements + let step = (max - min).abs() / (data_i_length as f64); // Create and return the struct SteppedVector {