From b7c58fc2f679d2627293a84eeb6749ac94f6f9ab Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Fri, 3 May 2024 18:40:06 -0700 Subject: [PATCH] Make logging optional via 'log' feature. --- Cargo.toml | 3 ++- src/buffer.rs | 10 +++++----- src/lib.rs | 12 ++++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8dbdbe1..5ae097f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,9 +22,9 @@ default = [] all = ["json"] json = ["serde", "serde_json"] tokio-io = ["tokio", "tokio-util"] +log = ["dep:log"] [dependencies] -log = "0.4.15" bytes = "1.0" futures-util = { version = "0.3", default-features = false } memchr = "2.4" @@ -34,6 +34,7 @@ mime = "0.3.10" encoding_rs = "0.8.20" spin = { version = "0.9", default-features = false, features = ["spin_mutex"] } +log = { version = "0.4.15", optional = true } serde = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true } tokio = { version = "1.0", features = [], optional = true } diff --git a/src/buffer.rs b/src/buffer.rs index 23f863d..cd61357 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -95,9 +95,9 @@ impl<'r> StreamBuffer<'r> { boundary: &str, field_name: Option<&str>, ) -> crate::Result> { - log::trace!("finding next field: {:?}", field_name); + trace!("finding next field: {:?}", field_name); if self.buf.is_empty() && self.eof { - log::trace!("empty buffer && EOF"); + trace!("empty buffer && EOF"); return Err(crate::Error::IncompleteFieldData { field_name: field_name.map(|s| s.to_owned()), }); @@ -110,7 +110,7 @@ impl<'r> StreamBuffer<'r> { match memchr::memmem::find(&self.buf, boundary_deriv.as_bytes()) { Some(idx) => { - log::trace!("new field found at {}", idx); + trace!("new field found at {}", idx); let bytes = self.buf.split_to(idx).freeze(); // discard \r\n. @@ -119,7 +119,7 @@ impl<'r> StreamBuffer<'r> { Ok(Some((true, bytes))) } None if self.eof => { - log::trace!("no new field found: EOF. terminating"); + trace!("no new field found: EOF. terminating"); Err(crate::Error::IncompleteFieldData { field_name: field_name.map(|s| s.to_owned()), }) @@ -133,7 +133,7 @@ impl<'r> StreamBuffer<'r> { 0 }; - log::trace!("no new field found, not EOF, checking close"); + trace!("no new field found, not EOF, checking close"); let bytes = &self.buf[rem_boundary_part_idx..]; match memchr::memmem::rfind(bytes, constants::CR.as_bytes()) { Some(rel_idx) => { diff --git a/src/lib.rs b/src/lib.rs index 09b78ca..d480221 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,8 @@ //! `AsyncRead` reader as a source, so that it can be plugged into any async //! Rust environment e.g. any async server. //! +//! To enable trace logging via the `log` crate, enable the `log` feature. +//! //! # Examples //! //! ```no_run @@ -125,6 +127,16 @@ pub use field::Field; pub use multipart::Multipart; pub use size_limit::SizeLimit; +#[cfg(feature = "log")] +macro_rules! trace { + ($($t:tt)*) => (::log::trace!($($t)*);); +} + +#[cfg(not(feature = "log"))] +macro_rules! trace { + ($($t:tt)*) => {}; +} + mod buffer; mod constants; mod constraints;