目录
- 1. 获取substrate-node-template代码
- 2. 添加一个用于测试的pallet至依赖到pallets目录
- 3. log方式来输出信息
- 3.1 将log依赖添到cargo.toml文件
- 3.2 log-test/src/lib.rs修改call方法
- 3.3 polkadot.js.调用测试函数do_something_log_test
- 4. printable trait方式来输出信息
- 4.1 首选需要将sp_runtime加到[dependencies]
- 4.2 log-test/src/lib.rs修改代码
- 4.3 polkadot.js.调用测试函数cause_error
- 5. 直接使用print方式来输出信息
- 5.1 log-test/src/lib.rs修改call方法
- 5.2 polkadot.js.调用测试函数do_something_log_test
- 6. 使用if_std!方式来输出信息
- 6.1 log-test/src/lib.rs修改代码
- 6.2 polkadot.js.调用测试函数do_something_log_test
1. 获取substrate-node-template代码
https://github.com/substrate-developer-hub/substrate-node-template/
获取代码后切换到 polkadot-v0.9.30 (这是一个tag)
2. 添加一个用于测试的pallet至依赖到pallets目录
对于如何添加一个新的pallet中以参照:添加新的pallet
将template/src/lib.rs的代码copy到log-test/src/lib.rs
3. log方式来输出信息
3.1 将log依赖添到cargo.toml文件
https://crates.io 查看一下可用的版本号
[dependencies]
......
log={version = "0.4.17", default-features = false }
[features]
default = ["std"]
std = [
......
"log/std",
]
3.2 log-test/src/lib.rs修改call方法
使用log::info!来输出调试信息
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn do_something_log_test(origin: OriginFor<T>, something: u32) -> DispatchResult {
let who = ensure_signed(origin)?;
log::info!("do_something_log_test-> accountId={:?}, somthing={:?}", who, something);
<Something<T>>::put(something);
Self::deposit_event(Event::SomethingStored(something, who));
Ok(())
}
编译完成后直接运行:
cargo build
./target/debug/node-template --dev
3.3 polkadot.js.调用测试函数do_something_log_test
https://polkadot.js.org/apps/#/extrinsics
选中LogTestMudule模块下的doSomethingLogTest、输入13579然后提交
这时在输出屏幕上可以看到如下信息:
4. printable trait方式来输出信息
4.1 首选需要将sp_runtime加到[dependencies]
[dependencies]
......
sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
[features]
default = ["std"]
std = [
......
"sp-runtime/std",
]
4.2 log-test/src/lib.rs修改代码
mod pallet中引用Printable
#[frame_support::pallet]
pub mod pallet {
use sp_runtime::traits::Printable;
use sp_runtime::print;
......
}
在Error枚举下方,增加Printable对Error的实现
#[pallet::error]
pub enum Error<T> {
/// Error names should be descriptive.
NoneValue,
/// Errors should have helpful documentation associated with them.
StorageOverflow,
}
impl<T: Config> Printable for Error<T> {
fn print(&self) {
match self {
Error::NoneValue => "### Printable for Error is NoneValue!!!".print(),
Error::StorageOverflow => "### Printable for Error is StorageOverflow!!!".print(),
_ => "### Printable for Error is Unknow Reason!!!".print(),
}
}
}
修改cause_error方法、让其调用上述的trait
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
let _who = ensure_signed(origin)?;
// Read a value from storage.
match <Something<T>>::get() {
// Return an error if the value has not been set.
None => {
print(Error::<T>::NoneValue);
return Err(Error::<T>::NoneValue.into())
},
Some(old) => {
// Increment the value read from storage; will error in the event of overflow.
let new = old.checked_add(1).ok_or({
print(Error::<T>::StorageOverflow);
Error::<T>::StorageOverflow})?;
// Update the value in storage with the incremented result.
<Something<T>>::put(new);
Ok(())
},
}
}
编译完成后以debug方式运行:
cargo build
RUST_LOG=runtime=debug ./target/debug/node-template --dev
4.3 polkadot.js.调用测试函数cause_error
这时在输出屏幕上可以看到如下Debug信息:
5. 直接使用print方式来输出信息
5.1 log-test/src/lib.rs修改call方法
do_something_log_test来输出调试信息
pub fn do_something_log_test(origin: OriginFor<T>, something: u32) -> DispatchResult {
print("### do_something_log_test start...");
......
}
编译完成后以debug方式运行:
cargo build
RUST_LOG=runtime=debug ./target/debug/node-template --dev
5.2 polkadot.js.调用测试函数do_something_log_test
这时在输出屏幕上可以看到如下Debug信息:
6. 使用if_std!方式来输出信息
6.1 log-test/src/lib.rs修改代码
mod pallet中引用if_std
#[frame_support::pallet]
pub mod pallet {
use sp_std::if_std;
......
}
修改do_something_log_test来输出调试信息
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn do_something_log_test(origin: OriginFor<T>, something: u32) -> DispatchResult {
let who = ensure_signed(origin)?;
if_std! {
// This code is only being compiled and executed when the `std` feature is enabled.
println!("### do_something_log_test Hello native world!");
println!("### something value is: {:#?}", something);
println!("### The caller account is: {:#?}", who);
}
<Something<T>>::put(something);
Self::deposit_event(Event::SomethingStored(something, who));
Ok(())
}
编译完成后直接运行:
cargo build
./target/debug/node-template --dev
6.2 polkadot.js.调用测试函数do_something_log_test
这时在输出屏幕上可以看到如下Debug信息: