Skip to content

Commit a619332

Browse files
committed
Use from instead of capture for KV values (#109)
1 parent 5d95b07 commit a619332

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

spdlog/src/kv.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
//! A value is stored directly with its type by default (after erasure, of
4141
//! course), using _modifier_ if you want it to be stored in another format.
4242
//!
43-
//! | Modifier | Description |
44-
//! |----------|---------------------------------------------------------------------------------------|
45-
//! | | No modifier, capture the value directly |
46-
//! | `:` | Capture the value using [`Display`] trait |
47-
//! | `:?` | Capture the value using [`Debug`] trait |
48-
//! | `:sval` | Capture the value using [`sval::Value`] trait, crate feature `sval` is required |
49-
//! | `:serde` | Capture the value using [`serde::Serialize`] trait, crate feature `serde` is required |
43+
//! | Modifier | Description |
44+
//! |----------|------------------------------------------------------------------------------------|
45+
//! | | No modifier, pass the value directly, and type for primitives will be captured |
46+
//! | `:` | Pass the value using [`Display`] trait |
47+
//! | `:?` | Pass the value using [`Debug`] trait |
48+
//! | `:sval` | Pass the value using [`sval::Value`] trait, crate feature `sval` is required |
49+
//! | `:serde` | Pass the value using [`serde::Serialize`] trait, crate feature `serde` is required |
5050
//!
5151
//! ```
5252
//! # use spdlog::prelude::*;
@@ -60,12 +60,12 @@
6060
//! # }
6161
//! let url = Url::parse("https://example.com")?;
6262
//! trace!("user browsed website", kv: { url: });
63-
//! // ^ Capture the value using `Display` trait
63+
//! // ^ Pass the value using `Display` trait
6464
//! // ^^^^ Shorthand syntax, equivalent to `url: = url`
6565
//!
6666
//! let orders = vec!["coffee", "pizza", "soup"];
6767
//! info!("order received", kv: { orders:? });
68-
//! // ^^ Capture the value using `Debug` trait
68+
//! // ^^ Pass the value using `Debug` trait
6969
//! // ^^^^^^^^ Shorthand syntax, equivalent to `orders:? = orders`
7070
//!
7171
//! #[derive(sval_derive::Value)]
@@ -74,7 +74,7 @@
7474
//! let pos = Point { x: 11.4, y: 5.14 };
7575
//! # #[cfg(feature = "sval")]
7676
//! trace!("user clicked", kv: { pos:sval });
77-
//! // ^^^^^ Capture the value using `sval::Value` trait
77+
//! // ^^^^^ Pass the value using `sval::Value` trait
7878
//! // ^^^^^^^^ Shorthand syntax, equivalent to `pos:sval = pos`
7979
//! # Ok(()) }
8080
//! ```

spdlog/src/log_macros.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ macro_rules! __kv {
201201
macro_rules! __kv_value {
202202
($k:ident [$($modifier:tt)*]) => { $crate::__kv_value!($k [$($modifier)*] = $k) };
203203
($k:ident [ ] = $v:expr) => { $crate::kv::Value::from(&$v) };
204-
($k:ident [: ] = $v:expr) => { $crate::kv::Value::capture_display(&$v) };
205-
($k:ident [:?] = $v:expr) => { $crate::kv::Value::capture_debug(&$v) };
206-
($k:ident [:sval] = $v:expr) => { $crate::kv::Value::capture_sval2(&$v) };
207-
($k:ident [:serde] = $v:expr) => { $crate::kv::Value::capture_serde1(&$v) };
204+
($k:ident [: ] = $v:expr) => { $crate::kv::Value::from_display(&$v) };
205+
($k:ident [:?] = $v:expr) => { $crate::kv::Value::from_debug(&$v) };
206+
($k:ident [:sval] = $v:expr) => { $crate::kv::Value::from_sval2(&$v) };
207+
($k:ident [:serde] = $v:expr) => { $crate::kv::Value::from_serde1(&$v) };
208208
}
209209

210210
#[cfg(test)]
@@ -394,11 +394,11 @@ mod tests {
394394
match record.payload() {
395395
"1" => assert!(value.to_i64().is_some()),
396396
"2" => assert!(value.to_str().is_some()),
397-
"3" => assert!(value.to_i64().is_some()),
398-
"4" => assert!(value.to_i64().is_some()),
399-
"5" => assert!(value.is::<Vec<i32>>()),
400-
"6" => assert!(value.is::<Data>()),
401-
"7" => assert!(value.is::<Data>()),
397+
"3" => assert_eq!(value.to_string(), "1"),
398+
"4" => assert_eq!(value.to_string(), "1"),
399+
"5" => assert_eq!(value.to_string(), "[1, 2]"),
400+
"6" => assert_eq!(value.to_string(), "Data { i: 1, v: [1, 2] }"),
401+
"7" => assert_eq!(value.to_string(), "Data { i: 1, v: [1, 2] }"),
402402
_ => panic!(),
403403
}
404404
Ok(())
@@ -424,13 +424,26 @@ mod tests {
424424

425425
info!(logger: asserter, "1", kv: { v = 1 });
426426
info!(logger: asserter, "2", kv: { v = "string" });
427+
427428
info!(logger: asserter, "3", kv: { v: = 1 });
429+
info!(logger: asserter, "3", kv: { v: = &1 });
430+
428431
info!(logger: asserter, "4", kv: { v:? = 1 });
432+
info!(logger: asserter, "4", kv: { v:? = &1 });
433+
429434
#[cfg(feature = "sval")]
430435
info!(logger: asserter, "5", kv: { v:sval = vec![1, 2] });
436+
#[cfg(feature = "sval")]
437+
info!(logger: asserter, "5", kv: { v:sval = &vec![1, 2] });
438+
431439
#[cfg(feature = "sval")]
432440
info!(logger: asserter, "6", kv: { v:sval = data });
441+
#[cfg(feature = "sval")]
442+
info!(logger: asserter, "6", kv: { v:sval = &data });
443+
433444
#[cfg(feature = "serde")]
434445
info!(logger: asserter, "7", kv: { v:serde = data });
446+
#[cfg(feature = "serde")]
447+
info!(logger: asserter, "7", kv: { v:serde = &data });
435448
}
436449
}

0 commit comments

Comments
 (0)