Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 743981a

Browse files
authored
Fix logging from inside the WASM runtime (#7355)
* Fix logging from inside the WASM runtime When using `RuntimeLogger` to log something from the runtime, we didn't set any logging level. So, we actually did not log anything from the runtime as logging is disabled by default. This pr fixes that by setting the logging level to `TRACE`. It also adds a test to ensure this does not break again ;) * Update frame/support/src/debug.rs
1 parent 1524703 commit 743981a

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frame/support/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ smallvec = "1.4.1"
3636
pretty_assertions = "0.6.1"
3737
frame-system = { version = "2.0.0", path = "../system" }
3838
parity-util-mem = { version = "0.7.0", features = ["primitive-types"] }
39+
substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" }
40+
sp-api = { version = "2.0.0", default-features = false, path = "../../primitives/api" }
3941

4042
[features]
4143
default = ["std"]

frame/support/src/debug.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,16 @@ impl RuntimeLogger {
170170
/// This is a no-op when running natively (`std`).
171171
#[cfg(not(feature = "std"))]
172172
pub fn init() {
173-
static LOGGER: RuntimeLogger = RuntimeLogger;;
173+
static LOGGER: RuntimeLogger = RuntimeLogger;
174174
let _ = log::set_logger(&LOGGER);
175+
176+
// Set max level to `TRACE` to ensure we propagate
177+
// all log entries to the native side that will do the
178+
// final filtering on what should be printed.
179+
//
180+
// If we don't set any level, logging is disabled
181+
// completly.
182+
log::set_max_level(log::LevelFilter::Trace);
175183
}
176184
}
177185

@@ -198,3 +206,42 @@ impl log::Log for RuntimeLogger {
198206

199207
fn flush(&self) {}
200208
}
209+
210+
#[cfg(test)]
211+
mod tests {
212+
use substrate_test_runtime_client::{
213+
ExecutionStrategy, TestClientBuilderExt, DefaultTestClientBuilderExt,
214+
TestClientBuilder, runtime::TestAPI,
215+
};
216+
use sp_api::ProvideRuntimeApi;
217+
use sp_runtime::generic::BlockId;
218+
219+
#[test]
220+
fn ensure_runtime_logger_works() {
221+
let executable = std::env::current_exe().unwrap();
222+
let output = std::process::Command::new(executable)
223+
.env("RUN_TEST", "1")
224+
.env("RUST_LOG", "trace")
225+
.args(&["--nocapture", "ensure_runtime_logger_works_implementation"])
226+
.output()
227+
.unwrap();
228+
229+
let output = dbg!(String::from_utf8(output.stderr).unwrap());
230+
assert!(output.contains("Hey I'm runtime"));
231+
}
232+
233+
/// This is no actual test. It will be called by `ensure_runtime_logger_works`
234+
/// to check that the runtime can print from the wasm side using the
235+
/// `RuntimeLogger`.
236+
#[test]
237+
fn ensure_runtime_logger_works_implementation() {
238+
if std::env::var("RUN_TEST").is_ok() {
239+
sp_tracing::try_init_simple();
240+
241+
let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::AlwaysWasm).build();
242+
let runtime_api = client.runtime_api();
243+
let block_id = BlockId::Number(0);
244+
runtime_api.do_trace_log(&block_id).expect("Logging should not fail");
245+
}
246+
}
247+
}

primitives/tracing/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ pub use crate::types::{
107107
/// Ignores any error. Useful for testing.
108108
#[cfg(feature = "std")]
109109
pub fn try_init_simple() {
110-
let _ = tracing_subscriber::fmt().with_writer(std::io::stderr).try_init();
110+
let _ = tracing_subscriber::fmt()
111+
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
112+
.with_writer(std::io::stderr).try_init();
111113
}
112114

113115
#[cfg(feature = "std")]

test-utils/runtime/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ cfg_if! {
340340
/// Test that ensures that we can call a function that takes multiple
341341
/// arguments.
342342
fn test_multiple_arguments(data: Vec<u8>, other: Vec<u8>, num: u32);
343+
/// Traces log "Hey I'm runtime."
344+
fn do_trace_log();
343345
}
344346
}
345347
} else {
@@ -391,6 +393,8 @@ cfg_if! {
391393
/// Test that ensures that we can call a function that takes multiple
392394
/// arguments.
393395
fn test_multiple_arguments(data: Vec<u8>, other: Vec<u8>, num: u32);
396+
/// Traces log "Hey I'm runtime."
397+
fn do_trace_log();
394398
}
395399
}
396400
}
@@ -698,6 +702,11 @@ cfg_if! {
698702
assert_eq!(&data[..], &other[..]);
699703
assert_eq!(data.len(), num as usize);
700704
}
705+
706+
fn do_trace_log() {
707+
frame_support::debug::RuntimeLogger::init();
708+
frame_support::debug::trace!("Hey I'm runtime");
709+
}
701710
}
702711

703712
impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
@@ -944,6 +953,11 @@ cfg_if! {
944953
assert_eq!(&data[..], &other[..]);
945954
assert_eq!(data.len(), num as usize);
946955
}
956+
957+
fn do_trace_log() {
958+
frame_support::debug::RuntimeLogger::init();
959+
frame_support::debug::trace!("Hey I'm runtime");
960+
}
947961
}
948962

949963
impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {

0 commit comments

Comments
 (0)