From eba7f9f5c082eb9c2faec3309f16dd49e0a9d3b1 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sun, 3 Dec 2023 14:37:47 +0800 Subject: [PATCH] feat(encode/pattern): add debug and release formatters --- src/encode/pattern/mod.rs | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/encode/pattern/mod.rs b/src/encode/pattern/mod.rs index 9316ecc8..74aeb579 100644 --- a/src/encode/pattern/mod.rs +++ b/src/encode/pattern/mod.rs @@ -53,6 +53,8 @@ //! the default style for all other levels. //! * `{h(the level is {l})}` - //! the level is ERROR +//! * `D`, `debug` - Outputs its arguments ONLY in debug build. +//! * `R`, `release` - Outputs its arguments ONLY in release build. //! * `l`, `level` - The log level. //! * `L`, `line` - The line that the log message came from, or `???` if not //! provided. @@ -449,6 +451,40 @@ impl<'a> From> for Chunk { params: parameters, } } + "D" | "debug" => { + if formatter.args.len() != 1 { + return Chunk::Error("expected exactly one argument".to_owned()); + } + + let chunks = formatter + .args + .pop() + .unwrap() + .into_iter() + .map(From::from) + .collect(); + Chunk::Formatted { + chunk: FormattedChunk::Debug(chunks), + params: parameters, + } + } + "R" | "release" => { + if formatter.args.len() != 1 { + return Chunk::Error("expected exactly one argument".to_owned()); + } + + let chunks = formatter + .args + .pop() + .unwrap() + .into_iter() + .map(From::from) + .collect(); + Chunk::Formatted { + chunk: FormattedChunk::Release(chunks), + params: parameters, + } + } "l" | "level" => no_args(&formatter.args, parameters, FormattedChunk::Level), "m" | "message" => no_args(&formatter.args, parameters, FormattedChunk::Message), "M" | "module" => no_args(&formatter.args, parameters, FormattedChunk::Module), @@ -554,6 +590,8 @@ enum FormattedChunk { Newline, Align(Vec), Highlight(Vec), + Debug(Vec), + Release(Vec), Mdc(String, String), } @@ -609,6 +647,22 @@ impl FormattedChunk { } Ok(()) } + FormattedChunk::Debug(ref chunks) => { + if cfg!(debug_assertions) { + for chunk in chunks { + chunk.encode(w, record)?; + } + } + Ok(()) + } + FormattedChunk::Release(ref chunks) => { + if !cfg!(debug_assertions) { + for chunk in chunks { + chunk.encode(w, record)?; + } + } + Ok(()) + } FormattedChunk::Mdc(ref key, ref default) => { log_mdc::get(key, |v| write!(w, "{}", v.unwrap_or(default))) } @@ -976,4 +1030,29 @@ mod tests { assert_eq!(buf, b"missing value"); } + + #[test] + #[cfg(feature = "simple_writer")] + fn debug_release() { + let debug_pat = "{D({l})}"; + let release_pat = "{R({l})}"; + + let debug_encoder = PatternEncoder::new(debug_pat); + let release_encoder = PatternEncoder::new(release_pat); + let mut debug_buf = vec![]; + let mut release_buf = vec![]; + + debug_encoder.encode(&mut SimpleWriter(&mut debug_buf), &Record::builder().level(Level::Info).build()) + .unwrap(); + release_encoder.encode(&mut SimpleWriter(&mut release_buf), &Record::builder().level(Level::Info).build()) + .unwrap(); + + if cfg!(debug_assertions) { + assert_eq!(debug_buf, b"INFO"); + assert!(release_buf.is_empty()); + } else { + assert_eq!(release_buf, b"INFO"); + assert!(debug_buf.is_empty()); + } + } }