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

Support debug logs in debug WASM builds #654

Merged
merged 14 commits into from
Sep 22, 2022
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ soroban-sdk = { path = "soroban-sdk" }
soroban-auth = { path = "soroban-auth" }
soroban-spec = { path = "soroban-spec" }
soroban-sdk-macros = { path = "soroban-sdk-macros" }
soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "fccd5196" }
soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "fccd5196" }
soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "fccd5196" }
soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "fccd5196" }
soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "fccd5196" }
soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "38ed32b4" }
soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "38ed32b4" }
soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "38ed32b4" }
soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "38ed32b4" }
soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "38ed32b4" }
stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "c026149" }
wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" }

Expand All @@ -55,3 +55,7 @@ debug-assertions = false
panic = "abort"
codegen-units = 1
lto = true

[profile.release-with-logs]
inherits = "release"
debug-assertions = true
2 changes: 1 addition & 1 deletion soroban-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bytes-lit = "0.0.4"
soroban-env-guest = { version = "0.0.5" }

[target.'cfg(not(target_family="wasm"))'.dependencies]
soroban-env-host = { version = "0.0.5", features = ["vm"] }
soroban-env-host = { version = "0.0.5", features = ["vm", "hostfn_log_fmt_values"] }
ed25519-dalek = { version = "1.0.1", optional = true }

[dev-dependencies]
Expand Down
31 changes: 25 additions & 6 deletions soroban-sdk/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
//! See [`log`][crate::log] for how to conveniently log debug events.
use core::fmt::Debug;

use crate::{env::internal::EnvBase, Env, RawVal};
use crate::{
env::internal::{self, EnvBase},
Bytes, Env, IntoVal, RawVal, Vec,
};

/// Log a debug event.
///
Expand All @@ -12,11 +15,17 @@ use crate::{env::internal::EnvBase, Env, RawVal};
/// [`RawVal`].
///
/// `log!` statements are only enabled in non optimized builds that have
/// `debug-assertions` enabled.
/// `debug-assertions` enabled. To enable `debug-assertions` add the following
/// lines to `Cargo.toml`, then build with the profile specified, `--profile
/// release-with-logs`. See the cargo docs for how to use [custom profiles].
///
/// ```toml
/// [profile.release-with-logs]
/// inherits = "release"
/// debug-assertions = true
/// ```
///
/// <p style="padding:0.75em;background:var(--code-block-background-color);">
/// <b>Note</b>: Log support in WASM builds is pending on <a href="https://github.com/stellar/rs-soroban-env/issues/447">soroban-env#447</a>.
/// </p>
/// [custom profiles]: https://doc.rust-lang.org/cargo/reference/profiles.html#custom-profiles
///
/// ### Examples
///
Expand Down Expand Up @@ -114,7 +123,17 @@ impl Logger {
pub fn log(&self, fmt: &'static str, args: &[RawVal]) {
if cfg!(debug_assertions) {
let env = self.env();
env.log_static_fmt_general(fmt, &args, &[]).unwrap();
// If building for WASM logging will be transmitted through the
// guest interface via host types. This is very inefficient. When
// building on non-WASM environments where the environment Host is
// directly available, use the log static variants.
if cfg!(target_family = "wasm") {
let fmt: Bytes = fmt.into_val(env);
let args: Vec<RawVal> = Vec::from_slice(env, args);
internal::Env::log_fmt_values(env, fmt.to_object(), args.to_object());
} else {
env.log_static_fmt_general(fmt, &args, &[]).unwrap();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/tests/contractfile_with_sha256.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub const WASM: &[u8] = soroban_sdk::contractfile!(
file = "../target/wasm32-unknown-unknown/release/example_add_i32.wasm",
sha256 = "26f5c00d05439950c7edbfcc1f2bd47f383094b2105f8473168407636b050258",
sha256 = "a99f9c800317a653dd6fe106e6c9789f0033b69b6bf9ff4c7fcdd63530557cb3",
);

#[test]
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/tests/contractimport_with_sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ADD_CONTRACT_ID: [u8; 32] = [0; 32];
mod addcontract {
soroban_sdk::contractimport!(
file = "../target/wasm32-unknown-unknown/release/example_add_i32.wasm",
sha256 = "26f5c00d05439950c7edbfcc1f2bd47f383094b2105f8473168407636b050258",
sha256 = "a99f9c800317a653dd6fe106e6c9789f0033b69b6bf9ff4c7fcdd63530557cb3",
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: sha256 does not match, expected: 26f5c00d05439950c7edbfcc1f2bd47f383094b2105f8473168407636b050258
error: sha256 does not match, expected: a99f9c800317a653dd6fe106e6c9789f0033b69b6bf9ff4c7fcdd63530557cb3
--> tests/trybuild/contractfile_with_sha256_verify_fail.rs:3:5
|
3 | sha256 = "0000000000000000000000000000000000000000000000000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: sha256 does not match, expected: 26f5c00d05439950c7edbfcc1f2bd47f383094b2105f8473168407636b050258
error: sha256 does not match, expected: a99f9c800317a653dd6fe106e6c9789f0033b69b6bf9ff4c7fcdd63530557cb3
--> tests/trybuild/contractimport_with_sha256_verify_fail.rs:3:5
|
3 | sha256 = "0000000000000000000000000000000000000000000000000000000000000000",
Expand Down