fluentbase_runtime/syscall_handler/host/
debug_log.rs1use crate::RuntimeContext;
2use core::cell::Cell;
3use rwasm::{StoreTr, TrapCode, Value};
4
5thread_local! {
6 pub static LAST_LOG_TIME: Cell<u128> = const { Cell::new(0) };
7}
8
9pub fn syscall_debug_log_handler(
10 caller: &mut impl StoreTr<RuntimeContext>,
11 params: &[Value],
12 _result: &mut [Value],
13) -> Result<(), TrapCode> {
14 let (message_ptr, message_len) = (
15 params[0].i32().unwrap() as usize,
16 params[1].i32().unwrap() as usize,
17 );
18 let mut buffer = vec![0u8; message_len];
19 caller.memory_read(message_ptr, &mut buffer)?;
20 syscall_debug_log_impl(&buffer);
21 Ok(())
22}
23
24#[cfg(feature = "debug-print")]
25pub fn syscall_debug_log_impl(mut msg: &[u8]) {
26 use std::time::SystemTime;
27 let curr_time = SystemTime::now()
28 .duration_since(SystemTime::UNIX_EPOCH)
29 .unwrap()
30 .as_micros();
31 let last_time = LAST_LOG_TIME.get();
32 let time_diff = if last_time > 0 {
33 curr_time - last_time
34 } else {
35 0
36 };
37 LAST_LOG_TIME.set(curr_time);
38 const DEBUG_LOG_MAXIMUM_LEN: usize = 1_000;
39 if msg.len() > DEBUG_LOG_MAXIMUM_LEN {
40 msg = &msg[..DEBUG_LOG_MAXIMUM_LEN]
41 }
42 println!(
43 "debug_log (diff {}us): {}",
44 time_diff,
45 std::str::from_utf8(msg)
46 .map(|s| s.to_string())
47 .unwrap_or("non utf-8 message".to_string())
48 );
49}
50
51#[cfg(not(feature = "debug-print"))]
52pub fn syscall_debug_log_impl(_msg: &[u8]) {}