Move the wasm lib, and cleanup rust directory and all references (#5585)
* git mv src/wasm-lib rust Signed-off-by: Jess Frazelle <github@jessfraz.com> * mv wasm-lib to workspace Signed-off-by: Jess Frazelle <github@jessfraz.com> * mv kcl-lib Signed-off-by: Jess Frazelle <github@jessfraz.com> * mv derive docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * resolve file paths Signed-off-by: Jess Frazelle <github@jessfraz.com> * clippy Signed-off-by: Jess Frazelle <github@jessfraz.com> * move more shit Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix more paths Signed-off-by: Jess Frazelle <github@jessfraz.com> * make yarn build:wasm work Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix scripts Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixups Signed-off-by: Jess Frazelle <github@jessfraz.com> * better references Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix cargo ci Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix reference Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix more ci Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix tests Signed-off-by: Jess Frazelle <github@jessfraz.com> * cargo sort Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix script Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * fmt Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix a dep Signed-off-by: Jess Frazelle <github@jessfraz.com> * sort Signed-off-by: Jess Frazelle <github@jessfraz.com> * remove unused deps Signed-off-by: Jess Frazelle <github@jessfraz.com> * Revert "remove unused deps" This reverts commit fbabdb062e275fd5cbc1476f8480a1afee15d972. * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * deps; Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
117
rust/kcl-lib/src/log.rs
Normal file
117
rust/kcl-lib/src/log.rs
Normal file
@ -0,0 +1,117 @@
|
||||
#![allow(dead_code)]
|
||||
use std::env;
|
||||
|
||||
#[cfg(feature = "dhat-heap")]
|
||||
use dhat::{HeapStats, Profiler};
|
||||
use web_time::Instant;
|
||||
|
||||
const LOG_ENV_VAR: &str = "ZOO_LOG";
|
||||
lazy_static::lazy_static! {
|
||||
static ref ENABLED: bool = {
|
||||
let env_var = env::var(LOG_ENV_VAR);
|
||||
let Ok(env_var) = env_var else {
|
||||
return false;
|
||||
};
|
||||
!env_var.is_empty()
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(feature = "dhat-heap")]
|
||||
lazy_static::lazy_static! {
|
||||
static ref PROFILER: Profiler = Profiler::builder().testing().build();
|
||||
}
|
||||
|
||||
/// Log a message
|
||||
pub(crate) fn log(msg: impl Into<String>) {
|
||||
if *ENABLED {
|
||||
log_inner(msg.into());
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_macros)]
|
||||
macro_rules! logln {
|
||||
($($rest:tt)*) => {
|
||||
crate::log::log(format!($($rest)*))
|
||||
}
|
||||
}
|
||||
pub(crate) use logln;
|
||||
|
||||
#[cfg(all(not(feature = "disable-println"), not(target_arch = "wasm32")))]
|
||||
#[inline]
|
||||
fn log_inner(msg: String) {
|
||||
eprintln!("{msg}");
|
||||
}
|
||||
|
||||
#[cfg(all(not(feature = "disable-println"), target_arch = "wasm32"))]
|
||||
#[inline]
|
||||
fn log_inner(msg: String) {
|
||||
web_sys::console::log_1(&msg.into());
|
||||
}
|
||||
|
||||
#[cfg(feature = "disable-println")]
|
||||
#[inline]
|
||||
fn log_inner(_msg: String) {}
|
||||
|
||||
/// A helper struct for recording and logging basic performance metrics.
|
||||
///
|
||||
/// It will log the metrics when dropped or if `log_now` is called.
|
||||
pub(crate) struct LogPerfStats<'a> {
|
||||
msg: &'a str,
|
||||
start_time: Instant,
|
||||
#[cfg(feature = "dhat-heap")]
|
||||
start_stats: HeapStats,
|
||||
cancelled: bool,
|
||||
}
|
||||
|
||||
impl<'a> LogPerfStats<'a> {
|
||||
#[cfg(not(feature = "dhat-heap"))]
|
||||
pub fn new(msg: &'a str) -> Self {
|
||||
LogPerfStats {
|
||||
msg,
|
||||
start_time: Instant::now(),
|
||||
cancelled: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "dhat-heap")]
|
||||
pub fn new(msg: &'a str) -> Self {
|
||||
lazy_static::initialize(&PROFILER);
|
||||
LogPerfStats {
|
||||
msg,
|
||||
start_time: Instant::now(),
|
||||
start_stats: HeapStats::get(),
|
||||
cancelled: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn log_now(&self) {
|
||||
let time = Instant::now().duration_since(self.start_time).as_secs_f64() * 1000.0;
|
||||
logln!("{}\n time: {time:.3}ms", self.msg);
|
||||
#[cfg(feature = "dhat-heap")]
|
||||
{
|
||||
let stats = HeapStats::get();
|
||||
let blocks = stats.total_blocks - self.start_stats.total_blocks;
|
||||
let bytes = (stats.total_bytes - self.start_stats.total_bytes) as f64 / 1_000_000.0;
|
||||
let cur = stats.curr_bytes as f64 / 1000.0;
|
||||
let max = stats.curr_bytes as f64 / 1000.0;
|
||||
|
||||
logln!(" memory:");
|
||||
logln!(" allocations: {bytes:.5} MB ({blocks} blocks)");
|
||||
logln!(" currently allocated: {cur:.3} KB");
|
||||
logln!(" max allocated: {max:.3} KB");
|
||||
}
|
||||
}
|
||||
|
||||
/// After `cancel`ing, this object will not log its stats on drop (you can still `log_now`).
|
||||
pub fn cancel(&mut self) {
|
||||
self.cancelled = true;
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LogPerfStats<'_> {
|
||||
fn drop(&mut self) {
|
||||
if !self.cancelled {
|
||||
self.log_now();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user