|
| 1 | +/// Some guidelines for tracing: |
| 2 | +/// - By default, only allow tracing events from crates of this repo. |
| 3 | +/// - Using `LOG_OUTPUT=chrome` to collect tracing events into a json file. |
| 4 | +/// - This only works on using `@rolldown/node`. If you are running rolldown in rust, this doesn't works. |
| 5 | +/// - Using `LOG=TRACE` to enable tracing or other values for more specific tracing. |
| 6 | +/// - See https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#example-syntax for more syntax details. |
1 | 7 | use std::sync::atomic::AtomicBool; |
2 | 8 |
|
3 | | -use tracing::{metadata::LevelFilter, Level}; |
| 9 | +use tracing::{level_filters::LevelFilter, Level}; |
4 | 10 | use tracing_chrome::FlushGuard; |
| 11 | +use tracing_subscriber::EnvFilter; |
| 12 | +use tracing_subscriber::{fmt, prelude::*}; |
5 | 13 |
|
6 | | -static IS_INITIALIZE: AtomicBool = AtomicBool::new(false); |
| 14 | +static IS_INITIALIZED: AtomicBool = AtomicBool::new(false); |
7 | 15 |
|
8 | | -pub fn enable_tracing_on_demand() -> Option<FlushGuard> { |
9 | | - use tracing_subscriber::{fmt, prelude::*, EnvFilter}; |
10 | | - if !IS_INITIALIZE.swap(true, std::sync::atomic::Ordering::SeqCst) { |
| 16 | +pub fn try_init_tracing() { |
| 17 | + if std::env::var("LOG").is_err() { |
| 18 | + // tracing will slow down the bundling process, so we only enable it when `LOG` is set. |
| 19 | + return; |
| 20 | + } |
| 21 | + if !IS_INITIALIZED.swap(true, std::sync::atomic::Ordering::SeqCst) { |
11 | 22 | tracing_subscriber::registry() |
12 | | - .with(fmt::layer()) |
13 | 23 | .with( |
14 | | - tracing_subscriber::filter::Targets::new().with_targets(vec![("rolldown", Level::TRACE)]), |
| 24 | + tracing_subscriber::filter::Targets::new() |
| 25 | + .with_targets(vec![("rolldown", Level::TRACE), ("rolldown_binding", Level::TRACE)]), |
| 26 | + ) |
| 27 | + .with( |
| 28 | + EnvFilter::builder() |
| 29 | + .with_env_var("LOG") |
| 30 | + .with_default_directive(LevelFilter::TRACE.into()) |
| 31 | + .from_env_lossy(), |
| 32 | + ) |
| 33 | + .with(fmt::layer().pretty().without_time()) |
| 34 | + .init(); |
| 35 | + tracing::trace!("Tracing is initialized."); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +pub fn try_init_tracing_with_chrome_layer() -> Option<FlushGuard> { |
| 40 | + use tracing_chrome::ChromeLayerBuilder; |
| 41 | + use tracing_subscriber::prelude::*; |
| 42 | + if std::env::var("LOG").is_err() { |
| 43 | + // tracing will slow down the bundling process, so we only enable it when `LOG` is set. |
| 44 | + return None; |
| 45 | + } |
| 46 | + if IS_INITIALIZED.swap(true, std::sync::atomic::Ordering::SeqCst) { |
| 47 | + None |
| 48 | + } else { |
| 49 | + let (chrome_layer, guard) = ChromeLayerBuilder::new().build(); |
| 50 | + tracing_subscriber::registry() |
| 51 | + .with( |
| 52 | + tracing_subscriber::filter::Targets::new() |
| 53 | + .with_targets(vec![("rolldown", Level::TRACE), ("rolldown_binding", Level::TRACE)]), |
| 54 | + ) |
| 55 | + .with( |
| 56 | + EnvFilter::builder() |
| 57 | + .with_env_var("LOG") |
| 58 | + .with_default_directive(LevelFilter::TRACE.into()) |
| 59 | + .from_env_lossy(), |
15 | 60 | ) |
16 | | - .with(EnvFilter::builder().with_default_directive(LevelFilter::TRACE.into()).from_env_lossy()) |
| 61 | + .with(chrome_layer) |
17 | 62 | .init(); |
| 63 | + Some(guard) |
18 | 64 | } |
19 | | - None |
20 | 65 | } |
0 commit comments