Skip to content

Commit

Permalink
chore: Rename kv_unstable feature as unstable-kv
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Jan 15, 2024
1 parent b5f0ea0 commit 5277afa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ color = ["dep:termcolor"]
auto-color = ["dep:is-terminal", "color"]
humantime = ["dep:humantime"]
regex = ["dep:regex"]
kv_unstable = ["log/kv_unstable"]
unstable-kv = ["log/kv_unstable"]

[dependencies]
log = { version = "0.4.8", features = ["std"] }
Expand Down
76 changes: 41 additions & 35 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//!
//! # Key Value arguments
//!
//! If the `kv_unstable` feature is enabled, then the default format will include key values from
//! If the `unstable-kv` feature is enabled, then the default format will include key values from
//! the log by default, but this can be disabled by calling [`Builder::format_key_value_style`]
//! with [`KVStyle::Hidden`].
//!
Expand All @@ -52,7 +52,7 @@ use std::io::prelude::*;
use std::rc::Rc;
use std::{fmt, io, mem};

#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
use log::kv;
use log::Record;

Expand Down Expand Up @@ -199,6 +199,7 @@ pub(crate) struct Builder {
pub format_indent: Option<usize>,
pub custom_format: Option<FormatFn>,
pub format_suffix: &'static str,
#[cfg(feature = "unstable-kv")]
pub key_value_style: KVStyle,
built: bool,
}
Expand Down Expand Up @@ -232,6 +233,7 @@ impl Builder {
written_header_value: false,
indent: built.format_indent,
suffix: built.format_suffix,
#[cfg(feature = "unstable-kv")]
key_value_style: built.key_value_style,
buf,
};
Expand All @@ -252,6 +254,7 @@ impl Default for Builder {
format_indent: Some(4),
custom_format: None,
format_suffix: "\n",
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Multiline,
built: false,
}
Expand All @@ -275,6 +278,7 @@ struct DefaultFormat<'a> {
indent: Option<usize>,
buf: &'a mut Formatter,
suffix: &'a str,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle,
}

Expand All @@ -287,6 +291,7 @@ impl<'a> DefaultFormat<'a> {
self.finish_header()?;

self.write_args(record)?;
#[cfg(feature = "unstable-kv")]
self.write_kv(record)?;
write!(self.buf, "{}", self.suffix)
}
Expand Down Expand Up @@ -447,52 +452,43 @@ impl<'a> DefaultFormat<'a> {
}
}

#[cfg(feature = "unstable-kv")]
fn write_kv(&mut self, record: &Record) -> io::Result<()> {
#[cfg(feature = "kv_unstable")]
{
match self.key_value_style {
KVStyle::Hidden => Ok(()),
KVStyle::Multiline => record
.key_values()
.visit(&mut KVMultilineVisitor(self))
.map_err(|e| io::Error::new(io::ErrorKind::Other, e)),
KVStyle::Inline => {
let kvs = record.key_values();
if kvs.count() > 0 {
write!(self.buf, " {}", self.subtle_style("{"))?;

kvs.visit(&mut KVInlineVisitor(self.buf))
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
write!(self.buf, "{}", self.subtle_style(" }"))?;
}
Ok(())
match self.key_value_style {
KVStyle::Hidden => Ok(()),
KVStyle::Multiline => record
.key_values()
.visit(&mut KVMultilineVisitor(self))
.map_err(|e| io::Error::new(io::ErrorKind::Other, e)),
KVStyle::Inline => {
let kvs = record.key_values();
if kvs.count() > 0 {
write!(self.buf, " {}", self.subtle_style("{"))?;

kvs.visit(&mut KVInlineVisitor(self.buf))
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
write!(self.buf, "{}", self.subtle_style(" }"))?;
}
Ok(())
}
}
#[cfg(not(feature = "kv_unstable"))]
{
// avoid dead code lints
let _ = record;
let _ = self.key_value_style;
Ok(())
}
}
}

#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
struct KVInlineVisitor<'a>(&'a mut Formatter);

#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
impl<'a, 'kvs> kv::Visitor<'kvs> for KVInlineVisitor<'a> {
fn visit_pair(&mut self, key: kv::Key<'kvs>, value: kv::Value<'kvs>) -> Result<(), kv::Error> {
write!(self.0, " {}={}", key, value).map_err(|e| e.into())
}
}

#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
struct KVMultilineVisitor<'a, 'fmt>(&'a mut DefaultFormat<'fmt>);

#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
impl<'a, 'kvs, 'fmt> kv::Visitor<'kvs> for KVMultilineVisitor<'a, 'fmt> {
fn visit_pair(&mut self, key: kv::Key<'kvs>, value: kv::Value<'kvs>) -> Result<(), kv::Error> {
let indent = self.0.indent.unwrap_or(0);
Expand Down Expand Up @@ -560,6 +556,7 @@ mod tests {
module_path: true,
target: false,
level: true,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: None,
Expand All @@ -579,6 +576,7 @@ mod tests {
module_path: false,
target: false,
level: false,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: None,
Expand All @@ -598,6 +596,7 @@ mod tests {
module_path: true,
target: false,
level: true,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: Some(4),
Expand All @@ -617,6 +616,7 @@ mod tests {
module_path: true,
target: false,
level: true,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: Some(0),
Expand All @@ -636,6 +636,7 @@ mod tests {
module_path: false,
target: false,
level: false,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: Some(4),
Expand All @@ -655,6 +656,7 @@ mod tests {
module_path: false,
target: false,
level: false,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: None,
Expand All @@ -674,6 +676,7 @@ mod tests {
module_path: false,
target: false,
level: false,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: Some(4),
Expand All @@ -695,6 +698,7 @@ mod tests {
module_path: true,
target: true,
level: true,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: None,
Expand All @@ -715,6 +719,7 @@ mod tests {
module_path: true,
target: true,
level: true,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: None,
Expand All @@ -736,6 +741,7 @@ mod tests {
module_path: true,
target: false,
level: true,
#[cfg(feature = "unstable-kv")]
key_value_style: KVStyle::Hidden,
written_header_value: false,
indent: None,
Expand All @@ -747,7 +753,7 @@ mod tests {
assert_eq!("[INFO test::path] log\nmessage\n", written);
}

#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
#[test]
fn format_kv_trailer() {
let kvs = &[("a", 1u32), ("b", 2u32)][..];
Expand Down Expand Up @@ -777,7 +783,7 @@ mod tests {
assert_eq!("[INFO ] log message { a=1 b=2 }\n", written);
}

#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
#[test]
fn format_kv_trailer_full() {
let kvs = &[("a", 1u32), ("b", 2u32)][..];
Expand Down Expand Up @@ -813,7 +819,7 @@ mod tests {
);
}

#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
#[test]
fn format_kv_multiline() {
let kvs = &[("a", 1u32), ("b", 2u32)][..];
Expand Down Expand Up @@ -843,7 +849,7 @@ mod tests {
assert_eq!("[INFO ] log\nmessage\na: 1\nb: 2\n", written);
}

#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
#[test]
fn format_kv_multiline_indented() {
let kvs = &[("a", 1u32), ("b", 2u32)][..];
Expand Down
2 changes: 1 addition & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl Builder {
}

/// Configure the style for writing key/value pairs
#[cfg(feature = "kv_unstable")]
#[cfg(feature = "unstable-kv")]
pub fn format_key_value_style(&mut self, style: fmt::KVStyle) -> &mut Self {
self.format.key_value_style = style;
self
Expand Down

0 comments on commit 5277afa

Please sign in to comment.