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

support kv_unstable #294

Closed
wants to merge 1 commit into from
Closed
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
62 changes: 62 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ color = ["dep:termcolor"]
auto-color = ["dep:is-terminal", "color"]
humantime = ["dep:humantime"]
regex = ["dep:regex"]
kv_unstable = ["log/kv_unstable"]

[dependencies]
log = { version = "0.4.8", features = ["std"] }
Expand Down
57 changes: 57 additions & 0 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
pub use super::{Target, TimestampPrecision, WriteStyle};
}

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

/// Formatting precision of timestamps.
///
/// Seconds give precision of full seconds, milliseconds give thousands of a
Expand Down Expand Up @@ -149,6 +152,9 @@
pub custom_format: Option<FormatFn>,
pub format_suffix: &'static str,
built: bool,

#[cfg(feature = "kv_unstable")]
pub format_key_values: bool,
}

impl Builder {
Expand Down Expand Up @@ -181,6 +187,9 @@
indent: built.format_indent,
suffix: built.format_suffix,
buf,

#[cfg(feature = "kv_unstable")]
key_values: built.format_key_values,
};

fmt.write(record)
Expand All @@ -200,6 +209,9 @@
custom_format: None,
format_suffix: "\n",
built: false,

#[cfg(feature = "kv_unstable")]
format_key_values: true,
}
}
}
Expand All @@ -221,6 +233,9 @@
indent: Option<usize>,
buf: &'a mut Formatter,
suffix: &'a str,

#[cfg(feature = "kv_unstable")]
key_values: bool,
}

impl<'a> DefaultFormat<'a> {
Expand All @@ -231,6 +246,9 @@
self.write_target(record)?;
self.finish_header()?;

#[cfg(feature = "kv_unstable")]
self.write_key_values(record)?;

self.write_args(record)
}

Expand Down Expand Up @@ -338,6 +356,45 @@
}
}

#[cfg(feature = "kv_unstable")]
fn write_key_values(&mut self, record: &Record) -> io::Result<()> {
if !self.key_values {
return Ok(());
}

struct KVVisitor<'a, 'b> {
fmt: &'a mut DefaultFormat<'b>,
visited: bool,
}

impl<'kvs> kv::Visitor<'kvs> for KVVisitor {

Check failure

Code scanning / clippy

implicit elided lifetime not allowed here Error

implicit elided lifetime not allowed here

Check failure

Code scanning / clippy

implicit elided lifetime not allowed here Error

implicit elided lifetime not allowed here
fn visit_pair(
&mut self,
key: kv::Key<'kvs>,
value: kv::Value<'kvs>,
) -> Result<(), kv::Error> {
if self.visited {
write!(self.fmt.buf, ", ")?;
} else {
self.visited = true;
}
write!(self.fmt.buf, "{}={}", key, value)?;
Ok(())
}
}

let mut visitor = KVVisitor {
fmt: self,
visited: false,
};
record.key_values().visit(&mut visitor)?;

Check failure

Code scanning / clippy

? couldn't convert the error to std::io::Error Error

? couldn't convert the error to std::io::Error

Check failure

Code scanning / clippy

? couldn't convert the error to std::io::Error Error

? couldn't convert the error to std::io::Error
if visitor.visited {
write!(self.buf, ": ")?;
}
Ok(())
}


fn write_args(&mut self, record: &Record) -> io::Result<()> {
match self.indent {
// Fast path for no indentation
Expand Down
7 changes: 7 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ impl Builder {
self
}

/// Configures if keys and values should be included.
#[cfg(feature = "kv_unstable")]
pub fn format_key_values(&mut self, value: bool) -> &mut Self {
self.format.format_key_values = value;
self
}

/// Adds a directive to the filter for a specific module.
///
/// # Examples
Expand Down
Loading