From cb90930632486d725c3a36a3d8499edb5c9de91b Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 30 Jun 2025 14:41:10 -0700 Subject: [PATCH] [mdns] improve local host address logging (#11651) This commit refines the logging of local host address events in the `Mdns` module. Address update events signaled from the platform layer are now logged at the `Debug` level instead of `Info`. This avoids excessive logging from platform implementations that use periodic polling for address monitoring. Instead, after the events are processed, an `Info` level log is now generated only if the address list has changed. This new log specifies which addresses were added or removed. Additionally, the format for IPv4 addresses (tracked as IPv4-mapped IPv6 addresses) is updated to use the standard dotted-decimal notation, making the logs easier to read. --- src/core/net/mdns.cpp | 26 ++++++++++++++++++++++++-- src/core/net/mdns.hpp | 2 ++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/core/net/mdns.cpp b/src/core/net/mdns.cpp index 4f2e858be..e5973072b 100644 --- a/src/core/net/mdns.cpp +++ b/src/core/net/mdns.cpp @@ -1566,7 +1566,7 @@ void Core::LocalHost::HandleAddressEvent(const Ip6::Address &aAddress, bool aAdd VerifyOrExit(Get().mIsEnabled); VerifyOrExit(aInfraIfIndex == Get().mInfraIfIndex); - LogInfo("Host address %s event: %s", aAddress.ToString().AsCString(), aAdded ? "added" : "removed"); + LogDebg("Host address %s event: %s", aAddress.ToString().AsCString(), aAdded ? "add" : "remove"); addrEvent = AddrEvent::Allocate(aAddress, aAdded); OT_ASSERT(addrEvent != nullptr); @@ -1601,7 +1601,7 @@ void Core::LocalHost::HandleAddressRemoveAll(uint32_t aInfraIfIndex) mAddrEvents.Clear(); mEventTimer.Stop(); - LogInfo("Host address event: remove all"); + LogDebg("Host address event: remove all"); for (const Ip6::Address &address : mIp4Addresses) { @@ -1645,6 +1645,10 @@ void Core::LocalHost::HandleEventTimer(void) { SuccessOrAssert(addresses.PushBack(address)); } + else + { + LogAddressChange(/* aAdded */ false, addrType, address); + } } // Next, add any new addresses for which we got an "added" @@ -1660,6 +1664,7 @@ void Core::LocalHost::HandleEventTimer(void) if (addrEvent.mAdded && !addresses.Contains(addrEvent.mAddress)) { SuccessOrAssert(addresses.PushBack(addrEvent.mAddress)); + LogAddressChange(/* aAdded */ true, addrType, addrEvent.mAddress); } } } @@ -1670,6 +1675,23 @@ exit: mAddrEvents.Clear(); } +#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) +void Core::LocalHost::LogAddressChange(bool aAdded, AddrType aAddrType, const Ip6::Address &aAddress) const +{ + Ip4::Address ip4Address; + + if (aAddrType == kIp4AddrType) + { + SuccessOrAssert(ip4Address.ExtractFromIp4MappedIp6Address(aAddress)); + } + + LogInfo("%s host address %s", aAdded ? "Adding" : "Removing", + aAddrType == kIp4AddrType ? ip4Address.ToString().AsCString() : aAddress.ToString().AsCString()); +} +#else +void Core::LocalHost::LogAddressChange(bool, AddrType, const Ip6::Address &) const {} +#endif + //---------------------------------------------------------------------------------------------------------------------- // Core::LocalHost::AddrEvent diff --git a/src/core/net/mdns.hpp b/src/core/net/mdns.hpp index 13d5eaf88..def39fa30 100644 --- a/src/core/net/mdns.hpp +++ b/src/core/net/mdns.hpp @@ -1182,6 +1182,8 @@ private: bool mAdded; }; + void LogAddressChange(bool aAdded, AddrType aAddrType, const Ip6::Address &aAddress) const; + using EventTimer = TimerMilliIn; Heap::String mName;