From cccf3417e5f0dace12a0da2ee157f1f284651700 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bastian=20K=C3=B6cher?= <git@kchr.de>
Date: Tue, 19 Nov 2024 22:02:12 +0000
Subject: [PATCH] Forward logging directives to Polkadot workers (#6534)

This pull request forward all the logging directives given to the node
via `RUST_LOG` or `-l` to the workers, instead of only forwarding
`RUST_LOG`.

---------

Co-authored-by: GitHub Action <action@github.com>
---
 Cargo.lock                                         |  1 +
 polkadot/node/core/pvf/Cargo.toml                  |  1 +
 polkadot/node/core/pvf/src/worker_interface.rs     |  6 ++----
 prdoc/pr_6534.prdoc                                | 10 ++++++++++
 substrate/client/tracing/src/logging/directives.rs |  7 ++++++-
 5 files changed, 20 insertions(+), 5 deletions(-)
 create mode 100644 prdoc/pr_6534.prdoc

diff --git a/Cargo.lock b/Cargo.lock
index 02d7da8f765..330c2563d97 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -17553,6 +17553,7 @@ dependencies = [
  "rococo-runtime",
  "rusty-fork",
  "sc-sysinfo",
+ "sc-tracing",
  "slotmap",
  "sp-core 28.0.0",
  "sp-maybe-compressed-blob 11.0.0",
diff --git a/polkadot/node/core/pvf/Cargo.toml b/polkadot/node/core/pvf/Cargo.toml
index a9f97c308f2..37d5878ea59 100644
--- a/polkadot/node/core/pvf/Cargo.toml
+++ b/polkadot/node/core/pvf/Cargo.toml
@@ -38,6 +38,7 @@ polkadot-node-primitives = { workspace = true, default-features = true }
 polkadot-node-subsystem = { workspace = true, default-features = true }
 polkadot-primitives = { workspace = true, default-features = true }
 
+sc-tracing = { workspace = true }
 sp-core = { workspace = true, default-features = true }
 sp-maybe-compressed-blob = { optional = true, workspace = true, default-features = true }
 polkadot-node-core-pvf-prepare-worker = { optional = true, workspace = true, default-features = true }
diff --git a/polkadot/node/core/pvf/src/worker_interface.rs b/polkadot/node/core/pvf/src/worker_interface.rs
index e63778d4692..f279fbb5354 100644
--- a/polkadot/node/core/pvf/src/worker_interface.rs
+++ b/polkadot/node/core/pvf/src/worker_interface.rs
@@ -237,10 +237,8 @@ impl WorkerHandle {
 		// Clear all env vars from the spawned process.
 		let mut command = process::Command::new(program.as_ref());
 		command.env_clear();
-		// Add back any env vars we want to keep.
-		if let Ok(value) = std::env::var("RUST_LOG") {
-			command.env("RUST_LOG", value);
-		}
+
+		command.env("RUST_LOG", sc_tracing::logging::get_directives().join(","));
 
 		let mut child = command
 			.args(extra_args)
diff --git a/prdoc/pr_6534.prdoc b/prdoc/pr_6534.prdoc
new file mode 100644
index 00000000000..7a92fe3c857
--- /dev/null
+++ b/prdoc/pr_6534.prdoc
@@ -0,0 +1,10 @@
+title: Forward logging directives to Polkadot workers
+doc:
+- audience: Node Dev
+  description: |-
+    This pull request forward all the logging directives given to the node via `RUST_LOG` or `-l` to the workers, instead of only forwarding `RUST_LOG`.
+crates:
+- name: polkadot-node-core-pvf
+  bump: patch
+- name: sc-tracing
+  bump: patch
diff --git a/substrate/client/tracing/src/logging/directives.rs b/substrate/client/tracing/src/logging/directives.rs
index a99e9c4c890..811511bb20f 100644
--- a/substrate/client/tracing/src/logging/directives.rs
+++ b/substrate/client/tracing/src/logging/directives.rs
@@ -40,7 +40,7 @@ pub(crate) fn add_default_directives(directives: &str) {
 	add_directives(directives);
 }
 
-/// Add directives to current directives
+/// Add directives to current directives.
 pub fn add_directives(directives: &str) {
 	CURRENT_DIRECTIVES
 		.get_or_init(|| Mutex::new(Vec::new()))
@@ -48,6 +48,11 @@ pub fn add_directives(directives: &str) {
 		.push(directives.to_owned());
 }
 
+/// Returns the current directives.
+pub fn get_directives() -> Vec<String> {
+	CURRENT_DIRECTIVES.get_or_init(|| Mutex::new(Vec::new())).lock().clone()
+}
+
 /// Parse `Directive` and add to default directives if successful.
 ///
 /// Ensures the supplied directive will be restored when resetting the log filter.
-- 
GitLab