Skip to content

Commit

Permalink
refactor(swc): teardown subscriber via drop
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 21, 2022
1 parent 3379259 commit 1eb5446
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 1 addition & 2 deletions packages/next-swc/crates/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ tracing = { version = "0.1.32", features = ["release_max_level_info"] }
tracing-futures = "0.2.5"
tracing-subscriber = "0.3.9"

# https://github.com/thoren-d/tracing-chrome/pull/10
tracing-chrome = { git = "https://github.com/kwonoj/tracing-chrome", rev = "0dd9d6c9e0f74f43993b58560f4ac0103e058df8" }
tracing-chrome = "0.5.0"

[build-dependencies]
napi-build = "1"
15 changes: 10 additions & 5 deletions packages/next-swc/crates/napi/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ DEALINGS IN THE SOFTWARE.
use anyhow::{anyhow, Context, Error};
use napi::{CallContext, Env, JsBuffer, JsExternal, JsString, JsUndefined, JsUnknown, Status};
use serde::de::DeserializeOwned;
use std::{any::type_name, convert::TryFrom, path::PathBuf};
use std::{any::type_name, cell::RefCell, convert::TryFrom, path::PathBuf};
use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
use tracing_subscriber::{filter, prelude::*, util::SubscriberInitExt, Layer};

Expand Down Expand Up @@ -125,17 +125,22 @@ pub fn init_custom_trace_subscriber(cx: CallContext) -> napi::Result<JsExternal>
.try_init()
.expect("Failed to register tracing subscriber");

cx.env.create_external(guard, None)
let guard_cell = RefCell::new(Some(guard));
cx.env.create_external(guard_cell, None)
}

/// Teardown currently running tracing subscriber to flush out remaining traces.
/// This should be called when parent node.js process exits, otherwise generated
/// trace will missing traces in the buffer.
/// trace may drop traces in the buffer.
#[js_function(1)]
pub fn teardown_trace_subscriber(cx: CallContext) -> napi::Result<JsUndefined> {
let guard_external = cx.get::<JsExternal>(0)?;
let guard = &*cx.env.get_value_external::<FlushGuard>(&guard_external)?;
let guard_cell = &*cx
.env
.get_value_external::<RefCell<Option<FlushGuard>>>(&guard_external)?;

guard.close();
if let Some(guard) = guard_cell.take() {
drop(guard);
}
cx.env.get_undefined()
}

0 comments on commit 1eb5446

Please sign in to comment.