简体   繁体   English

无法在 Rust 中的程序宏函数声明中打印

[英]Unable to print in a procedural macro function declaration in Rust

I am using a proc_macro and want to print some verbose for debugging.我正在使用proc_macro并想打印一些详细信息以进行调试。 The println! println! statement doesn't print anything.语句不打印任何内容。

This is the macro call:这是宏调用:

decl_module! {

    /// The module declaration.
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        // A default function for depositing events
        fn deposit_event() = default;

        /// Allow a user to claim ownership of an unclaimed proof
        fn create_claim(origin, proof: Vec<u8>) -> DispatchResult {
            // Verify that the incoming transaction is signed and store who the
            // caller of this function is.
            let sender = ensure_signed(origin)?;
            println!("send is: {}", sender);

            // Verify that the specified proof has not been claimed yet or error with the message
            ensure!(!Proofs::<T>::exists(&proof), "This proof has already been claimed.");

            // Call the `system` pallet to get the current block number
            let current_block = <system::Module<T>>::block_number();

            // Store the proof with the sender and the current block number
            Proofs::<T>::insert(&proof, (sender.clone(), current_block));

            // Emit an event that the claim was created
            Self::deposit_event(RawEvent::ClaimCreated(sender, proof));
            Ok(())
        }

        /// Allow the owner to revoke their claim
        fn revoke_claim(origin, proof: Vec<u8>) -> DispatchResult {
            // Determine who is calling the function
            let sender = ensure_signed(origin)?;

            // Verify that the specified proof has been claimed
            ensure!(Proofs::<T>::exists(&proof), "This proof has not been stored yet.");

            // Get owner of the claim
            let (owner, _) = Proofs::<T>::get(&proof);

            // Verify that sender of the current call is the claim owner
            ensure!(sender == owner, "You must own this claim to revoke it.");

            // Remove claim from storage
            Proofs::<T>::remove(&proof);

            // Emit an event that the claim was erased
            Self::deposit_event(RawEvent::ClaimRevoked(sender, proof));
            Ok(())
        }
    }
}

It is taken from here .它是从这里拍摄的。 I added the following line:我添加了以下行:

println!("send is: {}", sender);

I am running a blockchain(Polkadot dApp), in the terminal (or anywhere), I could not see the output message.我正在运行一个区块链(Polkadot dApp),在终端(或任何地方),我看不到输出消息。 Note: everything is working fine but I am unable to print.注意:一切正常,但我无法打印。

This macro call generates code that contains a print statement.此宏调用生成包含打印语句的代码。 It does not run that code.它不运行该代码。 You will not see the print output until the code is run and you call create_claim .在运行代码并调用create_claim之前,您不会看到打印输出。

If you want to debug your macro call, there are severaltools for macros-by-example, but I don't know if they also work for procedural macros or if there are equivalents.如果你想调试你的宏调用,有几个宏示例的工具,但我不知道它们是否也适用于程序宏,或者是否有等价物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM