debugging and single provider compime
This commit is contained in:
43
src/util/flow_debug.rs
Normal file
43
src/util/flow_debug.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
static NEXT_TRACE_ID: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
pub fn next_trace_id(prefix: &str) -> String {
|
||||
let id = NEXT_TRACE_ID.fetch_add(1, Ordering::Relaxed);
|
||||
format!("{prefix}-{id:06}")
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
pub fn emit(module: &str, line: u32, message: String) {
|
||||
let millis = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_millis())
|
||||
.unwrap_or_default();
|
||||
eprintln!("[debug][{millis}][{module}:{line}] {message}");
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "debug"))]
|
||||
pub fn emit(_module: &str, _line: u32, _message: String) {}
|
||||
|
||||
pub fn preview(value: &str, limit: usize) -> String {
|
||||
if value.len() <= limit {
|
||||
return value.to_string();
|
||||
}
|
||||
|
||||
let mut end = limit;
|
||||
while !value.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
format!("{}...", &value[..end])
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! flow_debug {
|
||||
($($arg:tt)*) => {{
|
||||
#[cfg(feature = "debug")]
|
||||
{
|
||||
$crate::util::flow_debug::emit(module_path!(), line!(), format!($($arg)*));
|
||||
}
|
||||
}};
|
||||
}
|
||||
Reference in New Issue
Block a user