diff --git a/src/core/common/logging.cpp b/src/core/common/logging.cpp index 8037e5f1f..24df5066a 100644 --- a/src/core/common/logging.cpp +++ b/src/core/common/logging.cpp @@ -179,6 +179,21 @@ void otLogMac(otLogLevel aLogLevel, const char *aFormat, ...) exit: return; } + +void otDumpMacFrame(otLogLevel aLogLevel, const char *aId, const void *aBuf, const size_t aLength) +{ + constexpr uint8_t kFixedStringPart = 10; // strlen(" seqno=000") + constexpr uint8_t kSeqnoIdx = 2; // index of the sequence number within aBuf + + size_t idLength = strlen(aId) + kFixedStringPart + 1; // allow for the '\0' character + char newId[25]; + + VerifyOrExit(idLength <= sizeof(newId)); + snprintf(newId, idLength, "%s seqno=%03u", aId, static_cast(aBuf)[kSeqnoIdx]); + otDump(aLogLevel, OT_LOG_REGION_MAC, newId, aBuf, aLength); +exit: + return; +} #endif #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE @@ -276,19 +291,20 @@ static void DumpLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const uint8_t void otDump(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aId, const void *aBuf, const size_t aLength) { - constexpr uint8_t kWidth = 72; + constexpr uint8_t kWidth = 72; + constexpr uint8_t kFixedStringPart = 10; // strlen("[ len=000]") - size_t idLen = strlen(aId); + size_t idLen = strlen(aId) + kFixedStringPart; ot::String string; - for (size_t i = 0; i < (kWidth - idLen) / 2 - 5; i++) + for (size_t i = 0; i < (kWidth - idLen) / 2; i++) { string.Append("="); } string.Append("[%s len=%03u]", aId, static_cast(aLength)); - for (size_t i = 0; i < (kWidth - idLen) / 2 - 4; i++) + for (size_t i = 0; i < kWidth - idLen - (kWidth - idLen) / 2; i++) { string.Append("="); } diff --git a/src/core/common/logging.hpp b/src/core/common/logging.hpp index 902b72efb..21243df80 100644 --- a/src/core/common/logging.hpp +++ b/src/core/common/logging.hpp @@ -2245,11 +2245,12 @@ void otLogOtns(const char *aFormat, ...); * */ #if OPENTHREAD_CONFIG_LOG_MAC -#define otDumpCritMac(aId, aBuf, aLength) otDumpCrit(OT_LOG_REGION_MAC, aId, aBuf, aLength) -#define otDumpWarnMac(aId, aBuf, aLength) otDumpWarn(OT_LOG_REGION_MAC, aId, aBuf, aLength) -#define otDumpNoteMac(aId, aBuf, aLength) otDumpNote(OT_LOG_REGION_MAC, aId, aBuf, aLength) -#define otDumpInfoMac(aId, aBuf, aLength) otDumpInfo(OT_LOG_REGION_MAC, aId, aBuf, aLength) -#define otDumpDebgMac(aId, aBuf, aLength) otDumpDebg(OT_LOG_REGION_MAC, aId, aBuf, aLength) +void otDumpMacFrame(otLogLevel aLogLevel, const char *aId, const void *aBuf, size_t aLength); +#define otDumpCritMac(aId, aBuf, aLength) otDumpMacFrame(OT_LOG_LEVEL_CRIT, aId, aBuf, aLength) +#define otDumpWarnMac(aId, aBuf, aLength) otDumpMacFrame(OT_LOG_LEVEL_WARN, aId, aBuf, aLength) +#define otDumpNoteMac(aId, aBuf, aLength) otDumpMacFrame(OT_LOG_LEVEL_NOTE, aId, aBuf, aLength) +#define otDumpInfoMac(aId, aBuf, aLength) otDumpMacFrame(OT_LOG_LEVEL_INFO, aId, aBuf, aLength) +#define otDumpDebgMac(aId, aBuf, aLength) otDumpMacFrame(OT_LOG_LEVEL_DEBG, aId, aBuf, aLength) #else #define otDumpCritMac(aId, aBuf, aLength) #define otDumpWarnMac(aId, aBuf, aLength)