Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up for: Use the umbrella crate for the parachain template #5993 #7464

Open
wants to merge 55 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
458f1d0
removing unused imports
seemantaggarwal Feb 5, 2025
a7eafd4
cargo lock commit
seemantaggarwal Feb 5, 2025
1b4dadf
removing sc-tracing from parachain node
seemantaggarwal Feb 5, 2025
31c7872
restoring dependencies
seemantaggarwal Feb 6, 2025
5a107ef
Merge branch 'master' into seemant-parachain-umbrella-crate-mig
seemantaggarwal Feb 6, 2025
60a91c2
adding sc-tracing in umbrella
seemantaggarwal Feb 6, 2025
64a49ad
importing sc-tracing
seemantaggarwal Feb 6, 2025
397a818
using sc-tracing from polkadot sdk
seemantaggarwal Feb 6, 2025
a889bbc
adding sc-tracing-proc-macro
seemantaggarwal Feb 6, 2025
9ae57ca
updating cargo toml for umbrella
seemantaggarwal Feb 6, 2025
b2c016a
Update umbrella/Cargo.toml
seemantaggarwal Feb 6, 2025
e15c49d
removing breaking umbrella updates
seemantaggarwal Feb 6, 2025
ec70c2e
trial and error
seemantaggarwal Feb 7, 2025
8a09b44
creating a new feature for umbrella
seemantaggarwal Feb 10, 2025
38c3dcc
Merge branch 'master' into seemant-parachain-umbrella-crate-mig
seemantaggarwal Feb 10, 2025
a62bced
cargo fmt
seemantaggarwal Feb 10, 2025
afcd41f
adding additional crate_name resolution for sc-tracing
seemantaggarwal Feb 13, 2025
db8ca90
fast forwarding Cargo.lock
seemantaggarwal Feb 13, 2025
e20a4f1
adding prdoc
seemantaggarwal Feb 13, 2025
718604e
Merge branch 'master' into seemant-parachain-umbrella-crate-mig
seemantaggarwal Feb 13, 2025
ab2822e
Merge branch 'master' into seemant-parachain-umbrella-crate-mig
seemantaggarwal Feb 14, 2025
8310ca2
taplo and fmt
seemantaggarwal Feb 14, 2025
3ef39b8
cargo.lock patch
seemantaggarwal Feb 14, 2025
0ae2a6c
cargo fmt
seemantaggarwal Feb 14, 2025
02d57d5
taplo format
seemantaggarwal Feb 14, 2025
1c69a34
simplifying crate_name code
seemantaggarwal Feb 14, 2025
f0f65c8
Merge branch 'master' into seemant-parachain-umbrella-crate-mig
seemantaggarwal Feb 14, 2025
3f36de3
step towards replicating codec into sc-tracing
seemantaggarwal Feb 16, 2025
ad6e74a
effort for including sc-tracing in polkadot-sdk
seemantaggarwal Feb 17, 2025
4ad0f0a
changing logic from startswith to fix value for polkadot-sdk resolution
seemantaggarwal Feb 17, 2025
2f95de5
reverting last commit
seemantaggarwal Feb 17, 2025
c7a07b9
cargo fmt
seemantaggarwal Feb 17, 2025
ac9c846
Merge branch 'master' into seemant-parachain-umbrella-crate-mig
seemantaggarwal Feb 17, 2025
5166954
making further changes
seemantaggarwal Feb 18, 2025
e2f570d
further effort, no success
seemantaggarwal Feb 18, 2025
52e0448
implementing suggested changes
seemantaggarwal Feb 23, 2025
13142f1
cargo and taplo
seemantaggarwal Feb 23, 2025
41f6258
resolving cargo.lock conflicts
seemantaggarwal Feb 23, 2025
7cf4e92
Merge branch 'master' into seemant-parachain-umbrella-crate-mig
seemantaggarwal Feb 23, 2025
41a860e
cargo fmt
seemantaggarwal Feb 23, 2025
60f3a6a
adding sc_tracing::loggin in sc-tracing feature
seemantaggarwal Feb 23, 2025
1ed9c36
removing unused imports
seemantaggarwal Feb 23, 2025
c93ad26
fixing umbrella crate changes
seemantaggarwal Feb 23, 2025
5391802
reverting changes from Cargo.toml
seemantaggarwal Feb 23, 2025
00c3bfa
explicit import for sc_tracing::logging
seemantaggarwal Feb 23, 2025
f0128f5
fixes
serban300 Feb 24, 2025
38d5c3c
messy attempt to re‑exports the logging module so that it’s availabl…
seemantaggarwal Mar 6, 2025
545d223
Update from seemantaggarwal running command 'fmt'
github-actions[bot] Mar 6, 2025
3cf488b
adding licenses
seemantaggarwal Mar 8, 2025
8f1997e
Merge branch 'master' into seemant-parachain-umbrella-crate-mig
seemantaggarwal Mar 8, 2025
10b5b92
conflict resolution on Cargo.lock
seemantaggarwal Mar 8, 2025
abf5bf5
fix errors
iulianbarbu Mar 11, 2025
51818d8
addressing comments
seemantaggarwal Mar 15, 2025
98a0fb2
Merge branch 'master' into seemant-parachain-umbrella-crate-mig
seemantaggarwal Mar 15, 2025
47e892d
Update from github-actions[bot] running command 'fmt'
github-actions[bot] Mar 15, 2025
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
11 changes: 4 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions prdoc/pr_7464.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: 'Enable importing sc-tracing macros through polkadot-sdk'
doc:
- audience: Node Dev
description: |-
This PR makes it possible to use the sc-tracing macros when they are imported through the umbrella crate.

crates:
- name: parachain-template-node
bump: minor
59 changes: 33 additions & 26 deletions substrate/client/tracing/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
// limitations under the License.

