From 38b48f09c10746c9cfeff6f0217a2a66e6922d96 Mon Sep 17 00:00:00 2001 From: plein <68096727+planetoryd@users.noreply.github.com> Date: Thu, 14 Aug 2025 08:32:37 +0800 Subject: [PATCH 1/3] logging in contract and non contract env --- rust/src/lib.rs | 1 - rust/src/log.rs | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 1739abe..30475ae 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -26,7 +26,6 @@ pub(crate) mod common_generated { } pub mod client_api; -#[cfg(feature = "contract")] pub mod log; #[cfg(feature = "contract")] pub mod rand; diff --git a/rust/src/log.rs b/rust/src/log.rs index 8fc87d4..53a0577 100644 --- a/rust/src/log.rs +++ b/rust/src/log.rs @@ -1,3 +1,13 @@ +#[macro_export] +macro_rules! info { + ($fmt:expr, $($args:tt)*) => { + #[cfg(not(feature="contract"))] + tracing::info!($fmt, $($args)*); + #[cfg(feature="contract")] + info(&format!($fmt, $($args)*)); + }; +} + pub fn info(msg: &str) { let ptr = msg.as_ptr() as _; unsafe { @@ -10,3 +20,13 @@ extern "C" { #[doc(hidden)] fn __frnt__logger__info(id: i64, ptr: i64, len: i32); } + +#[test] +fn log_non_contract() { + use tracing::level_filters::LevelFilter; + + tracing_subscriber::FmtSubscriber::builder() + .with_max_level(LevelFilter::INFO) + .init(); + info!("n={}, y={:?}", 1, 2); +} From 515ba777f46a91655f785c039afcc17b7893b14c Mon Sep 17 00:00:00 2001 From: plein <68096727+planetoryd@users.noreply.github.com> Date: Thu, 14 Aug 2025 09:18:47 +0800 Subject: [PATCH 2/3] fix --- rust/src/log.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/rust/src/log.rs b/rust/src/log.rs index 53a0577..d80801c 100644 --- a/rust/src/log.rs +++ b/rust/src/log.rs @@ -1,10 +1,10 @@ #[macro_export] macro_rules! info { - ($fmt:expr, $($args:tt)*) => { - #[cfg(not(feature="contract"))] - tracing::info!($fmt, $($args)*); - #[cfg(feature="contract")] - info(&format!($fmt, $($args)*)); + ($($arg:tt)*) => { + #[cfg(not(feature = "contract"))] + tracing::info!($($arg)*); + #[cfg(feature = "contract")] + info(&format!($($arg)*)); }; } @@ -15,6 +15,7 @@ pub fn info(msg: &str) { } } + #[link(wasm_import_module = "freenet_log")] extern "C" { #[doc(hidden)] @@ -29,4 +30,5 @@ fn log_non_contract() { .with_max_level(LevelFilter::INFO) .init(); info!("n={}, y={:?}", 1, 2); + info!("zk"); } From 5652be837e645d5c82e52c15d2cc0efa23c30d16 Mon Sep 17 00:00:00 2001 From: plein <68096727+planetoryd@users.noreply.github.com> Date: Thu, 14 Aug 2025 09:31:22 +0800 Subject: [PATCH 3/3] fix --- rust/src/log.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rust/src/log.rs b/rust/src/log.rs index d80801c..22450b6 100644 --- a/rust/src/log.rs +++ b/rust/src/log.rs @@ -4,7 +4,7 @@ macro_rules! info { #[cfg(not(feature = "contract"))] tracing::info!($($arg)*); #[cfg(feature = "contract")] - info(&format!($($arg)*)); + ::freenet_stdlib::log::info(&format!($($arg)*)); }; } @@ -15,7 +15,6 @@ pub fn info(msg: &str) { } } - #[link(wasm_import_module = "freenet_log")] extern "C" { #[doc(hidden)]