Skip to content

Commit

Permalink
Merge pull request #366 from austinabell/env/logsignature
Browse files Browse the repository at this point in the history
Switch log signature to str from bytes
  • Loading branch information
mikedotexe authored Jul 15, 2021
2 parents c947463 + 8473783 commit 932ebb5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
```
Expand All @@ -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");
}
}
```
Expand Down Expand Up @@ -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!");
}
}
```
Expand All @@ -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!");
}
}
```
Expand Down Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion examples/cross-contract-low-level/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
10 changes: 10 additions & 0 deletions near-sdk/src/environment/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _) }
}

Expand Down
6 changes: 3 additions & 3 deletions near-sdk/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
};
}

Expand Down

0 comments on commit 932ebb5

Please sign in to comment.