When working on Parity Substrate runtime development, how can I print out debug message for tracing and inspecting my variables?
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. sp_std::if_std!{}
macro. 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With