use proc_macro::TokenStream;
use proc_macro2::Span;
use proc_macro_crate::{crate_name, FoundCrate};
use quote::quote;
use syn::{Error, Expr, Ident, ItemFn};
use syn::{Error, Expr, ItemFn};

/// Add a log prefix to the function.
///
Expand Down Expand Up @@ -102,40 +100,49 @@ use syn::{Error, Expr, Ident, ItemFn};
/// 2020-10-16 08:12:58 [open-harbor-1619] 〽️ Prometheus server started at 127.0.0.1:9615
/// 2020-10-16 08:12:58 [open-harbor-1619] Listening for new connections on 127.0.0.1:9944.
/// ```
mod utils;

#[proc_macro_attribute]
pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
let item_fn = syn::parse_macro_input!(item as ItemFn);

// Ensure an argument was provided.
if arg.is_empty() {
return Error::new(
Span::call_site(),
"missing argument: name of the node. Example: sc_cli::prefix_logs_with(<expr>)",
proc_macro2::Span::call_site(),
"missing argument: prefix. Example: prefix_logs_with(\"Relaychain\")",
)
.to_compile_error()
.into()
.into();
}

let name = syn::parse_macro_input!(arg as Expr);
let prefix_expr = syn::parse_macro_input!(arg as Expr);
let item_fn = syn::parse_macro_input!(item as ItemFn);

let crate_name = match crate_name("sc-tracing") {
Ok(FoundCrate::Itself) => Ident::new("sc_tracing", Span::call_site()),
Ok(FoundCrate::Name(crate_name)) => Ident::new(&crate_name, Span::call_site()),
Err(e) => return Error::new(Span::call_site(), e).to_compile_error().into(),
// Resolve the proper sc_tracing path.
let resolved_crate = match utils::resolve_sc_tracing() {
Ok(path) => path,
Err(err) => return err.to_compile_error().into(),
};

let ItemFn { attrs, vis, sig, block } = item_fn;
let syn::ItemFn { attrs, vis, sig, block } = item_fn;

(quote! {
#(#attrs)*
#vis #sig {
let span = #crate_name::tracing::info_span!(
#crate_name::logging::PREFIX_LOG_SPAN,
name = #name,
);
let _enter = span.enter();

#block
// Generate different output based on whether the function is async.
let output = if sig.asyncness.is_some() {
// Async branch: wrap the block in a closure that returns an async block.
quote! {
#(#attrs)*
#vis #sig {
#resolved_crate::logging::apply_prefix_async(#prefix_expr, || async { #block }).await
}
}
} else {
// Sync branch: call the synchronous logging helper.
quote! {
#(#attrs)*
#vis #sig {
#resolved_crate::logging::apply_prefix_sync(#prefix_expr, || { #block })
}
}
})
.into()
};

output.into()
}
34 changes: 34 additions & 0 deletions substrate/client/tracing/proc-macro/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use proc_macro2::Span;
use proc_macro_crate::{crate_name, FoundCrate};
use syn::{Path, Result};

/// Resolve the correct path for sc_tracing:
/// - If `polkadot-sdk` is in scope, returns a Path corresponding to `polkadot_sdk::sc_tracing`
/// - Otherwise, falls back to `sc_tracing`
pub fn resolve_sc_tracing() -> Result<Path> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to put this logic in a separate function and file if we don't need to reuse it.

match crate_name("polkadot-sdk") {
Ok(FoundCrate::Itself) => syn::parse_str("polkadot_sdk::sc_tracing"),
Ok(FoundCrate::Name(sdk_name)) => syn::parse_str(&format!("{}::sc_tracing", sdk_name)),
Err(_) => match crate_name("sc-tracing") {
Ok(FoundCrate::Itself) => syn::parse_str("sc_tracing"),
Ok(FoundCrate::Name(name)) => syn::parse_str(&name),
Err(e) => Err(syn::Error::new(Span::call_site(), e)),
},
}
}
19 changes: 19 additions & 0 deletions substrate/client/tracing/src/logging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ macro_rules! enable_log_reloading {
}};
}

/// Synchronous helper: applies the log prefix for the duration of the function call.
pub fn apply_prefix_sync<F, R>(prefix: impl AsRef<str>, f: F) -> R
where
F: FnOnce() -> R,
{
::log::info!("Applying log prefix (sync): {}", prefix.as_ref());
f()
}

/// Asynchronous helper: applies the log prefix for the duration of the async function call.
pub async fn apply_prefix_async<F, Fut, R>(prefix: impl AsRef<str>, f: F) -> R
where
F: FnOnce() -> Fut,
Fut: std::future::Future<Output = R>,
{
::log::info!("Applying log prefix (async): {}", prefix.as_ref());
f().await
}

/// Convert a `Option<LevelFilter>` to a [`log::LevelFilter`].
///
/// `None` is interpreted as `Info`.
Expand Down
3 changes: 0 additions & 3 deletions templates/parachain/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ build = "build.rs"

[dependencies]
clap = { features = ["derive"], workspace = true }
codec = { workspace = true, default-features = true }
color-print = { workspace = true }
docify = { workspace = true }
futures = { workspace = true }
jsonrpsee = { features = ["server"], workspace = true }
log = { workspace = true, default-features = true }
serde = { features = ["derive"], workspace = true, default-features = true }
serde_json = { workspace = true, default-features = true }

polkadot-sdk = { workspace = true, features = ["node"] }

parachain-template-runtime = { workspace = true }

# Substrate
prometheus-endpoint = { workspace = true, default-features = true }
sc-tracing = { workspace = true, default-features = true }

[build-dependencies]
polkadot-sdk = { workspace = true, features = ["substrate-build-script-utils"] }
Expand Down
Loading