-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
86 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
"Finalizer", | ||
"hasher", | ||
"importee", | ||
"napi", | ||
"oxlint", | ||
"oxlintignore", | ||
"Renamer", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
pub mod napi_error_ext; | ||
use napi::Env; | ||
use std::env; | ||
use std::sync::atomic::AtomicBool; | ||
mod into_js_unknown_vec; | ||
pub use into_js_unknown_vec::*; | ||
mod js_callback; | ||
pub use js_callback::*; | ||
use rolldown_tracing::enable_tracing_on_demand; | ||
use rolldown_tracing::try_init_tracing_with_chrome_layer; | ||
|
||
static IS_ENABLE_TRACING: AtomicBool = AtomicBool::new(false); | ||
|
||
pub fn init_custom_trace_subscriber(mut napi_env: Env) { | ||
let value = env::var("TRACE").unwrap_or_default(); | ||
if value == "1" && !IS_ENABLE_TRACING.swap(true, std::sync::atomic::Ordering::SeqCst) { | ||
let guard = enable_tracing_on_demand(); | ||
if let Some(guard) = guard { | ||
napi_env | ||
.add_env_cleanup_hook(guard, |flush_guard| { | ||
flush_guard.flush(); | ||
drop(flush_guard); | ||
}) | ||
.expect("Should able to initialize cleanup for custom trace subscriber"); | ||
pub fn try_init_custom_trace_subscriber(mut napi_env: Env) { | ||
match std::env::var("LOG_LAYER") { | ||
Ok(val) if val == "chrome" => { | ||
let guard = try_init_tracing_with_chrome_layer(); | ||
if let Some(guard) = guard { | ||
napi_env | ||
.add_env_cleanup_hook(guard, |flush_guard| { | ||
flush_guard.flush(); | ||
drop(flush_guard); | ||
}) | ||
.expect("Should able to initialize cleanup for custom trace subscriber"); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,65 @@ | ||
/// Some guidelines for tracing: | ||
/// - By default, only allow tracing events from crates of this repo. | ||
/// - Using `LOG_OUTPUT=chrome` to collect tracing events into a json file. | ||
/// - This only works on using `@rolldown/node`. If you are running rolldown in rust, this doesn't works. | ||
/// - Using `LOG=TRACE` to enable tracing or other values for more specific tracing. | ||
/// - See https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#example-syntax for more syntax details. | ||
use std::sync::atomic::AtomicBool; | ||
|
||
use tracing::{metadata::LevelFilter, Level}; | ||
use tracing::{level_filters::LevelFilter, Level}; | ||
use tracing_chrome::FlushGuard; | ||
use tracing_subscriber::EnvFilter; | ||
use tracing_subscriber::{fmt, prelude::*}; | ||
|
||
static IS_INITIALIZE: AtomicBool = AtomicBool::new(false); | ||
static IS_INITIALIZED: AtomicBool = AtomicBool::new(false); | ||
|
||
pub fn enable_tracing_on_demand() -> Option<FlushGuard> { | ||
use tracing_subscriber::{fmt, prelude::*, EnvFilter}; | ||
if !IS_INITIALIZE.swap(true, std::sync::atomic::Ordering::SeqCst) { | ||
pub fn try_init_tracing() { | ||
if std::env::var("LOG").is_err() { | ||
// tracing will slow down the bundling process, so we only enable it when `LOG` is set. | ||
return; | ||
} | ||
if !IS_INITIALIZED.swap(true, std::sync::atomic::Ordering::SeqCst) { | ||
tracing_subscriber::registry() | ||
.with(fmt::layer()) | ||
.with( | ||
tracing_subscriber::filter::Targets::new().with_targets(vec![("rolldown", Level::TRACE)]), | ||
tracing_subscriber::filter::Targets::new() | ||
.with_targets(vec![("rolldown", Level::TRACE), ("rolldown_binding", Level::TRACE)]), | ||
) | ||
.with( | ||
EnvFilter::builder() | ||
.with_env_var("LOG") | ||
.with_default_directive(LevelFilter::TRACE.into()) | ||
.from_env_lossy(), | ||
) | ||
.with(fmt::layer().pretty().without_time()) | ||
.init(); | ||
tracing::trace!("Tracing is initialized."); | ||
} | ||
} | ||
|
||
pub fn try_init_tracing_with_chrome_layer() -> Option<FlushGuard> { | ||
use tracing_chrome::ChromeLayerBuilder; | ||
use tracing_subscriber::prelude::*; | ||
if std::env::var("LOG").is_err() { | ||
// tracing will slow down the bundling process, so we only enable it when `LOG` is set. | ||
return None; | ||
} | ||
if IS_INITIALIZED.swap(true, std::sync::atomic::Ordering::SeqCst) { | ||
None | ||
} else { | ||
let (chrome_layer, guard) = ChromeLayerBuilder::new().build(); | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::filter::Targets::new() | ||
.with_targets(vec![("rolldown", Level::TRACE), ("rolldown_binding", Level::TRACE)]), | ||
) | ||
.with( | ||
EnvFilter::builder() | ||
.with_env_var("LOG") | ||
.with_default_directive(LevelFilter::TRACE.into()) | ||
.from_env_lossy(), | ||
) | ||
.with(EnvFilter::builder().with_default_directive(LevelFilter::TRACE.into()).from_env_lossy()) | ||
.with(chrome_layer) | ||
.init(); | ||
Some(guard) | ||
} | ||
None | ||
} |