Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch log signature to str from bytes #366

Merged
merged 21 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a1b9148
Switch log signature to str from bytes
austinabell Apr 20, 2021
bb568c7
fix(log): switch macro type to expr to make api less restrictive
austinabell Apr 20, 2021
d00ef07
Switch function signature to &str to remove potential code duplication
austinabell Apr 20, 2021
54252df
Merge branch 'master' into env/logsignature
austinabell Apr 23, 2021
4994c92
Merge branch 'master' of github.com:near/near-sdk-rs into env/logsign…
austinabell May 17, 2021
6af641c
Merge branch 'master' of github.com:near/near-sdk-rs into env/logsign…
austinabell Jun 4, 2021
ec98504
remove breaking change on function signature by adding log_str and up…
austinabell Jun 4, 2021
84f714b
fix equivalent doc example
austinabell Jun 4, 2021
f514ec0
fmt
austinabell Jun 4, 2021
844c3a6
Merge branch 'master' into env/logsignature
austinabell Jun 4, 2021
3bd7cb1
add print added to log_str also
austinabell Jun 4, 2021
2a37b9f
change log debug prints to std error instead of std out
austinabell Jun 10, 2021
a5c95b6
Merge branch 'master' into env/logsignature
mikedotexe Jun 21, 2021
d8d8109
Merge branch 'master' into env/logsignature
austinabell Jun 23, 2021
b695537
Merge branch 'master' into env/logsignature
austinabell Jun 29, 2021
2ca4858
Merge branch 'master' into env/logsignature
mikedotexe Jul 4, 2021
481d850
Merge branch 'master' of github.com:near/near-sdk-rs into env/logsign…
austinabell Jul 7, 2021
59216bc
Merge branch 'env/logsignature' of github.com:austinabell/near-sdk-rs…
austinabell Jul 7, 2021
2f0dd43
Merge branch 'master' into env/logsignature
austinabell Jul 13, 2021
80c8f08
Merge branch 'master' into env/logsignature
austinabell Jul 14, 2021
8473783
Merge branch 'master' into env/logsignature
austinabell Jul 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be accepting anything that implements ToString trait? WDYT @matklad ?
Btw, is this method going to support no-std?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a huge benefit in supporting this because it would increase code size for the monomorphization of the generic and for more general usage, log! macro is used. I originally made it generic based on AsRef<str> but since it added code size (tested and an example given above) the macro calls as_ref and this base call remains as a concrete type. Having it be ToString would force an unnecessary allocation that I'm not sure can be optimized away by the compiler.

As for no_std, yes it is no_std compatible and has the same memory layout as bytes. It's currently used as such in the no_std experimental SDK I've been playing with.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, ToString would force allocation, and won't be enough to make a nice API. For the latter, we need a format-style macro.

#[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