Search code examples
substrate

How to print out tracing message in Substrate runtime development


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


Solution

  • 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.