Log node ID in exchanges (#32550)
* Log nodeID in exchange context
* Update comments
* Use ChipLogValueScopedNodeId with ScopedNodeId instead of SubjectDescriptor
* Make node id logging optional
* Restyled by whitespace
* Restyled by clang-format
---------
Co-authored-by: Restyled.io <commits@restyled.io>
diff --git a/src/lib/core/CHIPConfig.h b/src/lib/core/CHIPConfig.h
index 2de6a66..558e0ee 100644
--- a/src/lib/core/CHIPConfig.h
+++ b/src/lib/core/CHIPConfig.h
@@ -462,6 +462,16 @@
#endif // CHIP_CONFIG_ENABLE_CONDITION_LOGGING
/**
+ * @def CHIP_EXCHANGE_NODE_ID_LOGGING
+ *
+ * @brief
+ * If asserted (1), enable logging of node IDs in exchange context.
+ */
+#ifndef CHIP_EXCHANGE_NODE_ID_LOGGING
+#define CHIP_EXCHANGE_NODE_ID_LOGGING 0
+#endif // CHIP_EXCHANGE_NODE_ID_LOGGING
+
+/**
* @def CHIP_CONFIG_TEST
*
* @brief
diff --git a/src/lib/support/logging/TextOnlyLogging.h b/src/lib/support/logging/TextOnlyLogging.h
index d6b22f7..b0abef9 100644
--- a/src/lib/support/logging/TextOnlyLogging.h
+++ b/src/lib/support/logging/TextOnlyLogging.h
@@ -268,16 +268,24 @@
#define ChipLogValueMEI(aValue) static_cast<uint16_t>(aValue >> 16), static_cast<uint16_t>(aValue)
/**
- * Logging helpers for exchanges. For now just log the exchange id and whether
- * it's an initiator or responder, but eventually we may want to log the peer
- * node id as well (especially for the responder case). Some callsites only
+ * Logging helpers for exchanges. Log the exchange id, whether
+ * it's an initiator or responder and the scoped node. Some callsites only
* have the exchange id and initiator/responder boolean, not an actual exchange,
* so we want to have a helper for that case too.
*/
#define ChipLogFormatExchangeId "%u%c"
#define ChipLogValueExchangeId(id, isInitiator) id, ((isInitiator) ? 'i' : 'r')
+
+#if CHIP_EXCHANGE_NODE_ID_LOGGING
+#define ChipLogFormatExchange ChipLogFormatExchangeId " with Node: " ChipLogFormatScopedNodeId
+#define ChipLogValueExchange(ec) \
+ ChipLogValueExchangeId((ec)->GetExchangeId(), (ec)->IsInitiator()), \
+ ChipLogValueScopedNodeId((ec)->HasSessionHandle() ? (ec)->GetSessionHandle()->GetPeer() : ScopedNodeId())
+#else // CHIP_EXCHANGE_NODE_ID_LOGGING
#define ChipLogFormatExchange ChipLogFormatExchangeId
#define ChipLogValueExchange(ec) ChipLogValueExchangeId((ec)->GetExchangeId(), (ec)->IsInitiator())
+#endif // CHIP_EXCHANGE_NODE_ID_LOGGING
+
#define ChipLogValueExchangeIdFromSentHeader(payloadHeader) \
ChipLogValueExchangeId((payloadHeader).GetExchangeID(), (payloadHeader).IsInitiator())
// A received header's initiator boolean is the inverse of the exchange's.