diff --git a/src/core/common/log.hpp b/src/core/common/log.hpp index 49f0416e6..89bac0a15 100644 --- a/src/core/common/log.hpp +++ b/src/core/common/log.hpp @@ -78,6 +78,8 @@ enum LogLevel : uint8_t constexpr uint8_t kMaxLogModuleNameLength = 14; ///< Maximum module name length +constexpr uint16_t kMaxLogStringSize = OPENTHREAD_CONFIG_LOG_MAX_SIZE; ///< Max size of log string + #if OT_SHOULD_LOG && (OPENTHREAD_CONFIG_LOG_LEVEL != OT_LOG_LEVEL_NONE) /** * Registers log module name. diff --git a/src/core/thread/mesh_forwarder.cpp b/src/core/thread/mesh_forwarder.cpp index 151c38773..40f45992b 100644 --- a/src/core/thread/mesh_forwarder.cpp +++ b/src/core/thread/mesh_forwarder.cpp @@ -43,6 +43,7 @@ #include "net/ip6.hpp" #include "net/ip6_filter.hpp" #include "net/netif.hpp" +#include "net/socket.hpp" #include "net/tcp6.hpp" #include "net/udp6.hpp" #include "radio/radio.hpp" @@ -1882,27 +1883,27 @@ const char *MeshForwarder::MessagePriorityToString(const Message &aMessage) #if OPENTHREAD_CONFIG_LOG_SRC_DST_IP_ADDRESSES void MeshForwarder::LogIp6SourceDestAddresses(const Ip6::Headers &aHeaders, LogLevel aLogLevel) { - uint16_t srcPort = aHeaders.GetSourcePort(); - uint16_t dstPort = aHeaders.GetDestinationPort(); - - if (srcPort != 0) - { - LogAt(aLogLevel, " src:[%s]:%d", aHeaders.GetSourceAddress().ToString().AsCString(), srcPort); - } - else - { - LogAt(aLogLevel, " src:[%s]", aHeaders.GetSourceAddress().ToString().AsCString()); - } - - if (dstPort != 0) - { - LogAt(aLogLevel, " dst:[%s]:%d", aHeaders.GetDestinationAddress().ToString().AsCString(), dstPort); - } - else - { - LogAt(aLogLevel, " dst:[%s]", aHeaders.GetDestinationAddress().ToString().AsCString()); - } + LogIp6AddressAndPort("src", aHeaders.GetSourceAddress(), aHeaders.GetSourcePort(), aLogLevel); + LogIp6AddressAndPort("dst", aHeaders.GetDestinationAddress(), aHeaders.GetDestinationPort(), aLogLevel); } + +void MeshForwarder::LogIp6AddressAndPort(const char *aLabel, + const Ip6::Address &aAddress, + uint16_t aPort, + LogLevel aLogLevel) +{ + Ip6::SockAddr::InfoString string; + + string.Append("[%s]", aAddress.ToString().AsCString()); + + if (aPort != 0) + { + string.Append(":%u", aPort); + } + + LogAt(aLogLevel, " %s:%s", aLabel, string.AsCString()); +} + #else void MeshForwarder::LogIp6SourceDestAddresses(const Ip6::Headers &, LogLevel) {} #endif @@ -1913,30 +1914,19 @@ void MeshForwarder::LogIp6Message(MessageAction aAction, Error aError, LogLevel aLogLevel) { - Ip6::Headers headers; - bool shouldLogRss; - bool shouldLogRadio = false; - const char *radioString = ""; + Ip6::Headers headers; + String string; SuccessOrExit(headers.ParseFrom(aMessage)); - shouldLogRss = (aAction == kMessageReceive) || (aAction == kMessageReassemblyDrop); + string.Append("%s IPv6 %s msg, len:%u, chksum:%04x, ecn:%s, ", MessageActionToString(aAction, aError), + Ip6::Ip6::IpProtoToString(headers.GetIpProto()), aMessage.GetLength(), headers.GetChecksum(), + Ip6::Ip6::EcnToString(headers.GetEcn())); -#if OPENTHREAD_CONFIG_MULTI_RADIO - shouldLogRadio = true; - radioString = aMessage.IsRadioTypeSet() ? RadioTypeToString(aMessage.GetRadioType()) : "all"; -#endif + AppendMacAddrToLogString(string, aAction, aMacAddress); + AppendSecErrorPrioRssRadioLabelsToLogString(string, aAction, aMessage, aError); - LogAt(aLogLevel, "%s IPv6 %s msg, len:%d, chksum:%04x, ecn:%s%s%s, sec:%s%s%s, prio:%s%s%s%s%s", - MessageActionToString(aAction, aError), Ip6::Ip6::IpProtoToString(headers.GetIpProto()), aMessage.GetLength(), - headers.GetChecksum(), Ip6::Ip6::EcnToString(headers.GetEcn()), - (aMacAddress == nullptr) ? "" : ((aAction == kMessageReceive) ? ", from:" : ", to:"), - (aMacAddress == nullptr) ? "" : aMacAddress->ToString().AsCString(), - ToYesNo(aMessage.IsLinkSecurityEnabled()), - (aError == kErrorNone) ? "" : ", error:", (aError == kErrorNone) ? "" : ErrorToString(aError), - MessagePriorityToString(aMessage), shouldLogRss ? ", rss:" : "", - shouldLogRss ? aMessage.GetRssAverager().ToString().AsCString() : "", shouldLogRadio ? ", radio:" : "", - radioString); + LogAt(aLogLevel, "%s", string.AsCString()); if (aAction != kMessagePrepareIndirect) { @@ -1947,6 +1937,51 @@ exit: return; } +void MeshForwarder::AppendMacAddrToLogString(StringWriter &aString, + MessageAction aAction, + const Mac::Address *aMacAddress) +{ + VerifyOrExit(aMacAddress != nullptr); + + if (aAction == kMessageReceive) + { + aString.Append("from:"); + } + else + { + aString.Append("to:"); + } + + aString.Append("%s, ", aMacAddress->ToString().AsCString()); + +exit: + return; +} + +void MeshForwarder::AppendSecErrorPrioRssRadioLabelsToLogString(StringWriter &aString, + MessageAction aAction, + const Message &aMessage, + Error aError) +{ + aString.Append("sec:%s, ", ToYesNo(aMessage.IsLinkSecurityEnabled())); + + if (aError != kErrorNone) + { + aString.Append("error:%s, ", ErrorToString(aError)); + } + + aString.Append("prio:%s", MessagePriorityToString(aMessage)); + + if ((aAction == kMessageReceive) || (aAction == kMessageReassemblyDrop)) + { + aString.Append(", rss:%s", aMessage.GetRssAverager().ToString().AsCString()); + } + +#if OPENTHREAD_CONFIG_MULTI_RADIO + aString.Append(", radio:%s", aMessage.IsRadioTypeSet() ? RadioTypeToString(aMessage.GetRadioType()) : "all"); +#endif +} + void MeshForwarder::LogMessage(MessageAction aAction, const Message &aMessage) { LogMessage(aAction, aMessage, kErrorNone); diff --git a/src/core/thread/mesh_forwarder.hpp b/src/core/thread/mesh_forwarder.hpp index 2790be9ba..794141903 100644 --- a/src/core/thread/mesh_forwarder.hpp +++ b/src/core/thread/mesh_forwarder.hpp @@ -637,6 +637,9 @@ private: const Mac::Address *aAddress, Error aError, LogLevel aLogLevel); +#endif +#if OPENTHREAD_CONFIG_LOG_SRC_DST_IP_ADDRESSES + void LogIp6AddressAndPort(const char *aLabel, const Ip6::Address &aAddress, uint16_t aPort, LogLevel aLogLevel); #endif void LogIp6SourceDestAddresses(const Ip6::Headers &aHeaders, LogLevel aLogLevel); void LogIp6Message(MessageAction aAction, @@ -644,6 +647,11 @@ private: const Mac::Address *aAddress, Error aError, LogLevel aLogLevel); + void AppendSecErrorPrioRssRadioLabelsToLogString(StringWriter &aString, + MessageAction aAction, + const Message &aMessage, + Error aError); + void AppendMacAddrToLogString(StringWriter &aString, MessageAction aAction, const Mac::Address *aMacAddress); #endif // #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_NOTE) using TxTask = TaskletIn; diff --git a/src/core/thread/mesh_forwarder_ftd.cpp b/src/core/thread/mesh_forwarder_ftd.cpp index 1dc7fba10..97c68e40d 100644 --- a/src/core/thread/mesh_forwarder_ftd.cpp +++ b/src/core/thread/mesh_forwarder_ftd.cpp @@ -874,14 +874,12 @@ Error MeshForwarder::LogMeshFragmentHeader(MessageAction aAction, Mac::Addresses &aMeshAddrs, LogLevel aLogLevel) { - Error error = kErrorFailed; - bool hasFragmentHeader = false; - bool shouldLogRss; - Lowpan::MeshHeader meshHeader; - Lowpan::FragmentHeader fragmentHeader; - uint16_t headerLength; - bool shouldLogRadio = false; - const char *radioString = ""; + Error error = kErrorFailed; + bool hasFragmentHeader = false; + Lowpan::MeshHeader meshHeader; + Lowpan::FragmentHeader fragmentHeader; + uint16_t headerLength; + String string; SuccessOrExit(meshHeader.ParseFrom(aMessage, headerLength)); @@ -896,23 +894,17 @@ Error MeshForwarder::LogMeshFragmentHeader(MessageAction aAction, aOffset += headerLength; } - shouldLogRss = (aAction == kMessageReceive) || (aAction == kMessageReassemblyDrop); + string.Append("%s mesh frame, len:%u, ", MessageActionToString(aAction, aError), aMessage.GetLength()); -#if OPENTHREAD_CONFIG_MULTI_RADIO - shouldLogRadio = true; - radioString = aMessage.IsRadioTypeSet() ? RadioTypeToString(aMessage.GetRadioType()) : "all"; -#endif + AppendMacAddrToLogString(string, aAction, aMacAddress); - LogAt(aLogLevel, "%s mesh frame, len:%d%s%s, msrc:%s, mdst:%s, hops:%d, frag:%s, sec:%s%s%s%s%s%s%s", - MessageActionToString(aAction, aError), aMessage.GetLength(), - (aMacAddress == nullptr) ? "" : ((aAction == kMessageReceive) ? ", from:" : ", to:"), - (aMacAddress == nullptr) ? "" : aMacAddress->ToString().AsCString(), - aMeshAddrs.mSource.ToString().AsCString(), aMeshAddrs.mDestination.ToString().AsCString(), - meshHeader.GetHopsLeft() + ((aAction == kMessageReceive) ? 1 : 0), ToYesNo(hasFragmentHeader), - ToYesNo(aMessage.IsLinkSecurityEnabled()), - (aError == kErrorNone) ? "" : ", error:", (aError == kErrorNone) ? "" : ErrorToString(aError), - shouldLogRss ? ", rss:" : "", shouldLogRss ? aMessage.GetRssAverager().ToString().AsCString() : "", - shouldLogRadio ? ", radio:" : "", radioString); + string.Append("msrc:%s, mdst:%s, hops:%d, frag:%s, ", aMeshAddrs.mSource.ToString().AsCString(), + aMeshAddrs.mDestination.ToString().AsCString(), + meshHeader.GetHopsLeft() + ((aAction == kMessageReceive) ? 1 : 0), ToYesNo(hasFragmentHeader)); + + AppendSecErrorPrioRssRadioLabelsToLogString(string, aAction, aMessage, aError); + + LogAt(aLogLevel, "%s", string.AsCString()); if (hasFragmentHeader) {