|
8 | 8 | #include <lib/support/EnforceFormat.h>
|
9 | 9 | #include <lib/support/logging/Constants.h>
|
10 | 10 | #include <platform/CHIPDeviceConfig.h>
|
| 11 | +#include <system/SystemClock.h> |
11 | 12 |
|
12 | 13 | #include <cstdio>
|
13 | 14 | #include <ctype.h>
|
@@ -44,38 +45,57 @@ namespace chip {
|
44 | 45 | namespace Logging {
|
45 | 46 | namespace Platform {
|
46 | 47 |
|
| 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 | + |
47 | 70 | /**
|
48 | 71 | * CHIP log output function.
|
49 | 72 | */
|
50 | 73 |
|
51 | 74 | void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v)
|
52 | 75 | {
|
53 | 76 | char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
|
54 |
| - size_t prefixLen; |
55 |
| - |
56 |
| - prefixLen = 0; |
| 77 | + size_t formattedMsgLen; |
57 | 78 |
|
58 | 79 | // No build-time switches in Qorvo logging module.
|
59 | 80 | // Add small prefix to show logging category for now.
|
60 |
| - formattedMsg[prefixLen++] = '['; |
61 | 81 | switch (category)
|
62 | 82 | {
|
63 | 83 | case kLogCategory_Error:
|
64 |
| - formattedMsg[prefixLen++] = 'E'; |
| 84 | + formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, "[E]", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE); |
65 | 85 | break;
|
66 | 86 | case kLogCategory_Detail:
|
67 |
| - formattedMsg[prefixLen++] = 'D'; |
| 87 | + formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, "[D]", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE); |
68 | 88 | break;
|
69 | 89 | case kLogCategory_Progress:
|
70 | 90 | default:
|
71 |
| - formattedMsg[prefixLen++] = 'P'; |
| 91 | + formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, "[P]", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE); |
72 | 92 | break;
|
73 | 93 | }
|
74 |
| - snprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, "][%s] ", module); |
| 94 | + snprintf(formattedMsg + formattedMsgLen, sizeof(formattedMsg) - formattedMsgLen, "[%s] ", module); |
75 | 95 | formattedMsg[sizeof(formattedMsg) - 2] = 0; // -2 to allow at least one char for the vsnprintf
|
76 |
| - prefixLen = strlen(formattedMsg); |
| 96 | + formattedMsgLen = strlen(formattedMsg); |
77 | 97 |
|
78 |
| - vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, msg, v); |
| 98 | + vsnprintf(formattedMsg + formattedMsgLen, sizeof(formattedMsg) - formattedMsgLen, msg, v); |
79 | 99 |
|
80 | 100 | qvCHIP_Printf(kPrintfModuleLogging, formattedMsg);
|
81 | 101 |
|
|
0 commit comments