#include "logging.hpp"
#include "common/instance.hpp"
#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_DEBUG_UART) && (!OPENTHREAD_CONFIG_ENABLE_DEBUG_UART)
#error OPENTHREAD_CONFIG_ENABLE_DEBUG_UART_LOG requires OPENTHREAD_CONFIG_ENABLE_DEBUG_UART
#endif
#define otLogDump(aFormat, ...) _otDynamicLog(aLogLevel, aLogRegion, aFormat OPENTHREAD_CONFIG_LOG_SUFFIX, __VA_ARGS__)
#ifdef __cplusplus
extern "C" {
#endif
#if OPENTHREAD_CONFIG_LOG_PKT_DUMP == 1
static void DumpLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const void *aBuf, const size_t aLength)
{
char buf[80];
char *cur = buf;
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), "|");
cur += strlen(cur);
for (size_t i = 0; i < 16; i++)
{
if (i < aLength)
{
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), " %02X", ((uint8_t *)(aBuf))[i]);
cur += strlen(cur);
}
else
{
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), " ..");
cur += strlen(cur);
}
if (!((i + 1) % 8))
{
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), " |");
cur += strlen(cur);
}
}
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), " ");
cur += strlen(cur);
for (size_t i = 0; i < 16; i++)
{
char c = 0x7f & ((char *)(aBuf))[i];
if (i < aLength && isprint(c))
{
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), "%c", c);
cur += strlen(cur);
}
else
{
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), ".");
cur += strlen(cur);
}
}
otLogDump("%s", buf);
}
void otDump(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aId, const void *aBuf, const size_t aLength)
{
size_t idlen = strlen(aId);
const size_t width = 72;
char buf[80];
char * cur = buf;
for (size_t i = 0; i < (width - idlen) / 2 - 5; i++)
{
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), "=");
cur += strlen(cur);
}
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), "[%s len=%03u]", aId, static_cast<uint16_t>(aLength));
cur += strlen(cur);
for (size_t i = 0; i < (width - idlen) / 2 - 4; i++)
{
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), "=");
cur += strlen(cur);
}
otLogDump("%s", buf);
for (size_t i = 0; i < aLength; i += 16)
{
DumpLine(aLogLevel, aLogRegion, (uint8_t *)(aBuf) + i, (aLength - i) < 16 ? (aLength - i) : 16);
}
cur = buf;
for (size_t i = 0; i < width; i++)
{
snprintf(cur, sizeof(buf) - static_cast<size_t>(cur - buf), "-");
cur += strlen(cur);
}
otLogDump("%s", buf);
}
#else
void otDump(otLogLevel, otLogRegion, const char *, const void *, const size_t)
{
}
#endif
static const char *const sThreadErrorStrings[OT_NUM_ERRORS] = {
"OK", "Failed", "Drop", "NoBufs", "NoRoute", "Busy", "Parse", "InvalidArgs", "Security", "AddressQuery", "NoAddress", "Abort", "NotImplemented", "InvalidState", "NoAck", "ChannelAccessFailure", "Detached", "FcsErr", "NoFrameReceived", "UnknownNeighbor", "InvalidSourceAddress", "AddressFiltered", "DestinationAddressFiltered", "NotFound", "Already", "ReservedError25", "Ipv6AddressCreationFailure", "NotCapable", "ResponseTimeout", "Duplicated", "ReassemblyTimeout", "NotTmf", "NonLowpanDataFrame", "ReservedError33", "LinkMarginLow", "InvalidCommand", };
const char *otThreadErrorToString(otError aError)
{
const char *retval;
if (aError < OT_ARRAY_LENGTH(sThreadErrorStrings))
{
retval = sThreadErrorStrings[aError];
}
else
{
retval = "UnknownErrorType";
}
return retval;
}
const char *otLogLevelToPrefixString(otLogLevel aLogLevel)
{
const char *retval = "";
switch (aLogLevel)
{
case OT_LOG_LEVEL_NONE:
retval = _OT_LEVEL_NONE_PREFIX;
break;
case OT_LOG_LEVEL_CRIT:
retval = _OT_LEVEL_CRIT_PREFIX;
break;
case OT_LOG_LEVEL_WARN:
retval = _OT_LEVEL_WARN_PREFIX;
break;
case OT_LOG_LEVEL_NOTE:
retval = _OT_LEVEL_NOTE_PREFIX;
break;
case OT_LOG_LEVEL_INFO:
retval = _OT_LEVEL_INFO_PREFIX;
break;
case OT_LOG_LEVEL_DEBG:
retval = _OT_LEVEL_DEBG_PREFIX;
break;
}
return retval;
}
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_NONE
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
{
OT_UNUSED_VARIABLE(aLogLevel);
OT_UNUSED_VARIABLE(aLogRegion);
OT_UNUSED_VARIABLE(aFormat);
}
#endif
#ifdef __cplusplus
}
#endif