1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) Imazen LLC and the JPEG XL Project Authors.
// Algorithms and constants derived from libjxl (BSD-3-Clause).
// Licensed under AGPL-3.0-or-later. Commercial licenses at https://www.imazen.io/pricing
//! Debug logging to file for the VarDCT encoder.
//!
//! When the `debug-tokens` feature is enabled, debug output goes to a file
//! instead of stderr, making it easy to grep without clobbering context.
//!
//! Usage:
//! ```ignore
//! debug_log!("DC token: ctx={}, value={}", ctx, value);
//! ```
//!
//! Output goes to `<temp_dir>/jxl_enc_debug.log` (overwritten each run).
#[cfg(feature = "debug-tokens")]
use std::io::Write;
#[cfg(feature = "debug-tokens")]
use std::sync::Mutex;
#[cfg(feature = "debug-tokens")]
static DEBUG_LOG: Mutex<Option<std::fs::File>> = Mutex::new(None);
/// Initialize the debug log file. Called automatically on first use.
#[cfg(feature = "debug-tokens")]
pub fn init_debug_log() {
let mut guard = DEBUG_LOG.lock().unwrap();
if guard.is_none() {
let file = std::fs::File::create(std::env::temp_dir().join("jxl_enc_debug.log"))
.expect("Failed to create debug log file");
*guard = Some(file);
}
}
/// Write a line to the debug log file.
#[cfg(feature = "debug-tokens")]
pub fn write_debug_log(msg: &str) {
init_debug_log();
let mut guard = DEBUG_LOG.lock().unwrap();
if let Some(ref mut file) = *guard {
let _ = writeln!(file, "{}", msg);
}
}
/// Flush the debug log file.
#[cfg(feature = "debug-tokens")]
#[allow(dead_code)]
pub fn flush_debug_log() {
let mut guard = DEBUG_LOG.lock().unwrap();
if let Some(ref mut file) = *guard {
let _ = file.flush();
}
}
/// Debug log macro - writes to `<temp_dir>/jxl_enc_debug.log` when debug-tokens feature is enabled.
///
/// Usage: `debug_log!("message: {}", value);`
#[macro_export]
macro_rules! debug_log {
($($arg:tt)*) => {
#[cfg(feature = "debug-tokens")]
{
$crate::vardct::debug_log::write_debug_log(&format!($($arg)*));
}
};
}
/// Debug log macro that also flushes (use sparingly, for important checkpoints).
#[macro_export]
macro_rules! debug_log_flush {
($($arg:tt)*) => {
#[cfg(feature = "debug-tokens")]
{
$crate::vardct::debug_log::write_debug_log(&format!($($arg)*));
$crate::vardct::debug_log::flush_debug_log();
}
};
}