Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore "Print a message" recipe #14

Open
JoshOrndorff opened this issue Jun 14, 2019 · 4 comments
Open

Restore "Print a message" recipe #14

JoshOrndorff opened this issue Jun 14, 2019 · 4 comments

Comments

@JoshOrndorff
Copy link
Contributor

In the older runtime recipes that I deleted in 1495d8616b40, There was a section called "Print a message" which showed how to output simple debugging logs. That is not in the new https://substrate.dev/recipes .

I recently saw @shawntabrizi recommend instead to

  1. Compile wasm
  2. Add println!s
  3. Compile native

Is this a better approach?

Original

Sometimes we need to print debug messages while building applications. This recipe shows how to print messages for debugging your Substrate runtime code.

You need to include runtime_io crate from the Substrate core in order to use IO functions from within the runtime modules.

use support::{decl_module, dispatch::Result};
use runtime_io;
pub trait Trait: system::Trait {}
decl_module! {
  pub struct Module<T: Trait> for enum Call where origin: T::Origin {
    pub fn print_something(_origin) -> Result {
      // the following line prints a message on the node's terminal/console
      runtime_io::print(Hello World from Substrate Runtime!);
      Ok(())
    }
  }
}

When this function is executed, the message will appear on the terminal where the Substrate node is running. The following screenshot shows the printed message.
Text "Hello World from Substrate Runtime!"

@kaichaosun
Copy link

Add println!, then compile is annoying for just debugging. Using tests for similar purpose should be possible from my experience, but I'm not sure how hard or time-consuming in substrate development.

@JoshOrndorff
Copy link
Contributor Author

This isn't very actionable, and the recipes aren't even in the main devhub repo anymore.

FWIW the original code snippet is still basically correct except that it now comes from srml_support instead of sr_io.

@JoshOrndorff
Copy link
Contributor Author

@bkchr has observed learners asking for help with printing simple debugging lines, so this issue is still relevant. He suggested we teach them to use https://crates.parity.io/sp_runtime/fn.print.html I suggest we demonstrate this technique in the PoE tutorial.

The snippet I quoted above is still basically right, but we need to run the node with -lruntime=debug to see the message.

@bkchr
Copy link

bkchr commented Jan 21, 2020

You can also show sp_std::if_std! {} and with this: paritytech/substrate@cb9c181 people are always run the runtime in native (when possible)

@JoshOrndorff JoshOrndorff transferred this issue from polkadot-developers/substrate-developer-hub.github.io Mar 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants