【问题标题】:How to print out tracing message in Substrate runtime development如何在 Substrate 运行时开发中打印出跟踪消息
【发布时间】:2019-07-19 09:40:31
【问题描述】:

在进行Parity Substrate 运行时开发时,如何打印调试消息以跟踪和检查我的变量?

【问题讨论】:

  • 您用 substrate 标记了您的问题,您是否对 Substrate、Polkadot 等人的专用 Stack Exchange 问答网站感兴趣? -- 查看Area51 Substrate Proposal

标签: substrate


【解决方案1】:

以上两个答案在他们自己的意义上/时间上都是正确的。这是一个更准确的概述:

  • runtime_io::print("..."); 已被移动。您现在可以使用来自sp-runtime::print() 的相同功能。这些将在名为runtime 的日志目标中可见。所以你必须做RUST_LOG=runtime=debug。您仍在拨打sp_io under the hood though。另外,请注意frame_support 正在为您重新导出此内容。大多数托盘都需要frame_support,这使得使用更容易。
  • 如果您想为 wasm 和 native 编译,并且只想为本地执行打印,请使用 sp_std::if_std!{} 宏。
  • 最后,您可以使用frame_support::debug 模块。该模块提供了上述两个的包装器,以使使用更容易,更 rust-like。和普通的logger类似,可以使用debug::native::warn!(...)等。

最后一个有用的提示是:如果可能,您可以使用println! 来膨胀您的代码,然后使用SKIP_WASM_BUILD=1 cargo run [xxx]。当您正在开发并希望在没有上述任何设置的情况下快速调试打印时,这很有帮助。

【讨论】:

  • 如果你想在你的终端输出打印日志来调试一个特定托盘的测试时变量的值,你可以运行在你的托盘println!("<INSERT_MESSAGE> {:#?}", <INSERT_VARIABLE_NAME>);的函数中添加以下内容,然后使用 SKIP_WASM_BUILD=1 RUST_LOG=runtime=debug cargo test --package <INSERT_PALLET_NAME> -- --nocapture 运行测试
  • @LukeSchoen 你的答案应该是最好的,最新的答案!谢谢
【解决方案2】:

您还可以使用sp-std 中包含的if_std! 宏:

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

if_std! 是一个功能门,仅应在启用std 功能时运行。

示例

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

这更好,因为您可以println 变量和东西,而不是简单地打印一个字符串。

【讨论】:

    【解决方案3】:

    作为 Substrate 开发的新手,我发现最直接的方法是使用runtime_io::print()

    例子:

    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(());
        }
      }
    }
    
    

    然后该消息将出现在控制台中。快速注意它,因为它不断滚动。

    有关完整示例,请参阅TCR tutorial example in github

    【讨论】:

    • 这个答案不再有效,可以追溯到在基板中重命名板条箱之前。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多