Skip to content

Commit 39a3cb7

Browse files
committed
use guard
1 parent a2722a4 commit 39a3cb7

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

dsc/src/util.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ use std::collections::HashMap;
5252
use std::env;
5353
use std::io::{IsTerminal, Read, stdout, Write};
5454
use std::path::Path;
55+
use std::sync::{LazyLock, RwLock};
5556
use syntect::{
5657
easy::HighlightLines,
5758
highlighting::ThemeSet,
5859
parsing::SyntaxSet,
5960
util::{as_24_bit_terminal_escaped, LinesWithEndings}
6061
};
62+
use tokio::sync::OnceCell;
6163
use tracing::{Level, debug, error, info, warn, trace};
6264
use tracing_subscriber::{filter::EnvFilter, layer::SubscriberExt, Layer};
6365
use tracing_indicatif::IndicatifLayer;
@@ -307,6 +309,8 @@ pub fn write_object(json: &str, format: Option<&OutputFormat>, include_separator
307309
}
308310
}
309311

312+
static TRACING_GUARD: LazyLock<RwLock<OnceCell<tracing::subscriber::DefaultGuard>>> = LazyLock::new(|| RwLock::new(OnceCell::new()));
313+
310314
#[allow(clippy::too_many_lines)]
311315
pub fn enable_tracing(trace_level_arg: Option<&TraceLevel>, trace_format_arg: Option<&TraceFormat>) {
312316

@@ -325,6 +329,7 @@ pub fn enable_tracing(trace_level_arg: Option<&TraceLevel>, trace_format_arg: Op
325329
.boxed();
326330
let default_subscriber = tracing_subscriber::Registry::default().with(default_fmt).with(default_filter).with(default_indicatif_layer);
327331
let default_guard = tracing::subscriber::set_default(default_subscriber);
332+
TRACING_GUARD.write().unwrap().set(default_guard).ok();
328333

329334
// read setting/policy from files
330335
if let Ok(v) = get_setting("tracing") {
@@ -422,7 +427,6 @@ pub fn enable_tracing(trace_level_arg: Option<&TraceLevel>, trace_format_arg: Op
422427

423428
let subscriber = tracing_subscriber::Registry::default().with(fmt).with(filter).with(indicatif_layer);
424429

425-
drop(default_guard);
426430
if tracing::subscriber::set_global_default(subscriber).is_err() {
427431
eprintln!("{}", t!("util.failedToSetTracing"));
428432
}
@@ -696,8 +700,9 @@ pub fn merge_parameters(file_params: &str, inline_params: &str) -> Result<String
696700
///
697701
/// * `code` - The exit code to use when exiting the process
698702
pub fn exit(code: i32) -> ! {
699-
// Small delay to ensure async writes complete
700-
std::thread::sleep(std::time::Duration::from_millis(50));
703+
// drop tracing guard to flush any pending trace logs before exiting
704+
let guard = TRACING_GUARD.write().unwrap().take();
705+
drop(guard);
701706

702707
// Force any pending writes to complete
703708
if let Err(e) = std::io::stderr().flush() {

0 commit comments

Comments
 (0)