Skip to main content

pixelflux/
log.rs

1//! The verbosity switch behind every backend's tagged line.
2//!
3//! What an operator reads back from a user is printed plainly: one line per
4//! capture start, encoder choice, zero-copy or readback decision, and fallback,
5//! tagged `[X11]`, `[Wayland]`, `[HostCapture]`, or `[pixelflux]` on stdout,
6//! with warnings and errors on stderr. The mechanics behind those lines (each
7//! dmabuf import, the CUDA device enumeration, the per-second rate counters,
8//! an in-place reconfigure) go through `debug!` and print only once the
9//! switch is on: a capture whose `CaptureSettings.debug_logging` is set turns
10//! it on for the process (selkies passes its own `--debug` there), and
11//! `PIXELFLUX_DEBUG=1` in the environment does the same for standalone use.
12
13use std::sync::OnceLock;
14use std::sync::atomic::{AtomicBool, Ordering};
15
16static DEBUG: AtomicBool = AtomicBool::new(false);
17static ENV_DEBUG: OnceLock<bool> = OnceLock::new();
18
19/// Turn the debug lines on or off for the process.
20pub fn set_debug(on: bool) {
21    DEBUG.store(on, Ordering::Relaxed);
22}
23
24/// Whether debug lines print: the last capture's setting, or `PIXELFLUX_DEBUG`.
25pub fn debug_enabled() -> bool {
26    DEBUG.load(Ordering::Relaxed)
27        || *ENV_DEBUG.get_or_init(|| {
28            std::env::var_os("PIXELFLUX_DEBUG").is_some_and(|v| !v.is_empty() && v != "0")
29        })
30}
31
32/// A `println!` that prints only while `debug_enabled`.
33macro_rules! debug {
34    ($($arg:tt)*) => {
35        if $crate::log::debug_enabled() {
36            println!($($arg)*);
37        }
38    };
39}
40pub(crate) use debug;