【发布时间】:2019-07-19 09:40:31
【问题描述】:
在进行Parity Substrate 运行时开发时,如何打印调试消息以跟踪和检查我的变量?
【问题讨论】:
-
您用
substrate标记了您的问题,您是否对 Substrate、Polkadot 等人的专用 Stack Exchange 问答网站感兴趣? -- 查看Area51 Substrate Proposal
标签: substrate
在进行Parity Substrate 运行时开发时,如何打印调试消息以跟踪和检查我的变量?
【问题讨论】:
substrate 标记了您的问题,您是否对 Substrate、Polkadot 等人的专用 Stack Exchange 问答网站感兴趣? -- 查看Area51 Substrate Proposal
标签: substrate
以上两个答案在他们自己的意义上/时间上都是正确的。这是一个更准确的概述:
runtime_io::print("..."); 已被移动。您现在可以使用来自sp-runtime::print() 的相同功能。这些将在名为runtime 的日志目标中可见。所以你必须做RUST_LOG=runtime=debug。您仍在拨打sp_io under the hood though。另外,请注意frame_support 正在为您重新导出此内容。大多数托盘都需要frame_support,这使得使用更容易。 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 运行测试
您还可以使用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 变量和东西,而不是简单地打印一个字符串。
【讨论】:
作为 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。
【讨论】: