diff --git a/src/platform/qpg/Logging.cpp b/src/platform/qpg/Logging.cpp index daee235e1c0ae2..37c716028f667b 100644 --- a/src/platform/qpg/Logging.cpp +++ b/src/platform/qpg/Logging.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -44,6 +45,28 @@ namespace chip { namespace Logging { namespace Platform { +/** + * @brief Add a timestamp in hh:mm:ss.ms format and the given prefix string to the given char buffer + * The time stamp is derived from the boot time + * + * @param logBuffer: pointer to the buffer where to add the information + * prefix: A prefix to add to the trace e.g. The category + * maxSize: Space availaible in the given buffer. + */ +static size_t AddTimeStampAndPrefixStr(char * logBuffer, const char * prefix, size_t maxSize) +{ + // Derive the hours, minutes, seconds and milliseconds since boot time millisecond counter + uint64_t bootTime = chip::System::SystemClock().GetMonotonicMilliseconds64().count(); + uint16_t milliseconds = bootTime % 1000; + uint32_t totalSeconds = bootTime / 1000; + uint8_t seconds = totalSeconds % 60; + totalSeconds /= 60; + uint8_t minutes = totalSeconds % 60; + uint32_t hours = totalSeconds / 60; + + return snprintf(logBuffer, maxSize, "[%02lu:%02u:%02u.%03u]%s", hours, minutes, seconds, milliseconds, prefix); +} + /** * CHIP log output function. */ @@ -51,31 +74,28 @@ namespace Platform { void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v) { char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]; - size_t prefixLen; - - prefixLen = 0; + size_t formattedMsgLen; // No build-time switches in Qorvo logging module. // Add small prefix to show logging category for now. - formattedMsg[prefixLen++] = '['; switch (category) { case kLogCategory_Error: - formattedMsg[prefixLen++] = 'E'; + formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, "[E]", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE); break; case kLogCategory_Detail: - formattedMsg[prefixLen++] = 'D'; + formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, "[D]", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE); break; case kLogCategory_Progress: default: - formattedMsg[prefixLen++] = 'P'; + formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, "[P]", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE); break; } - snprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, "][%s] ", module); + snprintf(formattedMsg + formattedMsgLen, sizeof(formattedMsg) - formattedMsgLen, "[%s] ", module); formattedMsg[sizeof(formattedMsg) - 2] = 0; // -2 to allow at least one char for the vsnprintf - prefixLen = strlen(formattedMsg); + formattedMsgLen = strlen(formattedMsg); - vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, msg, v); + vsnprintf(formattedMsg + formattedMsgLen, sizeof(formattedMsg) - formattedMsgLen, msg, v); qvCHIP_Printf(kPrintfModuleLogging, formattedMsg);