https://github.com/substrate-developer-hub/substrate-node-template/
获取代码后切换到 polkadot-v0.9.30 (这是一个tag)
对于如何添加一个新的pallet中以参照:添加新的pallet
将template/src/lib.rs的代码copy到log-test/src/lib.rs
[dependencies]
......
log={version = "0.4.17", default-features = false }
[features]
default = ["std"]
std = [
......
"log/std",
]
使用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
https://polkadot.js.org/apps/#/extrinsics
选中LogTestMudule模块下的doSomethingLogTest、输入13579然后提交
这时在输出屏幕上可以看到如下信息:
[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",
]
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
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
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