substrate-developer-hub / substrate-cms

5 stars 5 forks source link

Restore "Print a message" recipe #14

Open JoshOrndorff opened 5 years ago

JoshOrndorff commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

You can also show sp_std::if_std! {} and with this: https://github.com/paritytech/substrate/commit/cb9c1818f198b946b402652d173bbd76e8fbe7d0 people are always run the runtime in native (when possible)