Skip to main content

pdf_oxide/pipeline/
logging.rs

1//! Logging utilities for the text extraction pipeline.
2//!
3//! This module provides convenient logging macros that respect the configured log level.
4//!
5//! When the `logging` feature is disabled, all logging is compiled out.
6//! When enabled, logging goes to stderr with formatted messages.
7
8/// Log an INFO level message.
9///
10/// Only logs when the configured log level is Info or more verbose.
11/// Compiled out entirely when `logging` feature is disabled.
12///
13/// # Examples
14///
15/// ```ignore
16/// extract_log_info!("Starting text extraction from 5 pages");
17/// extract_log_info!("Processing page {}/{}", 1, 5);
18/// ```
19#[macro_export]
20macro_rules! extract_log_info {
21    ($msg:expr) => {
22        #[cfg(feature = "logging")]
23        eprintln!("[INFO] {}", $msg);
24    };
25    ($fmt:expr, $($arg:tt)*) => {
26        #[cfg(feature = "logging")]
27        eprintln!("[INFO] {}", format!($fmt, $($arg)*));
28    };
29}
30
31/// Log a WARN level message.
32///
33/// Only logs when the configured log level is Warn or more verbose.
34/// Compiled out entirely when `logging` feature is disabled.
35///
36/// # Examples
37///
38/// ```ignore
39/// extract_log_warn!("Document contains unrecognized font");
40/// extract_log_warn!("Page {} has invalid encoding", page_num);
41/// ```
42#[macro_export]
43macro_rules! extract_log_warn {
44    ($msg:expr) => {
45        #[cfg(feature = "logging")]
46        eprintln!("[WARN] {}", $msg);
47    };
48    ($fmt:expr, $($arg:tt)*) => {
49        #[cfg(feature = "logging")]
50        eprintln!("[WARN] {}", format!($fmt, $($arg)*));
51    };
52}
53
54/// Log a DEBUG level message.
55///
56/// Only logs when the configured log level is Debug or more verbose.
57/// Compiled out entirely when `logging` feature is disabled.
58///
59/// # Examples
60///
61/// ```ignore
62/// extract_log_debug!("Processing page 1/5");
63/// extract_log_debug!("Detected document script: {:?}", script);
64/// ```
65#[macro_export]
66macro_rules! extract_log_debug {
67    ($msg:expr) => {
68        #[cfg(feature = "logging")]
69        eprintln!("[DEBUG] {}", $msg);
70    };
71    ($fmt:expr, $($arg:tt)*) => {
72        #[cfg(feature = "logging")]
73        eprintln!("[DEBUG] {}", format!($fmt, $($arg)*));
74    };
75}
76
77/// Log a TRACE level message.
78///
79/// Only logs when the configured log level is Trace.
80/// Compiled out entirely when `logging` feature is disabled.
81///
82/// Use this for very detailed information like character-level details.
83///
84/// # Examples
85///
86/// ```ignore
87/// extract_log_trace!("Detected 125 word boundaries on page 1");
88/// extract_log_trace!("TJ offset: {:?}, threshold: {}, boundary: {}",
89///     tj_offset, threshold, is_boundary);
90/// ```
91#[macro_export]
92macro_rules! extract_log_trace {
93    ($msg:expr) => {
94        #[cfg(feature = "logging")]
95        eprintln!("[TRACE] {}", $msg);
96    };
97    ($fmt:expr, $($arg:tt)*) => {
98        #[cfg(feature = "logging")]
99        eprintln!("[TRACE] {}", format!($fmt, $($arg)*));
100    };
101}
102
103/// Log an ERROR level message.
104///
105/// Always logs (independent of log level configuration).
106/// Compiled out entirely when `logging` feature is disabled.
107///
108/// # Examples
109///
110/// ```ignore
111/// extract_log_error!("Failed to extract text from page");
112/// extract_log_error!("Invalid PDF content: {}", error);
113/// ```
114#[macro_export]
115macro_rules! extract_log_error {
116    ($msg:expr) => {
117        #[cfg(feature = "logging")]
118        eprintln!("[ERROR] {}", $msg);
119    };
120    ($fmt:expr, $($arg:tt)*) => {
121        #[cfg(feature = "logging")]
122        eprintln!("[ERROR] {}", format!($fmt, $($arg)*));
123    };
124}
125
126// Re-export the macros for convenience
127pub use extract_log_debug;
128pub use extract_log_error;
129pub use extract_log_info;
130pub use extract_log_trace;
131pub use extract_log_warn;