Skip to content

Commit fec6d7a

Browse files
dvdm-qorvopull[bot]
authored andcommitted
[QPG] Added device timestamps in qpg logging (#27321)
1 parent 3242d76 commit fec6d7a

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/platform/qpg/Logging.cpp

+30-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <lib/support/EnforceFormat.h>
99
#include <lib/support/logging/Constants.h>
1010
#include <platform/CHIPDeviceConfig.h>
11+
#include <system/SystemClock.h>
1112

1213
#include <cstdio>
1314
#include <ctype.h>
@@ -44,38 +45,57 @@ namespace chip {
4445
namespace Logging {
4546
namespace Platform {
4647

48+
/**
49+
* @brief Add a timestamp in hh:mm:ss.ms format and the given prefix string to the given char buffer
50+
* The time stamp is derived from the boot time
51+
*
52+
* @param logBuffer: pointer to the buffer where to add the information
53+
* prefix: A prefix to add to the trace e.g. The category
54+
* maxSize: Space availaible in the given buffer.
55+
*/
56+
static size_t AddTimeStampAndPrefixStr(char * logBuffer, const char * prefix, size_t maxSize)
57+
{
58+
// Derive the hours, minutes, seconds and milliseconds since boot time millisecond counter
59+
uint64_t bootTime = chip::System::SystemClock().GetMonotonicMilliseconds64().count();
60+
uint16_t milliseconds = bootTime % 1000;
61+
uint32_t totalSeconds = bootTime / 1000;
62+
uint8_t seconds = totalSeconds % 60;
63+
totalSeconds /= 60;
64+
uint8_t minutes = totalSeconds % 60;
65+
uint32_t hours = totalSeconds / 60;
66+
67+
return snprintf(logBuffer, maxSize, "[%02lu:%02u:%02u.%03u]%s", hours, minutes, seconds, milliseconds, prefix);
68+
}
69+
4770
/**
4871
* CHIP log output function.
4972
*/
5073

5174
void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v)
5275
{
5376
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
54-
size_t prefixLen;
55-
56-
prefixLen = 0;
77+
size_t formattedMsgLen;
5778

5879
// No build-time switches in Qorvo logging module.
5980
// Add small prefix to show logging category for now.
60-
formattedMsg[prefixLen++] = '[';
6181
switch (category)
6282
{
6383
case kLogCategory_Error:
64-
formattedMsg[prefixLen++] = 'E';
84+
formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, "[E]", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
6585
break;
6686
case kLogCategory_Detail:
67-
formattedMsg[prefixLen++] = 'D';
87+
formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, "[D]", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
6888
break;
6989
case kLogCategory_Progress:
7090
default:
71-
formattedMsg[prefixLen++] = 'P';
91+
formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, "[P]", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
7292
break;
7393
}
74-
snprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, "][%s] ", module);
94+
snprintf(formattedMsg + formattedMsgLen, sizeof(formattedMsg) - formattedMsgLen, "[%s] ", module);
7595
formattedMsg[sizeof(formattedMsg) - 2] = 0; // -2 to allow at least one char for the vsnprintf
76-
prefixLen = strlen(formattedMsg);
96+
formattedMsgLen = strlen(formattedMsg);
7797

78-
vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, msg, v);
98+
vsnprintf(formattedMsg + formattedMsgLen, sizeof(formattedMsg) - formattedMsgLen, msg, v);
7999

80100
qvCHIP_Printf(kPrintfModuleLogging, formattedMsg);
81101

0 commit comments

Comments
 (0)