6

When working on Parity Substrate runtime development, how can I print out debug message for tracing and inspecting my variables?

Jimmy Chu
  • 972
  • 8
  • 27
  • 1
    You tagged your question with `substrate`, would you be interested in a dedicated Stack Exchange Q&A site for Substrate, Polkadot, et al. -- check out the [Area51 Substrate Proposal](https://area51.stackexchange.com/proposals/122626/substrate?referrer=NTUwMTkxYjJjOTJiNjE0YzMxYjgwMGNkZmFlYzdhZTczYjk1ZWY3ZGI4NzJmODUwN2RlYTQ2MTNjZTdkOTZhMAzuL-zybtPN9CHzwE-WUdvBC8WxvPG46b4ayadke6kG0) – q9f Jul 28 '19 at 10:59

4 Answers4

8

Both of the above answers are correct in their own sense/time. Here's a more accurate overview:

  • runtime_io::print("..."); has been moved. You can now use the same function from sp-runtime::print(). These will be visible in a log target named runtime and level DEBUG. So you'd have to do RUST_LOG=runtime=debug. You are still calling into [sp_io under the hood though]. Similar functions are provided un frame_support::print and frame_support::debug as well.
  • If you want to have more control over the log target/level, you can either directly use the log crate. Similarly, you need to make sure that you are enabling the appropriate log target via RUST_LOG. Be ware that you if you forget to specify the log target in log, the crate path will be used by default.
  • If you want to compile for wasm and native, and want prints only for native execution, use sp_std::if_std!{} macro.

A final useful tip is to: when possible, you can just bloat your code with println! and do SKIP_WASM_BUILD=1 cargo run [xxx]. This is helpful when you are developing and want quick debug prints without any of the setup explained above.

kianenigma
  • 1,365
  • 12
  • 20
  • 1
    if you want to print logs to your terminal output to debug the value of variables when running tests for a specific pallet, you can run add the following in a function of your pallet `println!(" {:#?}", );`, and then run the tests with `SKIP_WASM_BUILD=1 RUST_LOG=runtime=debug cargo test --package -- --nocapture` – Luke Schoen Oct 06 '21 at 05:17
  • @LukeSchoen your answer should be the best, up-to-date answer! Thank you – Russo Mar 21 '22 at 06:54
3

You can also use the if_std! macro included with sp-std:

https://github.com/paritytech/substrate/pull/2979

if_std! is a feature gate that should only be run when std feature is enabled.

Example

sp_std::if_std! {
    // This code is only being compiled and executed when the `std` feature is enabled.
    println!("Hello native world");
}

This is better because you can println variables and stuff rather than simply printing a string.

Shawn Tabrizi
  • 12,206
  • 1
  • 38
  • 69
1

you can use the log crate, add it to your cargo.toml and use it like this:

log::info!("hello {}",substrate);

source : https://docs.substrate.io/test/debug/

ezio
  • 359
  • 2
  • 13
0

As a newcomer to Substrate development, the most direct way I found is with runtime_io::print().

Example:

use runtime_io::{ self };

decl_module! {
  pub struct Module<T: Trait> for enum Call where origin: T::Origin {
    fn deposit_event<T>() = default;

    pub fn my_func(origin) -> Result {
      runtime_io::print("Hello World");
      Ok(());
    }
  }
}

The message will then appear in the console. Pay quick attention to it as it is constantly scrolling.

For a complete example, refer to the TCR tutorial example in github.

Jimmy Chu
  • 972
  • 8
  • 27