diff --git a/CHANGELOG.md b/CHANGELOG.md index ffd592e94..d109601fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * expose `cur_block` and `genesis_config` from `RuntimeStandalone` to configure simulation tests. [PR 390](https://github.com/near/near-sdk-rs/pull/390). * fix(simulator): failing with long chains. [PR 385](https://github.com/near/near-sdk-rs/pull/385). * Make block time configurable to sim contract tests. [PR 378](https://github.com/near/near-sdk-rs/pull/378). +* Deprecate `env::log` in favour of `env::log_str`. The logs assume that the bytes are utf8, so this will be a cleaner interface to use. [PR 366](https://github.com/near/near-sdk-rs/pull/366). * Update syscall interface to no longer go through `BLOCKCHAIN_INTERFACE`. Instead uses `near_sdk::sys` which is under the `unstable` feature flag if needed. [PR 417](https://github.com/near/near-sdk-rs/pull/417). * Set up global allocator by default for WASM architectures. [PR 429](https://github.com/near/near-sdk-rs/pull/429). * This removes the re-export of `wee_alloc` because if this feature is enabled, the allocator will already be set. diff --git a/HELP.md b/HELP.md index b3480f70e..ed9113022 100644 --- a/HELP.md +++ b/HELP.md @@ -182,7 +182,7 @@ There is an macro decorator `#[private]` that checks that the current account ID impl Contract { #[private] pub fn resolve_transfer(&mut self) { - env::log(b"This is a callback"); + env::log_str("This is a callback"); } } ``` @@ -196,7 +196,7 @@ impl Contract { if env::current_account_id() != env::predecessor_account_id() { near_sdk::env::panic(b"Method resolve_transfer is private"); } - env::log(b"This is a callback"); + env::log_str("This is a callback"); } } ``` @@ -348,11 +348,11 @@ to receive attached deposits. Otherwise, if a deposit is attached to a non-payab impl Contract { #[payable] pub fn take_my_money(&mut self) { - env::log(b"Thanks!"); + env::log_str("Thanks!"); } pub fn do_not_take_my_money(&mut self) { - env::log(b"Thanks!"); + env::log_str("Thanks!"); } } ``` @@ -363,14 +363,14 @@ This is equivalent to: #[near_bindgen] impl Contract { pub fn take_my_money(&mut self) { - env::log(b"Thanks!"); + env::log_str("Thanks!"); } pub fn do_not_take_my_money(&mut self) { if near_sdk::env::attached_deposit() != 0 { near_sdk::env::panic(b"Method do_not_take_my_money doesn't accept deposit"); } - env::log(b"Thanks!"); + env::log_str("Thanks!"); } } ``` @@ -412,7 +412,7 @@ log!("Transferred {} tokens from {} to {}", amount, sender_id, receiver_id); It's equivalent to the following message: ```rust -env::log(format!("Transferred {} tokens from {} to {}", amount, sender_id, receiver_id).as_bytes()); +env::log_str(format!("Transferred {} tokens from {} to {}", amount, sender_id, receiver_id).as_ref()); ``` ## Return `Promise` diff --git a/examples/cross-contract-low-level/src/lib.rs b/examples/cross-contract-low-level/src/lib.rs index c66c7292d..8dd6227cd 100644 --- a/examples/cross-contract-low-level/src/lib.rs +++ b/examples/cross-contract-low-level/src/lib.rs @@ -145,7 +145,7 @@ impl CrossContract { pub fn check_promise(&mut self) { match env::promise_result(0) { PromiseResult::Successful(_) => { - env::log(b"Check_promise successful"); + env::log_str("Check_promise successful"); self.checked_promise = true; } _ => panic!("Promise with index 0 failed"), diff --git a/near-sdk/src/environment/env.rs b/near-sdk/src/environment/env.rs index 73a7405d5..cfbed2998 100644 --- a/near-sdk/src/environment/env.rs +++ b/near-sdk/src/environment/env.rs @@ -510,10 +510,20 @@ pub fn panic(message: &[u8]) -> ! { unsafe { sys::panic_utf8(message.len() as _, message.as_ptr() as _) } unreachable!() } +/// Logs the string message message. This message is stored on chain. +pub fn log_str(message: &str) { + #[cfg(all(debug_assertions, not(target_arch = "wasm32")))] + eprintln!("{}", message); + + unsafe { sys::log_utf8(message.len() as _, message.as_ptr() as _) } +} + /// Log the UTF-8 encodable message. +#[deprecated(since = "4.0.0", note = "Use env::log_str for logging messages.")] pub fn log(message: &[u8]) { #[cfg(all(debug_assertions, not(target_arch = "wasm32")))] eprintln!("{}", String::from_utf8_lossy(message)); + unsafe { sys::log_utf8(message.len() as _, message.as_ptr() as _) } } diff --git a/near-sdk/src/utils/mod.rs b/near-sdk/src/utils/mod.rs index 357789798..a7c0cb0d2 100644 --- a/near-sdk/src/utils/mod.rs +++ b/near-sdk/src/utils/mod.rs @@ -33,11 +33,11 @@ use crate::{env, AccountId, PromiseResult}; /// [`BlockchainInterface`]: crate::BlockchainInterface #[macro_export] macro_rules! log { - ($arg:tt) => { - $crate::env::log($arg.as_bytes()) + ($arg:expr) => { + $crate::env::log_str($arg.as_ref()) }; ($($arg:tt)*) => { - $crate::env::log(format!($($arg)*).as_bytes()) + $crate::env::log_str(format!($($arg)*).as_str()) }; }