From 77a16cda4e403d2db6cf6fa6ceeeee32ed65cf3d Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 19 Nov 2025 16:15:57 +0000 Subject: [PATCH 1/7] test: set debug flag automatically at crate level via static AtomicBool --- syncdoc-migrate/src/debug.rs | 21 +++++++++++++++++++++ syncdoc-migrate/src/lib.rs | 7 ++++++- syncdoc-migrate/src/tests/mod.rs | 9 +++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 syncdoc-migrate/src/debug.rs diff --git a/syncdoc-migrate/src/debug.rs b/syncdoc-migrate/src/debug.rs new file mode 100644 index 0000000..5964c98 --- /dev/null +++ b/syncdoc-migrate/src/debug.rs @@ -0,0 +1,21 @@ +use std::env; +use std::sync::atomic::{AtomicBool, Ordering}; + +/// Atomic flag indicating whether debug output is enabled. +/// Initialised once from the `SYNCDOC_DEBUG` environment variable. +static DEBUG_ENABLED: AtomicBool = AtomicBool::new(env::var("SYNCDOC_DEBUG").is_ok()); + +/// Enable or disable debug output programmatically. +/// +/// Useful in tests or runtime scenarios where you want to toggle debug +/// output without using environment variables. +pub fn set_debug(enabled: bool) { + DEBUG_ENABLED.store(enabled, Ordering::Relaxed); +} + +/// Query whether debug output is currently enabled. +/// +/// Returns `true` if debugging is enabled, `false` otherwise. +pub fn is_enabled() -> bool { + DEBUG_ENABLED.load(Ordering::Relaxed) +} diff --git a/syncdoc-migrate/src/lib.rs b/syncdoc-migrate/src/lib.rs index 1aabe47..5ec77b5 100644 --- a/syncdoc-migrate/src/lib.rs +++ b/syncdoc-migrate/src/lib.rs @@ -1,6 +1,7 @@ // syncdoc-migrate/src/lib.rs pub mod config; +pub mod debug; pub mod discover; mod extract; mod report; @@ -17,10 +18,14 @@ pub use write::{ extract_all_docs, find_expected_doc_paths, write_extractions, DocExtraction, WriteReport, }; +/// Macro for debug output in syncdoc. +/// +/// Prints to stderr only if debug output is enabled via the atomic flag (tests do this using ctor) +/// or the `SYNCDOC_DEBUG` environment variable at startup. #[macro_export] macro_rules! syncdoc_debug { ($($arg:tt)*) => { - if std::env::var("SYNCDOC_DEBUG").is_ok() { + if $crate::debug::is_enabled() { eprintln!("[SYNCDOC DEBUG] {}", format!($($arg)*)); } }; diff --git a/syncdoc-migrate/src/tests/mod.rs b/syncdoc-migrate/src/tests/mod.rs index d85f644..58eb1ad 100644 --- a/syncdoc-migrate/src/tests/mod.rs +++ b/syncdoc-migrate/src/tests/mod.rs @@ -9,3 +9,12 @@ mod restore; mod rewrite; mod strip; mod write; + +use crate::debug; +use ctor::ctor; + +#[ctor] +fn init_debug() { + // Enable debug output for all tests automatically + debug::set_debug(true); +} From ce21638fbb6378eeff8c646a376829cba10b02b7 Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 19 Nov 2025 16:37:01 +0000 Subject: [PATCH 2/7] fix: must not initialise via env --- syncdoc-migrate/src/debug.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/syncdoc-migrate/src/debug.rs b/syncdoc-migrate/src/debug.rs index 5964c98..72c7666 100644 --- a/syncdoc-migrate/src/debug.rs +++ b/syncdoc-migrate/src/debug.rs @@ -1,21 +1,30 @@ +//! Debug printer control for syncdoc. +//! +//! Provides a thread-safe atomic flag for debug logging via STDERR and a function +//! to enable it programmatically (used in tests). + use std::env; use std::sync::atomic::{AtomicBool, Ordering}; /// Atomic flag indicating whether debug output is enabled. -/// Initialised once from the `SYNCDOC_DEBUG` environment variable. -static DEBUG_ENABLED: AtomicBool = AtomicBool::new(env::var("SYNCDOC_DEBUG").is_ok()); +/// Initialised at runtime from `SYNCDOC_DEBUG`. +static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false); + +/// Initialise the debug atomic from the environment variable. +/// Typically called at program start, or via ctor in tests. +pub fn init_from_env() { + if env::var("SYNCDOC_DEBUG").is_ok() { + DEBUG_ENABLED.store(true, Ordering::Relaxed); + } +} /// Enable or disable debug output programmatically. -/// -/// Useful in tests or runtime scenarios where you want to toggle debug -/// output without using environment variables. +/// Tests can call this directly, or you can wire it via a ctor. pub fn set_debug(enabled: bool) { DEBUG_ENABLED.store(enabled, Ordering::Relaxed); } -/// Query whether debug output is currently enabled. -/// -/// Returns `true` if debugging is enabled, `false` otherwise. +/// Check whether debug output is enabled. pub fn is_enabled() -> bool { DEBUG_ENABLED.load(Ordering::Relaxed) } From e9e2f06aa1e7948bc2db7a25d1d97749a05a85ab Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 19 Nov 2025 16:49:44 +0000 Subject: [PATCH 3/7] test: working migrate crate debug switch --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ syncdoc-migrate/Cargo.toml | 1 + syncdoc-migrate/src/tests/mod.rs | 5 ++--- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0550ccf..bdf9436 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,6 +90,22 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "ctor" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ffc71fcdcdb40d6f087edddf7f8f1f8f79e6cf922f555a9ee8779752d4819bd" +dependencies = [ + "ctor-proc-macro", + "dtor", +] + +[[package]] +name = "ctor-proc-macro" +version = "0.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52560adf09603e58c9a7ee1fe1dcb95a16927b17c127f0ac02d6e768a0e25bc1" + [[package]] name = "demo_cfg_attr_call" version = "0.4.0" @@ -166,6 +182,21 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" +[[package]] +name = "dtor" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "404d02eeb088a82cfd873006cb713fe411306c7d182c344905e101fb1167d301" +dependencies = [ + "dtor-proc-macro", +] + +[[package]] +name = "dtor-proc-macro" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f678cf4a922c215c63e0de95eb1ff08a958a81d47e485cf9da1e27bf6305cfa5" + [[package]] name = "duct" version = "1.1.1" @@ -684,6 +715,7 @@ name = "syncdoc-migrate" version = "0.4.0" dependencies = [ "braces", + "ctor", "duct", "imara-diff", "insta", diff --git a/syncdoc-migrate/Cargo.toml b/syncdoc-migrate/Cargo.toml index 5d9e298..cf09867 100644 --- a/syncdoc-migrate/Cargo.toml +++ b/syncdoc-migrate/Cargo.toml @@ -22,6 +22,7 @@ unsynn.workspace = true [dev-dependencies] braces = "0.2.1" +ctor = "0.6.1" insta.workspace = true rust-format.workspace = true tempfile.workspace = true diff --git a/syncdoc-migrate/src/tests/mod.rs b/syncdoc-migrate/src/tests/mod.rs index 58eb1ad..51ef6f5 100644 --- a/syncdoc-migrate/src/tests/mod.rs +++ b/syncdoc-migrate/src/tests/mod.rs @@ -11,10 +11,9 @@ mod strip; mod write; use crate::debug; -use ctor::ctor; -#[ctor] +/// Automatically enable debug output for all tests +#[ctor::ctor] fn init_debug() { - // Enable debug output for all tests automatically debug::set_debug(true); } From e42c8b3fea95ab88570bdb225c75819bf92508cc Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 19 Nov 2025 17:59:07 +0000 Subject: [PATCH 4/7] chore(migrate): set up ctor test debugging --- Cargo.lock | 1 + syncdoc-core/Cargo.toml | 1 + syncdoc-core/src/debug.rs | 30 ++++++++++++++++++++++++++++++ syncdoc-core/src/lib.rs | 14 +++++++++++++- syncdoc-core/tests/helpers.rs | 5 +++++ syncdoc-migrate/tests/helpers.rs | 5 +++++ syncdoc/src/debug.rs | 30 ++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 syncdoc-core/src/debug.rs create mode 100644 syncdoc/src/debug.rs diff --git a/Cargo.lock b/Cargo.lock index bdf9436..bc9ea69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -697,6 +697,7 @@ name = "syncdoc-core" version = "0.4.0" dependencies = [ "braces", + "ctor", "duct", "insta", "itertools", diff --git a/syncdoc-core/Cargo.toml b/syncdoc-core/Cargo.toml index c834c1b..b4651b1 100644 --- a/syncdoc-core/Cargo.toml +++ b/syncdoc-core/Cargo.toml @@ -23,6 +23,7 @@ unsynn.workspace = true [dev-dependencies] braces = "0.2.6" +ctor = "0.6.1" duct = "1.1.1" insta.workspace = true itertools = "0.14.0" diff --git a/syncdoc-core/src/debug.rs b/syncdoc-core/src/debug.rs new file mode 100644 index 0000000..72c7666 --- /dev/null +++ b/syncdoc-core/src/debug.rs @@ -0,0 +1,30 @@ +//! Debug printer control for syncdoc. +//! +//! Provides a thread-safe atomic flag for debug logging via STDERR and a function +//! to enable it programmatically (used in tests). + +use std::env; +use std::sync::atomic::{AtomicBool, Ordering}; + +/// Atomic flag indicating whether debug output is enabled. +/// Initialised at runtime from `SYNCDOC_DEBUG`. +static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false); + +/// Initialise the debug atomic from the environment variable. +/// Typically called at program start, or via ctor in tests. +pub fn init_from_env() { + if env::var("SYNCDOC_DEBUG").is_ok() { + DEBUG_ENABLED.store(true, Ordering::Relaxed); + } +} + +/// Enable or disable debug output programmatically. +/// Tests can call this directly, or you can wire it via a ctor. +pub fn set_debug(enabled: bool) { + DEBUG_ENABLED.store(enabled, Ordering::Relaxed); +} + +/// Check whether debug output is enabled. +pub fn is_enabled() -> bool { + DEBUG_ENABLED.load(Ordering::Relaxed) +} diff --git a/syncdoc-core/src/lib.rs b/syncdoc-core/src/lib.rs index 78bed32..f8ab4ea 100644 --- a/syncdoc-core/src/lib.rs +++ b/syncdoc-core/src/lib.rs @@ -1,5 +1,6 @@ /// syncdoc-core: documentation injection helper macros pub mod config; +pub mod debug; mod doc_injector; mod omnibus; pub mod parse; @@ -9,11 +10,22 @@ pub mod token_processors; pub use doc_injector::{module_doc_impl, omnidoc_impl}; pub use omnibus::inject_all_docs_impl; +/// Macro for debug output in syncdoc. +/// +/// Prints to stderr only if debug output is enabled via the atomic flag (tests do this using ctor) +/// or the `SYNCDOC_DEBUG` environment variable at startup. #[macro_export] macro_rules! syncdoc_debug { ($($arg:tt)*) => { - if std::env::var("SYNCDOC_DEBUG").is_ok() { + if $crate::debug::is_enabled() { eprintln!("[SYNCDOC DEBUG] {}", format!($($arg)*)); } }; } + +/// Automatically enable debug output for all tests +#[cfg(test)] +#[ctor::ctor] +fn init_debug() { + crate::debug::set_debug(true); +} diff --git a/syncdoc-core/tests/helpers.rs b/syncdoc-core/tests/helpers.rs index 2ac0b06..3d0f32b 100644 --- a/syncdoc-core/tests/helpers.rs +++ b/syncdoc-core/tests/helpers.rs @@ -11,6 +11,11 @@ use std::path::PathBuf; use std::process::Command; use tempfile::TempDir; +#[ctor::ctor] +fn init_debug() { + syncdoc_core::debug::set_debug(true); +} + pub struct TestCrate { _temp_dir: TempDir, root: PathBuf, diff --git a/syncdoc-migrate/tests/helpers.rs b/syncdoc-migrate/tests/helpers.rs index 533572e..18c8dd6 100644 --- a/syncdoc-migrate/tests/helpers.rs +++ b/syncdoc-migrate/tests/helpers.rs @@ -10,6 +10,11 @@ use syncdoc_migrate::{ }; use tempfile::TempDir; +#[ctor::ctor] +fn init_debug() { + syncdoc_migrate::debug::set_debug(true); +} + pub fn setup_test_file(source: &str, filename: &str) -> (TempDir, PathBuf) { let temp_dir = TempDir::new().unwrap(); let file_path = temp_dir.path().join(filename); diff --git a/syncdoc/src/debug.rs b/syncdoc/src/debug.rs new file mode 100644 index 0000000..72c7666 --- /dev/null +++ b/syncdoc/src/debug.rs @@ -0,0 +1,30 @@ +//! Debug printer control for syncdoc. +//! +//! Provides a thread-safe atomic flag for debug logging via STDERR and a function +//! to enable it programmatically (used in tests). + +use std::env; +use std::sync::atomic::{AtomicBool, Ordering}; + +/// Atomic flag indicating whether debug output is enabled. +/// Initialised at runtime from `SYNCDOC_DEBUG`. +static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false); + +/// Initialise the debug atomic from the environment variable. +/// Typically called at program start, or via ctor in tests. +pub fn init_from_env() { + if env::var("SYNCDOC_DEBUG").is_ok() { + DEBUG_ENABLED.store(true, Ordering::Relaxed); + } +} + +/// Enable or disable debug output programmatically. +/// Tests can call this directly, or you can wire it via a ctor. +pub fn set_debug(enabled: bool) { + DEBUG_ENABLED.store(enabled, Ordering::Relaxed); +} + +/// Check whether debug output is enabled. +pub fn is_enabled() -> bool { + DEBUG_ENABLED.load(Ordering::Relaxed) +} From ea1447566cfb4822df235485fe96772523b90940 Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 19 Nov 2025 18:17:06 +0000 Subject: [PATCH 5/7] refactor: shared atomic --- syncdoc-migrate/src/debug.rs | 30 ------------------------------ syncdoc-migrate/src/lib.rs | 17 +++-------------- syncdoc-migrate/src/tests/mod.rs | 4 +--- syncdoc-migrate/tests/helpers.rs | 2 +- 4 files changed, 5 insertions(+), 48 deletions(-) delete mode 100644 syncdoc-migrate/src/debug.rs diff --git a/syncdoc-migrate/src/debug.rs b/syncdoc-migrate/src/debug.rs deleted file mode 100644 index 72c7666..0000000 --- a/syncdoc-migrate/src/debug.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! Debug printer control for syncdoc. -//! -//! Provides a thread-safe atomic flag for debug logging via STDERR and a function -//! to enable it programmatically (used in tests). - -use std::env; -use std::sync::atomic::{AtomicBool, Ordering}; - -/// Atomic flag indicating whether debug output is enabled. -/// Initialised at runtime from `SYNCDOC_DEBUG`. -static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false); - -/// Initialise the debug atomic from the environment variable. -/// Typically called at program start, or via ctor in tests. -pub fn init_from_env() { - if env::var("SYNCDOC_DEBUG").is_ok() { - DEBUG_ENABLED.store(true, Ordering::Relaxed); - } -} - -/// Enable or disable debug output programmatically. -/// Tests can call this directly, or you can wire it via a ctor. -pub fn set_debug(enabled: bool) { - DEBUG_ENABLED.store(enabled, Ordering::Relaxed); -} - -/// Check whether debug output is enabled. -pub fn is_enabled() -> bool { - DEBUG_ENABLED.load(Ordering::Relaxed) -} diff --git a/syncdoc-migrate/src/lib.rs b/syncdoc-migrate/src/lib.rs index 5ec77b5..433936b 100644 --- a/syncdoc-migrate/src/lib.rs +++ b/syncdoc-migrate/src/lib.rs @@ -1,7 +1,6 @@ // syncdoc-migrate/src/lib.rs pub mod config; -pub mod debug; pub mod discover; mod extract; mod report; @@ -9,6 +8,9 @@ pub mod restore; pub mod rewrite; pub mod write; +// Re-export core's macro +pub use syncdoc_core::syncdoc_debug; + pub use config::DocsPathMode; pub use discover::{discover_rust_files, get_or_create_docs_path, parse_file, ParsedFile}; pub use extract::{extract_doc_content, has_doc_attrs}; @@ -18,18 +20,5 @@ pub use write::{ extract_all_docs, find_expected_doc_paths, write_extractions, DocExtraction, WriteReport, }; -/// Macro for debug output in syncdoc. -/// -/// Prints to stderr only if debug output is enabled via the atomic flag (tests do this using ctor) -/// or the `SYNCDOC_DEBUG` environment variable at startup. -#[macro_export] -macro_rules! syncdoc_debug { - ($($arg:tt)*) => { - if $crate::debug::is_enabled() { - eprintln!("[SYNCDOC DEBUG] {}", format!($($arg)*)); - } - }; -} - #[cfg(test)] mod tests; diff --git a/syncdoc-migrate/src/tests/mod.rs b/syncdoc-migrate/src/tests/mod.rs index 51ef6f5..de17885 100644 --- a/syncdoc-migrate/src/tests/mod.rs +++ b/syncdoc-migrate/src/tests/mod.rs @@ -10,10 +10,8 @@ mod rewrite; mod strip; mod write; -use crate::debug; - /// Automatically enable debug output for all tests #[ctor::ctor] fn init_debug() { - debug::set_debug(true); + syncdoc_core::debug::set_debug(true); } diff --git a/syncdoc-migrate/tests/helpers.rs b/syncdoc-migrate/tests/helpers.rs index 18c8dd6..f8bf53a 100644 --- a/syncdoc-migrate/tests/helpers.rs +++ b/syncdoc-migrate/tests/helpers.rs @@ -12,7 +12,7 @@ use tempfile::TempDir; #[ctor::ctor] fn init_debug() { - syncdoc_migrate::debug::set_debug(true); + syncdoc_core::debug::set_debug(true); } pub fn setup_test_file(source: &str, filename: &str) -> (TempDir, PathBuf) { From edbf540f1d4ac9be4d73184ad1c45d66a8540448 Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 19 Nov 2025 18:29:02 +0000 Subject: [PATCH 6/7] chore(syncdoc): init debug in helpers [main crate tests] --- Cargo.lock | 1 + syncdoc/Cargo.toml | 1 + syncdoc/tests/cli_integration.rs | 5 +++++ syncdoc/tests/roundtrip/helpers.rs | 5 +++++ 4 files changed, 12 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index bc9ea69..919e763 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -681,6 +681,7 @@ version = "0.4.0" dependencies = [ "assert_cmd", "braces", + "ctor", "facet", "facet-args", "insta", diff --git a/syncdoc/Cargo.toml b/syncdoc/Cargo.toml index 96fb3d3..d16c7f0 100644 --- a/syncdoc/Cargo.toml +++ b/syncdoc/Cargo.toml @@ -47,6 +47,7 @@ syncdoc-migrate = { optional = true, workspace = true } [dev-dependencies] assert_cmd.workspace = true braces = "0.2.6" +ctor = "0.6.1" insta.workspace = true regex = "1.12.2" rust-format.workspace = true diff --git a/syncdoc/tests/cli_integration.rs b/syncdoc/tests/cli_integration.rs index 22381c5..b170455 100644 --- a/syncdoc/tests/cli_integration.rs +++ b/syncdoc/tests/cli_integration.rs @@ -6,6 +6,11 @@ use std::fs; use std::path::Path; use tempfile::TempDir; +#[ctor::ctor] +fn init_debug() { + syncdoc_core::debug::set_debug(true); +} + fn to_braces(paths: &[&str]) -> String { let braces_config = BraceConfig::default(); brace_paths(paths, &braces_config).expect("Brace error") diff --git a/syncdoc/tests/roundtrip/helpers.rs b/syncdoc/tests/roundtrip/helpers.rs index 628b0f7..6bb4510 100644 --- a/syncdoc/tests/roundtrip/helpers.rs +++ b/syncdoc/tests/roundtrip/helpers.rs @@ -7,6 +7,11 @@ use std::path::{Path, PathBuf}; use std::process::Command; use tempfile::TempDir; +#[ctor::ctor] +fn init_debug() { + syncdoc_core::debug::set_debug(true); +} + /// Module configuration for test setup #[derive(Debug, Clone)] pub struct ModuleConfig { From 35301261d8049bd3e0bdbb251539666eec24bb1a Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 19 Nov 2025 19:17:23 +0000 Subject: [PATCH 7/7] feat: debug default to cfg(test) if env var unset --- syncdoc-core/src/debug.rs | 32 ++++++++++++++++++++++++-------- syncdoc-core/src/lib.rs | 7 ------- syncdoc-core/tests/helpers.rs | 2 +- syncdoc/src/debug.rs | 30 ------------------------------ 4 files changed, 25 insertions(+), 46 deletions(-) delete mode 100644 syncdoc/src/debug.rs diff --git a/syncdoc-core/src/debug.rs b/syncdoc-core/src/debug.rs index 72c7666..6511d6f 100644 --- a/syncdoc-core/src/debug.rs +++ b/syncdoc-core/src/debug.rs @@ -1,25 +1,34 @@ //! Debug printer control for syncdoc. //! //! Provides a thread-safe atomic flag for debug logging via STDERR and a function -//! to enable it programmatically (used in tests). +//! to enable it programmatically (runs automatically if compiled in `cfg(test)`). use std::env; use std::sync::atomic::{AtomicBool, Ordering}; /// Atomic flag indicating whether debug output is enabled. -/// Initialised at runtime from `SYNCDOC_DEBUG`. static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false); -/// Initialise the debug atomic from the environment variable. -/// Typically called at program start, or via ctor in tests. +/// Initialise the debug atomic from the `SYNCDOC_DEBUG` environment variable. +/// +/// - Treats `"0"`, `"false"`, `"no"`, `"off"` as false. +/// - Any other value is true. +/// - If the variable is unset, defaults to true for tests, false otherwise. pub fn init_from_env() { - if env::var("SYNCDOC_DEBUG").is_ok() { - DEBUG_ENABLED.store(true, Ordering::Relaxed); - } + let enabled = match env::var("SYNCDOC_DEBUG") { + Ok(val) => { + let val = val.trim(); + !(val == "0" + || val.eq_ignore_ascii_case("false") + || val.eq_ignore_ascii_case("no") + || val.eq_ignore_ascii_case("off")) + } + Err(_) => cfg!(test), + }; + set_debug(enabled); } /// Enable or disable debug output programmatically. -/// Tests can call this directly, or you can wire it via a ctor. pub fn set_debug(enabled: bool) { DEBUG_ENABLED.store(enabled, Ordering::Relaxed); } @@ -28,3 +37,10 @@ pub fn set_debug(enabled: bool) { pub fn is_enabled() -> bool { DEBUG_ENABLED.load(Ordering::Relaxed) } + +/// Automatically enable debug output for tests, respecting the env var. +#[cfg(test)] +#[ctor::ctor] +fn init_debug_for_tests() { + init_from_env(); +} diff --git a/syncdoc-core/src/lib.rs b/syncdoc-core/src/lib.rs index f8ab4ea..58d3c2a 100644 --- a/syncdoc-core/src/lib.rs +++ b/syncdoc-core/src/lib.rs @@ -22,10 +22,3 @@ macro_rules! syncdoc_debug { } }; } - -/// Automatically enable debug output for all tests -#[cfg(test)] -#[ctor::ctor] -fn init_debug() { - crate::debug::set_debug(true); -} diff --git a/syncdoc-core/tests/helpers.rs b/syncdoc-core/tests/helpers.rs index 3d0f32b..0fa3092 100644 --- a/syncdoc-core/tests/helpers.rs +++ b/syncdoc-core/tests/helpers.rs @@ -13,7 +13,7 @@ use tempfile::TempDir; #[ctor::ctor] fn init_debug() { - syncdoc_core::debug::set_debug(true); + syncdoc_core::debug::init_from_env(); } pub struct TestCrate { diff --git a/syncdoc/src/debug.rs b/syncdoc/src/debug.rs deleted file mode 100644 index 72c7666..0000000 --- a/syncdoc/src/debug.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! Debug printer control for syncdoc. -//! -//! Provides a thread-safe atomic flag for debug logging via STDERR and a function -//! to enable it programmatically (used in tests). - -use std::env; -use std::sync::atomic::{AtomicBool, Ordering}; - -/// Atomic flag indicating whether debug output is enabled. -/// Initialised at runtime from `SYNCDOC_DEBUG`. -static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false); - -/// Initialise the debug atomic from the environment variable. -/// Typically called at program start, or via ctor in tests. -pub fn init_from_env() { - if env::var("SYNCDOC_DEBUG").is_ok() { - DEBUG_ENABLED.store(true, Ordering::Relaxed); - } -} - -/// Enable or disable debug output programmatically. -/// Tests can call this directly, or you can wire it via a ctor. -pub fn set_debug(enabled: bool) { - DEBUG_ENABLED.store(enabled, Ordering::Relaxed); -} - -/// Check whether debug output is enabled. -pub fn is_enabled() -> bool { - DEBUG_ENABLED.load(Ordering::Relaxed) -}