From 7a6b64c3f0792b45b86e5c7c8601a0b7570c0c72 Mon Sep 17 00:00:00 2001 From: rzvxa <3788964+rzvxa@users.noreply.github.com> Date: Sat, 7 Sep 2024 09:03:26 +0000 Subject: [PATCH] refactor(ast_tools): remove unsafe code from logger. (#5574) https://github.com/oxc-project/oxc/pull/5519#discussion_r1747097404 --- tasks/ast_tools/src/main.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tasks/ast_tools/src/main.rs b/tasks/ast_tools/src/main.rs index b3b1bc4a7ded1..a3661c4d6f3ef 100644 --- a/tasks/ast_tools/src/main.rs +++ b/tasks/ast_tools/src/main.rs @@ -60,8 +60,7 @@ fn main() -> std::result::Result<(), Box> { let cli_options = cli_options().run(); if cli_options.quiet { - // SAFETY: we haven't started using logger yet! - unsafe { logger::quiet() }; + logger::quiet().normalize_with("Failed to set logger to `quiet` mode.")?; } let AstCodegenResult { outputs, schema } = SOURCE_PATHS @@ -145,16 +144,16 @@ fn write_ci_filter( #[macro_use] mod logger { - static mut LOG: bool = true; + use std::sync::OnceLock; - /// Shouldn't be called after first use of the logger macros. - pub(super) unsafe fn quiet() { - LOG = false; + static LOG: OnceLock = OnceLock::new(); + + pub(super) fn quiet() -> Result<(), bool> { + LOG.set(false) } pub(super) fn __internal_log_enable() -> bool { - // SAFETY:`LOG` doesn't change from the moment we start using it. - unsafe { LOG } + *LOG.get_or_init(|| true) } macro_rules! log {