Skip to content

Commit ae1e5aa

Browse files
committed
trace_log is disabled by default
1 parent edd4cb1 commit ae1e5aa

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

fuzz/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/testutils.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,30 @@
44
//! this module.
55
use crate::Reader;
66
use 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

815
thread_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
2038
pub 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);

tests/testutils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use html5gum::testutils::OUTPUT;
1919
/// log buffer -- it is bound in size and compiled out in release mode. Code can use the
2020
/// `crate::trace_log` macro to write lines to it.
2121
pub fn catch_unwind_and_report(f: impl FnOnce() + UnwindSafe) -> Result<(), Failed> {
22+
html5gum::testutils::enable_trace_log();
23+
2224
static PANIC_HOOK: Once = Once::new();
2325
PANIC_HOOK.call_once(|| {
2426
panic::set_hook(Box::new(|_info| {

0 commit comments

Comments
 (0)