Skip to content

Commit

Permalink
fix: Make log macro fully compatible with std::format
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `let x = "1"; log!(x)` doesn't work anymore, and `log!("str with no formatting")` now allocates, like `format!`. For logging `&str` with no additional formatting and no allocations, use `env::log_str` method instead.
  • Loading branch information
Sliman4 committed Jun 3, 2024
1 parent 8a908c3 commit f2514e5
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions near-sdk/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) use cache_entry::{CacheEntry, EntryState};
use crate::{env, NearToken, PromiseResult};

/// Helper macro to log a message through [`env::log_str`].
/// This macro can be used similar to the [`std::format`] macro in most cases.
/// This macro can be used similar to the [`std::format`] macro.
///
/// This differs from [`std::format`] because instead of generating a string, it will log the utf8
/// bytes as a log through [`env::log_str`].
Expand All @@ -25,7 +25,7 @@ use crate::{env, NearToken, PromiseResult};
/// # fn main() {
/// log!("test");
/// let world: &str = "world";
/// log!(world);
/// log!("{world}");
/// log!("Hello {}", world);
/// log!("x = {}, y = {y}", 10, y = 30);
/// # }
Expand All @@ -34,11 +34,8 @@ use crate::{env, NearToken, PromiseResult};
/// [`env::log_str`]: crate::env::log_str
#[macro_export]
macro_rules! log {
($arg:expr) => {
$crate::env::log_str($arg.as_ref())
};
($($arg:tt)*) => {
$crate::env::log_str(format!($($arg)*).as_str())
$crate::env::log_str(::std::format!($($arg)*).as_str())
};
}

Expand Down

0 comments on commit f2514e5

Please sign in to comment.