Skip to content

Commit

Permalink
feat(encode/pattern): add debug and release formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC committed Dec 3, 2023
1 parent 5d9ae3a commit eba7f9f
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/encode/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
//! the default style for all other levels.
//! * `{h(the level is {l})}` -
//! <code style="color: red; font-weight: bold">the level is ERROR</code>
//! * `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.
Expand Down Expand Up @@ -449,6 +451,40 @@ impl<'a> From<Piece<'a>> 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),
Expand Down Expand Up @@ -554,6 +590,8 @@ enum FormattedChunk {
Newline,
Align(Vec<Chunk>),
Highlight(Vec<Chunk>),
Debug(Vec<Chunk>),
Release(Vec<Chunk>),
Mdc(String, String),
}

Expand Down Expand Up @@ -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)))
}
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit eba7f9f

Please sign in to comment.