Skip to content

Commit

Permalink
feat(napi): add tracing via OXC_LOG:DEBUG (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Jun 30, 2024
1 parent 068aeda commit 2551871
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 6 deletions.
98 changes: 97 additions & 1 deletion Cargo.lock

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

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ e.g.

The input values are `options`, `path` and `specifier`, the returned value is `ret`.

### NAPI

```
OXC_LOG=DEBUG your_program
```

### Rolldown

```bash
Expand Down
2 changes: 2 additions & 0 deletions examples/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ fn main() {
let options = ResolveOptions {
alias_fields: vec![vec!["browser".into()]],
alias: vec![("asdf".into(), vec![AliasValue::from("./test.js")])],
extensions: vec![".js".into(), ".ts".into()],
extension_alias: vec![(".js".into(), vec![".ts".into()])],
..ResolveOptions::default()
};

Expand Down
9 changes: 5 additions & 4 deletions napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ test = false
doctest = false

[dependencies]
oxc_resolver = { path = ".." }
napi = { version = "2.16.7", default-features = false, features = ["napi3", "serde-json", "async"] }
napi-derive = { version = "2.16.6" }
tokio = "1.38.0"
oxc_resolver = { path = ".." }
napi = { version = "2.16.7", default-features = false, features = ["napi3", "serde-json", "async"] }
napi-derive = { version = "2.16.6" }
tokio = "1.38.0"
tracing-subscriber = { version = "0.3.18", features = [] } # Omit the `regex` feature

[build-dependencies]
napi-build = "2.1.3"
Expand Down
7 changes: 6 additions & 1 deletion napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ use std::{
use napi_derive::napi;
use oxc_resolver::{ResolveOptions, Resolver};

use self::options::{NapiResolveOptions, StrOrStrList};
use self::{
options::{NapiResolveOptions, StrOrStrList},
tracing::init_tracing,
};

mod options;
mod tracing;

#[napi(object)]
pub struct ResolveResult {
Expand Down Expand Up @@ -47,6 +51,7 @@ pub struct ResolverFactory {
impl ResolverFactory {
#[napi(constructor)]
pub fn new(options: NapiResolveOptions) -> Self {
init_tracing();
Self { resolver: Arc::new(Resolver::new(Self::normalize_options(options))) }
}

Expand Down
23 changes: 23 additions & 0 deletions napi/src/tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::sync::OnceLock;

use tracing_subscriber::{filter::Targets, prelude::*};

/// To debug `oxc_resolver`:
/// `OXC_LOG=DEBUG your program`
pub fn init_tracing() {
static TRACING: OnceLock<()> = OnceLock::new();
TRACING.get_or_init(|| {
// Usage without the `regex` feature.
// <https://github.com/tokio-rs/tracing/issues/1436#issuecomment-918528013>
tracing_subscriber::registry()
.with(std::env::var("OXC_LOG").map_or_else(
|_| Targets::new(),
|env_var| {
use std::str::FromStr;
Targets::from_str(&env_var).unwrap()
},
))
.with(tracing_subscriber::fmt::layer())
.init();
});
}

0 comments on commit 2551871

Please sign in to comment.