Skip to content

Commit

Permalink
Make logging optional via 'log' feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed May 4, 2024
1 parent 541df39 commit b7c58fc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }
Expand Down
10 changes: 5 additions & 5 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ impl<'r> StreamBuffer<'r> {
boundary: &str,
field_name: Option<&str>,
) -> crate::Result<Option<(bool, Bytes)>> {
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()),
});
Expand All @@ -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.
Expand All @@ -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()),
})
Expand All @@ -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) => {
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b7c58fc

Please sign in to comment.