Init RuntimeLogger automatically for each runtime api call (#8128)

* Init `RuntimeLogger` automatically for each runtime api call

This pr change the runtime api in such a way to always and automatically
enable the `RuntimeLogger`. This enables the user to use `log` or
`tracing` from inside the runtime to create log messages. As logging
introduces some extra code and especially increases the size of the wasm
blob. It is advised to disable all logging completely with
`sp-api/disable-logging` when doing the wasm builds for the on-chain
wasm runtime.

Besides these changes, the pr also brings most of the logging found in
frame to the same format "runtime::*".

* Update frame/im-online/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update test-utils/runtime/Cargo.toml

* Fix test

* Don't use tracing in the runtime, as we don't support it :D

* Fixes

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Bastian Köcher
2021-03-01 15:29:17 +01:00
committed by GitHub
parent f2d9bb9ea6
commit 68390d4085
65 changed files with 571 additions and 422 deletions
+24 -1
View File
@@ -43,6 +43,7 @@ impl WasmBuilderSelectProject {
rust_flags: Vec::new(),
file_name: None,
project_cargo_toml: get_manifest_dir().join("Cargo.toml"),
features_to_enable: Vec::new(),
}
}
@@ -60,6 +61,7 @@ impl WasmBuilderSelectProject {
rust_flags: Vec::new(),
file_name: None,
project_cargo_toml: path,
features_to_enable: Vec::new(),
})
} else {
Err("Project path must point to the `Cargo.toml` of the project")
@@ -88,6 +90,8 @@ pub struct WasmBuilder {
/// The path to the `Cargo.toml` of the project that should be built
/// for wasm.
project_cargo_toml: PathBuf,
/// Features that should be enabled when building the wasm binary.
features_to_enable: Vec<String>,
}
impl WasmBuilder {
@@ -132,10 +136,20 @@ impl WasmBuilder {
self
}
/// Enable the given feature when building the wasm binary.
///
/// `feature` needs to be a valid feature that is defined in the project `Cargo.toml`.
pub fn enable_feature(mut self, feature: impl Into<String>) -> Self {
self.features_to_enable.push(feature.into());
self
}
/// Build the WASM binary.
pub fn build(self) {
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!"));
let file_path = out_dir.join(self.file_name.unwrap_or_else(|| "wasm_binary.rs".into()));
let file_path = out_dir.join(
self.file_name.clone().unwrap_or_else(|| "wasm_binary.rs".into()),
);
if check_skip_build() {
// If we skip the build, we still want to make sure to be called when an env variable
@@ -151,6 +165,8 @@ impl WasmBuilder {
file_path,
self.project_cargo_toml,
self.rust_flags.into_iter().map(|f| format!("{} ", f)).collect(),
self.features_to_enable,
self.file_name,
);
// As last step we need to generate our `rerun-if-changed` stuff. If a build fails, we don't
@@ -200,10 +216,15 @@ fn generate_rerun_if_changed_instructions() {
/// constant `WASM_BINARY`, which contains the built WASM binary.
/// `project_cargo_toml` - The path to the `Cargo.toml` of the project that should be built.
/// `default_rustflags` - Default `RUSTFLAGS` that will always be set for the build.
/// `features_to_enable` - Features that should be enabled for the project.
/// `wasm_binary_name` - The optional wasm binary name that is extended with `.compact.wasm`.
/// If `None`, the project name will be used.
fn build_project(
file_name: PathBuf,
project_cargo_toml: PathBuf,
default_rustflags: String,
features_to_enable: Vec<String>,
wasm_binary_name: Option<String>,
) {
let cargo_cmd = match crate::prerequisites::check() {
Ok(cmd) => cmd,
@@ -217,6 +238,8 @@ fn build_project(
&project_cargo_toml,
&default_rustflags,
cargo_cmd,
features_to_enable,
wasm_binary_name,
);
let (wasm_binary, wasm_binary_bloaty) = if let Some(wasm_binary) = wasm_binary {
@@ -97,6 +97,8 @@ pub(crate) fn create_and_compile(
project_cargo_toml: &Path,
default_rustflags: &str,
cargo_cmd: CargoCommandVersioned,
features_to_enable: Vec<String>,
wasm_binary_name: Option<String>,
) -> (Option<WasmBinary>, WasmBinaryBloaty) {
let wasm_workspace_root = get_wasm_workspace_root();
let wasm_workspace = wasm_workspace_root.join("wbuild");
@@ -108,12 +110,14 @@ pub(crate) fn create_and_compile(
&wasm_workspace,
&crate_metadata,
&crate_metadata.workspace_root,
features_to_enable,
);
build_project(&project, default_rustflags, cargo_cmd);
let (wasm_binary, bloaty) = compact_wasm_file(
&project,
project_cargo_toml,
wasm_binary_name,
);
wasm_binary.as_ref().map(|wasm_binary|
@@ -199,7 +203,7 @@ fn create_project_cargo_toml(
crate_name: &str,
crate_path: &Path,
wasm_binary: &str,
enabled_features: &[String],
enabled_features: impl Iterator<Item = String>,
) {
let mut workspace_toml: Table = toml::from_str(
&fs::read_to_string(
@@ -265,7 +269,7 @@ fn create_project_cargo_toml(
wasm_project.insert("package".into(), crate_name.into());
wasm_project.insert("path".into(), crate_path.display().to_string().into());
wasm_project.insert("default-features".into(), false.into());
wasm_project.insert("features".into(), enabled_features.to_vec().into());
wasm_project.insert("features".into(), enabled_features.collect::<Vec<_>>().into());
dependencies.insert("wasm-project".into(), wasm_project.into());
@@ -339,6 +343,7 @@ fn create_project(
wasm_workspace: &Path,
crate_metadata: &Metadata,
workspace_root_path: &Path,
features_to_enable: Vec<String>,
) -> PathBuf {
let crate_name = get_crate_name(project_cargo_toml);
let crate_path = project_cargo_toml.parent().expect("Parent path exists; qed");
@@ -354,13 +359,16 @@ fn create_project(
enabled_features.push("runtime-wasm".into());
}
let mut enabled_features = enabled_features.into_iter().collect::<HashSet<_>>();
enabled_features.extend(features_to_enable.into_iter());
create_project_cargo_toml(
&wasm_project_folder,
workspace_root_path,
&crate_name,
&crate_path,
&wasm_binary,
&enabled_features,
enabled_features.into_iter(),
);
write_file_if_changed(
@@ -437,16 +445,22 @@ fn build_project(project: &Path, default_rustflags: &str, cargo_cmd: CargoComman
fn compact_wasm_file(
project: &Path,
cargo_manifest: &Path,
wasm_binary_name: Option<String>,
) -> (Option<WasmBinary>, WasmBinaryBloaty) {
let is_release_build = is_release_build();
let target = if is_release_build { "release" } else { "debug" };
let wasm_binary = get_wasm_binary_name(cargo_manifest);
let default_wasm_binary_name = get_wasm_binary_name(cargo_manifest);
let wasm_file = project.join("target/wasm32-unknown-unknown")
.join(target)
.join(format!("{}.wasm", wasm_binary));
.join(format!("{}.wasm", default_wasm_binary_name));
let wasm_compact_file = if is_release_build {
let wasm_compact_file = project.join(format!("{}.compact.wasm", wasm_binary));
let wasm_compact_file = project.join(
format!(
"{}.compact.wasm",
wasm_binary_name.clone().unwrap_or_else(|| default_wasm_binary_name.clone()),
)
);
wasm_gc::garbage_collect_file(&wasm_file, &wasm_compact_file)
.expect("Failed to compact generated WASM binary.");
Some(WasmBinary(wasm_compact_file))
@@ -454,7 +468,16 @@ fn compact_wasm_file(
None
};
(wasm_compact_file, WasmBinaryBloaty(wasm_file))
let bloaty_file_name = if let Some(name) = wasm_binary_name {
format!("{}.wasm", name)
} else {
format!("{}.wasm", default_wasm_binary_name)
};
let bloaty_file = project.join(bloaty_file_name);
fs::copy(wasm_file, &bloaty_file).expect("Copying the bloaty file to the project dir.");
(wasm_compact_file, WasmBinaryBloaty(bloaty_file))
}
/// Custom wrapper for a [`cargo_metadata::Package`] to store it in