Skip to content

Commit 8a5dba1

Browse files
authored
feat(wasm): support tracing-chrome in wasm (#10246)
* Writer * Fix clippy * Fix clippy * cleanupGlobalTrace early * Fix fmt * Update writer api * Fix fmt
1 parent 708bfb0 commit 8a5dba1

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

crates/rspack_tracing/src/chrome.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct ChromeTracer {
1313

1414
impl Tracer for ChromeTracer {
1515
fn setup(&mut self, output: &str) -> Option<Layered> {
16-
let trace_writer = TraceWriter::from(output);
16+
let trace_writer = TraceWriter::from(output.to_owned());
1717
let (chrome_layer, guard) = ChromeLayerBuilder::new()
1818
.trace_style(TraceStyle::Async)
1919
.include_args(true)

crates/rspack_tracing/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@ mod chrome;
22
mod stdout;
33
mod tracer;
44

5-
use std::{fs, io, path::Path};
5+
use std::{fs, io, path::PathBuf};
66

77
pub use chrome::ChromeTracer;
88
pub use stdout::StdoutTracer;
99
pub use tracer::Tracer;
1010
use tracing_subscriber::fmt::writer::BoxMakeWriter;
11-
pub(crate) enum TraceWriter<'a> {
11+
pub(crate) enum TraceWriter {
1212
Stdout,
1313
Stderr,
14-
File { path: &'a Path },
14+
File { path: PathBuf },
1515
}
1616

17-
impl<'a> From<&'a str> for TraceWriter<'a> {
18-
fn from(s: &'a str) -> Self {
19-
match s {
17+
impl From<String> for TraceWriter {
18+
fn from(s: String) -> Self {
19+
match s.as_str() {
2020
"stdout" => Self::Stdout,
2121
"stderr" => Self::Stderr,
22-
path => Self::File {
23-
path: Path::new(path),
24-
},
22+
_ => Self::File { path: s.into() },
2523
}
2624
}
2725
}
2826

29-
impl TraceWriter<'_> {
27+
impl TraceWriter {
3028
pub fn make_writer(&self) -> BoxMakeWriter {
3129
match self {
3230
TraceWriter::Stdout => BoxMakeWriter::new(io::stdout),

crates/rspack_tracing/src/stdout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct StdoutTracer;
1010
impl Tracer for StdoutTracer {
1111
fn setup(&mut self, output: &str) -> Option<Layered> {
1212
use tracing_subscriber::{fmt, prelude::*};
13-
let trace_writer = TraceWriter::from(output);
13+
let trace_writer = TraceWriter::from(output.to_owned());
1414
Some(
1515
fmt::layer()
1616
.pretty()

crates/rspack_tracing_chrome/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub struct ChromeLayerBuilder<S>
5252
where
5353
S: Subscriber + for<'span> LookupSpan<'span> + Send + Sync,
5454
{
55-
out_writer: Option<Box<dyn Write + Send>>,
55+
out_writer: Option<Box<dyn FnOnce() -> Box<dyn Write> + Send>>,
5656
name_fn: Option<NameFn<S>>,
5757
cat_fn: Option<NameFn<S>>,
5858
include_args: bool,
@@ -97,7 +97,7 @@ where
9797
///
9898
/// If `file` could not be opened/created. To handle errors,
9999
/// open a file and pass it to [`writer`](crate::ChromeLayerBuilder::writer) instead.
100-
pub fn file<P: AsRef<Path>>(self, file: P) -> Self {
100+
pub fn file<P: AsRef<Path> + Send + 'static>(self, file: P) -> Self {
101101
self.writer(std::fs::File::create(file).expect("Failed to create trace file."))
102102
}
103103

@@ -112,7 +112,7 @@ where
112112
/// # tracing_subscriber::registry().with(layer).init();
113113
/// ```
114114
pub fn writer<W: Write + Send + 'static>(mut self, writer: W) -> Self {
115-
self.out_writer = Some(Box::new(writer));
115+
self.out_writer = Some(Box::new(|| Box::new(writer)));
116116
self
117117
}
118118

@@ -285,11 +285,10 @@ where
285285
let (tx, rx) = mpsc::channel();
286286
OUT.with(|val| val.replace(Some(tx.clone())));
287287

288-
let out_writer = builder
289-
.out_writer
290-
.unwrap_or_else(|| create_default_writer());
288+
let out_writer = builder.out_writer;
291289

292290
let handle = std::thread::spawn(move || {
291+
let out_writer = out_writer.map_or_else(|| create_default_writer() as _, |f| f());
293292
let mut write = BufWriter::new(out_writer);
294293
write.write_all(b"[\n").unwrap();
295294

packages/rspack/src/Compiler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ class Compiler {
459459
this.hooks.afterDone.call(stats!);
460460

461461
instanceBinding.shutdownAsyncRuntime();
462+
instanceBinding.cleanupGlobalTrace();
462463
isRuntimeShutdown = true;
463464
};
464465

0 commit comments

Comments
 (0)