44//! this module.
55use crate :: Reader ;
66use std:: cell:: Cell ;
7+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
8+
9+ /// Global flag to enable trace logging. When false, trace_log is a no-op.
10+ ///
11+ /// During fuzzing, we want to run with cfg(debug_assertions) yet at the same time not run
12+ /// trace_log to keep target stability (in AFL terms). Adding more cargo featureflags is unwieldy.
13+ static TRACE_LOG_ENABLED : AtomicBool = AtomicBool :: new ( false ) ;
714
815thread_local ! {
916 /// Buffer of all debugging output logged internally by html5gum.
1017 pub static OUTPUT : Cell <String > = Cell :: default ( ) ;
1118}
1219
20+ /// Enable trace logging for the current process.
21+ /// This should be called at the start of any test that needs to capture trace logs.
22+ pub fn enable_trace_log ( ) {
23+ TRACE_LOG_ENABLED . store ( true , Ordering :: Relaxed ) ;
24+ }
25+
26+ /// Disable trace logging for the current process.
27+ pub fn disable_trace_log ( ) {
28+ TRACE_LOG_ENABLED . store ( false , Ordering :: Relaxed ) ;
29+ }
30+
1331/// Simple debug logger for tests.
1432///
1533/// The test harness used by `tests/html5lib_tokenizer.rs` cannot capture stdout, see [libtest-mimic
@@ -18,6 +36,10 @@ thread_local! {
1836///
1937/// A noop version for non-test builds is implemented in src/lib.rs
2038pub fn trace_log ( msg : & str ) {
39+ if !TRACE_LOG_ENABLED . load ( Ordering :: Relaxed ) {
40+ return ;
41+ }
42+
2143 OUTPUT . with ( |cell| {
2244 let mut buf = cell. take ( ) ;
2345 buf. push_str ( msg) ;
0 commit comments