Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(encode/pattern): add debug and release formatters #301

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ jobs:
run: cargo build

- name: Test lib
run: cargo test --all-features
run: |
cargo test --all-features
# This test needs another run under the release mode
cargo test --release --features simple_writer encode::pattern::tests::debug_release
env:
RUSTFLAGS: -D warnings

Expand Down
87 changes: 87 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,37 @@ 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());
}
}
}