When working on Parity Substrate runtime development, how can I print out debug message for tracing and inspecting my variables?
3 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 fromsp-runtime::print(). These will be visible in a log target namedruntime. So you'd have to doRUST_LOG=runtime=debug. You are still calling intosp_iounder the hood though. Also, note thatframe_supportis re-exporting this for you. Most pallets needframe_supportanyhow 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::debugmodule. This module provides wrappers around the above two to make the usage easier and more rust-like. Similar to a normal logger, you can usedebug::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.
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.
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.
substrate, would you be interested in a dedicated Stack Exchange Q&A site for Substrate, Polkadot, et al. -- check out the Area51 Substrate Proposal - Afr