From fa2c4ba3ab9e148df612f84ce7aa67856838c510 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sun, 15 Jan 2023 17:17:04 +0800 Subject: [PATCH] feat(encode/pattern): add debug and release formatters --- src/encode/pattern/mod.rs | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/encode/pattern/mod.rs b/src/encode/pattern/mod.rs index 61f236b5..2e018624 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!(release_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))) }