diff --git a/src/connectivity/wlan/lib/inspect/src/log/impls.rs b/src/connectivity/wlan/lib/inspect/src/log/impls.rs index c9a069afa6e30a42eddeb5050b77be9ca3b2bff5..4385aeaf1d8e0e33646f49c722180d60af49475d 100644 --- a/src/connectivity/wlan/lib/inspect/src/log/impls.rs +++ b/src/connectivity/wlan/lib/inspect/src/log/impls.rs @@ -68,16 +68,6 @@ impl<V: WriteInspect + ?Sized> WriteInspect for &V { } } -/// `Option<T>` does not write an inspect value in the `None` case -impl<T: WriteInspect> WriteInspect for Option<T> { - fn write_inspect(&self, node: &mut finspect::ObjectTreeNode, key: &str) { - match self { - Some(val) => val.write_inspect(node, key), - None => (), - } - } -} - // --- Implementations for WLAN types --- impl_write_inspect!(Str, self => format!("{:?}", self), fidl_common::Cbw, fidl_mlme::BssTypes); diff --git a/src/connectivity/wlan/lib/inspect/src/log/mod.rs b/src/connectivity/wlan/lib/inspect/src/log/mod.rs index 8818fea7a5cdf7af21fc726f34242bbe67072f5c..91907d6105c263666cf0818aa0468f90e2c36ed4 100644 --- a/src/connectivity/wlan/lib/inspect/src/log/mod.rs +++ b/src/connectivity/wlan/lib/inspect/src/log/mod.rs @@ -35,7 +35,17 @@ pub trait WriteInspect { /// subkey2: 2, /// } /// }) +/// inspect_log!(bounded_list_node, { // logging optional value (only "some" is logged) +/// some?: Some(1), +/// none?: Option::<String>::None +/// } /// ``` +/// +/// Note that `inspect_log` will always append a new node. Thus, the following snippet +/// ``` +/// inspect_log!(bounded_list_node, none?: Option::<String>::None); +/// ``` +/// would create a node with only a timestamp. #[macro_export] macro_rules! inspect_log { // internal-only shared implementation, should not be used outside @@ -62,6 +72,7 @@ macro_rules! inspect_log { #[macro_export] macro_rules! inspect_insert { ($node:expr,) => {}; + // inserting a nested tree ($node:expr, $key:ident: { $($sub:tt)+ }) => { { let child = $node.create_child(stringify!($key)); @@ -74,6 +85,16 @@ macro_rules! inspect_insert { inspect_insert!($node, $($rest)*); }; + // inserting an optional value + ($node:expr, $key:ident?: $val:expr) => { + $node.insert_maybe(stringify!($key), $val); + }; + ($node:expr, $key:ident?: $val:expr, $($rest:tt)*) => { + inspect_insert!($node, $key?: $val); + inspect_insert!($node, $($rest)*); + }; + + // inserting a value that implements WriteInspect ($node:expr, $key:ident: $val:expr) => { $node.insert(stringify!($key), &$val); }; @@ -156,6 +177,17 @@ mod tests { test_utils::assert_str_prop(&sub2_obj, "subsub1", "subsubval1"); } + #[test] + fn test_inspect_log_optional_value() { + let mut node = BoundedListNode::new(finspect::ObjectTreeNode::new_root(), 10); + + inspect_log!(node, some?: Some("a"), none?: Option::<String>::None); + let node0 = node.inner().lock().get_child("0").expect("expect node entry 0"); + let obj0 = node0.lock().evaluate(); + test_utils::assert_str_prop(&obj0, "some", "a"); + assert!(obj0.get_property("none").is_none()); + } + #[test] fn test_inspect_log_parsing() { // if this test compiles, it's considered as succeeded @@ -185,25 +217,12 @@ mod tests { // if this test compiles, it's considered as succeeded let mut node = BoundedListNode::new(finspect::ObjectTreeNode::new_root(), 10); let s = String::from("s"); - inspect_log!(node, s: s); + let opt = Some(1); + inspect_log!(node, s: s, opt?: opt); // Should not cause compiler error since value is not moved println!("{}", s); - } - - #[test] - fn test_log_option() { - let mut node = BoundedListNode::new(finspect::ObjectTreeNode::new_root(), 10); - - inspect_log!(node, some: Some("a")); - let node0 = node.inner().lock().get_child("0").expect("expect node entry 0"); - let obj0 = node0.lock().evaluate(); - test_utils::assert_str_prop(&obj0, "some", "a"); - - inspect_log!(node, none: None as Option<String>); - let node1 = node.inner().lock().get_child("1").expect("expect node entry 1"); - let obj1 = node1.lock().evaluate(); - assert!(obj1.get_property("none").is_none()); + println!("{:?}", opt); } #[test] diff --git a/src/connectivity/wlan/lib/inspect/src/nodes/mod.rs b/src/connectivity/wlan/lib/inspect/src/nodes/mod.rs index 335df2702d21db5a65f35f600133ab7a29b7089b..17c3d9545e528f2f7ae3ede634d2a0f81caa0858 100644 --- a/src/connectivity/wlan/lib/inspect/src/nodes/mod.rs +++ b/src/connectivity/wlan/lib/inspect/src/nodes/mod.rs @@ -27,6 +27,7 @@ pub trait NodeExt { fn insert_str<S: Into<String>>(&mut self, key: &str, value: S) -> &mut Self; fn insert_debug<D: std::fmt::Debug>(&mut self, key: &str, value: D) -> &mut Self; fn insert<V: WriteInspect>(&mut self, key: &str, value: V) -> &mut Self; + fn insert_maybe<V: WriteInspect>(&mut self, key: &str, value: Option<V>) -> &mut Self; } impl NodeExt for finspect::ObjectTreeNode { @@ -70,6 +71,13 @@ impl NodeExt for finspect::ObjectTreeNode { value.write_inspect(self, key); self } + + fn insert_maybe<V: WriteInspect>(&mut self, key: &str, value: Option<V>) -> &mut Self { + if let Some(ref value) = value { + value.write_inspect(self, key); + } + self + } } #[cfg(test)] diff --git a/src/connectivity/wlan/lib/sme/src/client/mod.rs b/src/connectivity/wlan/lib/sme/src/client/mod.rs index 05b176cf2f3fa539e3dbb90b1e994b9512179cd8..fa4d98aad087b907c6fed64f52d3a2e8f4ca461b 100644 --- a/src/connectivity/wlan/lib/sme/src/client/mod.rs +++ b/src/connectivity/wlan/lib/sme/src/client/mod.rs @@ -412,7 +412,7 @@ fn inspect_log_join_scan( .insert("rsni_db", bss.rsni_dbh / 2) .insert("rssi_dbm", bss.rssi_dbm); }); - inspect_log!(node, bss_list: inspect_bss, result: result_msg); + inspect_log!(node, bss_list: inspect_bss, result?: result_msg); } fn report_connect_finished( diff --git a/src/connectivity/wlan/lib/sme/src/client/state.rs b/src/connectivity/wlan/lib/sme/src/client/state.rs index 4e8dfd20edcf26b529329bc894ba0f5883acc0f3..d16f075738df7336fbb9e2699c470d0f6c40f6da 100644 --- a/src/connectivity/wlan/lib/sme/src/client/state.rs +++ b/src/connectivity/wlan/lib/sme/src/client/state.rs @@ -320,7 +320,7 @@ impl State { inspect_log!(context.inspect.states, { from: start_state, to: new_state.state_name(), - ctx: state_change_msg, + ctx?: state_change_msg, }); } new_state @@ -409,7 +409,7 @@ impl State { inspect_log!(context.inspect.states, { from: start_state, to: new_state.state_name(), - ctx: state_change_msg, + ctx?: state_change_msg, }); } new_state