Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print out tracing message in Substrate runtime development

Tags:

substrate

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

like image 349
Jimmy Chu Avatar asked Jul 19 '19 09:07

Jimmy Chu


2 Answers

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. So you'd have to do RUST_LOG=runtime=debug. You are still calling into sp_io under the hood though. Also, note that frame_support is re-exporting this for you. Most pallets need frame_support anyhow and this maeks the usage easier.
  • If you want to compile for wasm and native, and want prints only for native execution, use sp_std::if_std!{} macro.
  • Finally, you can use frame_support::debug module. This module provides wrappers around the above two to make the usage easier and more rust-like. Similar to a normal logger, you can use debug::native::warn!(...) etc.

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.

like image 55
kianenigma Avatar answered Oct 16 '22 05:10

kianenigma


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.

like image 26
Shawn Tabrizi Avatar answered Oct 16 '22 06:10

Shawn Tabrizi