Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,51 @@ impl From<LogLevel> for log::Level {
}
}

/// TODO
Comment thread
athei marked this conversation as resolved.
Outdated
#[derive(Encode, Decode, PassByEnum, Copy, Clone)]
pub enum LogLevelFilter {
/// `Off` log level.
Off = 0,
/// `Error` log level.
Error = 1,
/// `Warn` log level.
Warn = 2,
/// `Info` log level.
Info = 3,
/// `Debug` log level.
Debug = 4,
/// `Trace` log level.
Trace = 5,
}

impl From<LogLevelFilter> for log::LevelFilter {
fn from(l: LogLevelFilter) -> Self {
use self::LogLevelFilter::*;
match l {
Off => Self::Off,
Error => Self::Error,
Warn => Self::Warn,
Info => Self::Info,
Debug => Self::Debug,
Trace => Self::Trace,
}
}
}

impl From<log::LevelFilter> for LogLevelFilter {
fn from(l: log::LevelFilter) -> Self {
use log::LevelFilter::*;
match l {
Off => Self::Off,
Error => Self::Error,
Warn => Self::Warn,
Info => Self::Info,
Debug => Self::Debug,
Trace => Self::Trace,
}
}
}

/// Encodes the given value into a buffer and returns the pointer and the length as a single `u64`.
///
/// When Substrate calls into Wasm it expects a fixed signature for functions exported
Expand Down
6 changes: 5 additions & 1 deletion primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use sp_core::{
use sp_keystore::{KeystoreExt, SyncCryptoStore};

use sp_core::{
OpaquePeerId, crypto::KeyTypeId, ed25519, sr25519, ecdsa, H256, LogLevel,
OpaquePeerId, crypto::KeyTypeId, ed25519, sr25519, ecdsa, H256, LogLevel, LogLevelFilter,
offchain::{
Timestamp, HttpRequestId, HttpRequestStatus, HttpError, StorageKind, OpaqueNetworkState,
},
Expand Down Expand Up @@ -1082,6 +1082,10 @@ pub trait Logging {
)
}
}

fn max_level() -> LogLevelFilter {
log::max_level().into()
}
}

#[derive(Encode, Decode)]
Expand Down
28 changes: 16 additions & 12 deletions primitives/runtime/src/runtime_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl RuntimeLogger {
//
// If we don't set any level, logging is disabled
// completly.
log::set_max_level(log::LevelFilter::Trace);
log::set_max_level(sp_io::logging::max_level().into());
Comment thread
athei marked this conversation as resolved.
}
}

Expand Down Expand Up @@ -81,28 +81,32 @@ mod tests {
TestClientBuilder, runtime::TestAPI,
};
use sp_api::{ProvideRuntimeApi, BlockId};
use std::{env, str::FromStr};

#[test]
fn ensure_runtime_logger_works() {
if std::env::var("RUN_TEST").is_ok() {
fn ensure_runtime_logger_respects_host_max_log_level() {
if env::var("RUN_TEST").is_ok() {
sp_tracing::try_init_simple();
log::set_max_level(log::LevelFilter::from_str(&env::var("RUST_LOG").unwrap()).unwrap());

let client = TestClientBuilder::new()
.set_execution_strategy(ExecutionStrategy::AlwaysWasm).build();
let runtime_api = client.runtime_api();
let block_id = BlockId::Number(0);
runtime_api.do_trace_log(&block_id).expect("Logging should not fail");
} else {
let executable = std::env::current_exe().unwrap();
let output = std::process::Command::new(executable)
.env("RUN_TEST", "1")
.env("RUST_LOG", "trace")
.args(&["--nocapture", "ensure_runtime_logger_works"])
.output()
.unwrap();
for (level, should_print) in &[("trace", true), ("info", false)] {
let executable = std::env::current_exe().unwrap();
let output = std::process::Command::new(executable)
.env("RUN_TEST", "1")
.env("RUST_LOG", level)
.args(&["--nocapture", "ensure_runtime_logger_respects_host_max_log_level"])
.output()
.unwrap();

let output = dbg!(String::from_utf8(output.stderr).unwrap());
assert!(output.contains("Hey I'm runtime"));
let output = String::from_utf8(output.stderr).unwrap();
assert!(output.contains("Hey I'm runtime") == *should_print);
}
}
}
}
2 changes: 1 addition & 1 deletion test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ cfg_if! {
}

fn do_trace_log() {
log::error!("Hey I'm runtime: {}", log::STATIC_MAX_LEVEL);
log::trace!("Hey I'm runtime: {}", log::STATIC_MAX_LEVEL);
}
}

Expand Down