Skip to content

Commit 6251662

Browse files
Add timestamp to the logs
1 parent cf4f2b8 commit 6251662

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

src/platform/EFR32/Logging.cpp

+53-28
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <lib/support/SafeString.h>
88
#include <lib/support/logging/CHIPLogging.h>
9+
#include <system/SystemClock.h>
910

1011
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
1112
#include <openthread/platform/logging.h>
@@ -54,21 +55,23 @@
5455
#include "SEGGER_RTT.h"
5556
#include "SEGGER_RTT_Conf.h"
5657

57-
#define LOG_ERROR "<error > "
58-
#define LOG_WARN "<warn > "
59-
#define LOG_INFO "<info > "
60-
#define LOG_DETAIL "<detail> "
58+
#define LOG_ERROR "[error ]"
59+
#define LOG_WARN "[warn ]"
60+
#define LOG_INFO "[info ]"
61+
#define LOG_DETAIL "[detail]"
6162
#if CHIP_SYSTEM_CONFIG_USE_LWIP
62-
#define LOG_LWIP "<lwip > "
63+
#define LOG_LWIP "[lwip ]"
6364
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
64-
#define LOG_EFR32 "<efr32 > "
65+
#define LOG_EFR32 "[efr32 ]"
6566
// If a new category string LOG_* is created, add it in the MaxStringLength arguments below
6667
#if CHIP_SYSTEM_CONFIG_USE_LWIP
6768
static constexpr size_t kMaxCategoryStrLen = chip::MaxStringLength(LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DETAIL, LOG_LWIP, LOG_EFR32);
6869
#else
6970
static constexpr size_t kMaxCategoryStrLen = chip::MaxStringLength(LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DETAIL, LOG_EFR32);
7071
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
7172

73+
static constexpr size_t kMaxTimestampStrLen = 16; // "[" (HH)HH:MM:SS + "." + miliseconds(3digits) + "]"
74+
7275
#if EFR32_LOG_ENABLED
7376
static bool sLogInitialized = false;
7477
#endif
@@ -78,6 +81,35 @@ static uint8_t sCmdLineBuffer[LOG_RTT_BUFFER_SIZE];
7881
#endif
7982

8083
#if EFR32_LOG_ENABLED
84+
85+
using namespace chip;
86+
87+
/**
88+
* @brief Add a timestamp in hh:mm:ss.ms format and the given prefix string to the given char buffer
89+
* The time stamp is derived from the boot time
90+
*
91+
* @param logBuffer: pointer to the buffer where to add the information
92+
* prefix: A prefix to add to the trace e.g. The category
93+
* maxSize: Space availaible in the given buffer.
94+
*/
95+
static size_t AddTimeStampAndPrefixStr(char * logBuffer, const char * prefix, size_t maxSize)
96+
{
97+
VerifyOrDie(logBuffer != nullptr);
98+
VerifyOrDie(prefix != nullptr);
99+
VerifyOrDie(maxSize > kMaxTimestampStrLen + strlen(prefix)); // Greater than to at least accommodate a ending Null Character
100+
101+
// Derive the hours, minutes, seconds and milliseconds since boot time millisecond counter
102+
uint64_t bootTime = chip::System::SystemClock().GetMonotonicMilliseconds64().count();
103+
uint16_t milliseconds = bootTime % 1000;
104+
uint32_t totalSeconds = bootTime / 1000;
105+
uint8_t seconds = totalSeconds % 60;
106+
totalSeconds /= 60;
107+
uint8_t minutes = totalSeconds % 60;
108+
uint32_t hours = totalSeconds / 60;
109+
110+
return snprintf(logBuffer, maxSize, "[%02lu:%02u:%02u.%03u]%s", hours, minutes, seconds, milliseconds, prefix);
111+
}
112+
81113
/**
82114
* Print a log message to RTT
83115
*/
@@ -135,10 +167,10 @@ extern "C" void efr32Log(const char * aFormat, ...)
135167
va_start(v, aFormat);
136168
#if EFR32_LOG_ENABLED
137169
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
138-
static_assert(sizeof(formattedMsg) > kMaxCategoryStrLen); // Greater than to at least accommodate a ending Null Character
170+
static_assert(sizeof(formattedMsg) >
171+
kMaxTimestampStrLen + kMaxCategoryStrLen); // Greater than to at least accommodate a ending Null Character
139172

140-
strcpy(formattedMsg, LOG_EFR32);
141-
size_t prefixLen = strlen(formattedMsg);
173+
size_t prefixLen = AddTimeStampAndPrefixStr(formattedMsg, LOG_EFR32, CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
142174
size_t len = vsnprintf(formattedMsg + prefixLen, sizeof formattedMsg - prefixLen, aFormat, v);
143175

144176
if (len >= sizeof formattedMsg - prefixLen)
@@ -182,26 +214,24 @@ void LogV(const char * module, uint8_t category, const char * aFormat, va_list v
182214
size_t formattedMsgLen;
183215

184216
// len for Category string + "[" + Module name + "] " (Brackets and space =3)
185-
constexpr size_t maxPrefixLen = kMaxCategoryStrLen + chip::Logging::kMaxModuleNameLen + 3;
217+
constexpr size_t maxPrefixLen = kMaxTimestampStrLen + kMaxCategoryStrLen + chip::Logging::kMaxModuleNameLen + 3;
186218
static_assert(sizeof(formattedMsg) > maxPrefixLen); // Greater than to at least accommodate a ending Null Character
187219

188220
switch (category)
189221
{
190222
case kLogCategory_Error:
191-
strcpy(formattedMsg, LOG_ERROR);
223+
formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, LOG_ERROR, CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
192224
break;
193225
case kLogCategory_Progress:
194226
default:
195-
strcpy(formattedMsg, LOG_INFO);
227+
formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, LOG_INFO, CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
196228
break;
197229
case kLogCategory_Detail:
198-
strcpy(formattedMsg, LOG_DETAIL);
230+
formattedMsgLen = AddTimeStampAndPrefixStr(formattedMsg, LOG_DETAIL, CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
199231
break;
200232
}
201233

202-
formattedMsgLen = strlen(formattedMsg);
203-
204-
// Form the log prefix, e.g. "[DL] "
234+
// Add the module name to the log prefix , e.g. "[DL] "
205235
snprintf(formattedMsg + formattedMsgLen, sizeof(formattedMsg) - formattedMsgLen, "[%s] ", module);
206236
formattedMsg[sizeof(formattedMsg) - 1] = 0;
207237
formattedMsgLen = strlen(formattedMsg);
@@ -238,8 +268,7 @@ extern "C" void LwIPLog(const char * aFormat, ...)
238268
#if EFR32_LOG_ENABLED
239269
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
240270

241-
strcpy(formattedMsg, LOG_LWIP);
242-
size_t prefixLen = strlen(formattedMsg);
271+
size_t prefixLen = AddTimeStampAndPrefixStr(formattedMsg, LOG_LWIP, CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
243272
size_t len = vsnprintf(formattedMsg + prefixLen, sizeof formattedMsg - prefixLen, aFormat, v);
244273

245274
if (len >= sizeof formattedMsg - prefixLen)
@@ -272,34 +301,30 @@ extern "C" void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const ch
272301

273302
va_start(v, aFormat);
274303
#if EFR32_LOG_ENABLED
304+
size_t prefixLen;
275305
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
276306

277307
if (sLogInitialized)
278308
{
279309
switch (aLogLevel)
280310
{
281311
case OT_LOG_LEVEL_CRIT:
282-
strcpy(formattedMsg, LOG_ERROR "[ot] ");
312+
prefixLen = AddTimeStampAndPrefixStr(formattedMsg, LOG_ERROR "[ot] ", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
283313
break;
284314
case OT_LOG_LEVEL_WARN:
285-
strcpy(formattedMsg, LOG_WARN "[ot] ");
315+
prefixLen = AddTimeStampAndPrefixStr(formattedMsg, LOG_WARN "[ot] ", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
286316
break;
287317
case OT_LOG_LEVEL_NOTE:
288-
strcpy(formattedMsg, LOG_INFO "[ot] ");
289-
break;
290318
case OT_LOG_LEVEL_INFO:
291-
strcpy(formattedMsg, LOG_INFO "[ot] ");
319+
prefixLen = AddTimeStampAndPrefixStr(formattedMsg, LOG_INFO "[ot] ", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
292320
break;
293321
case OT_LOG_LEVEL_DEBG:
294-
strcpy(formattedMsg, LOG_DETAIL "[ot] ");
295-
break;
296322
default:
297-
strcpy(formattedMsg, LOG_DETAIL "[ot] ");
323+
prefixLen = AddTimeStampAndPrefixStr(formattedMsg, LOG_DETAIL "[ot] ", CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE);
298324
break;
299325
}
300326

301-
size_t prefixLen = strlen(formattedMsg);
302-
size_t len = vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, aFormat, v);
327+
size_t len = vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, aFormat, v);
303328

304329
if (len >= sizeof formattedMsg - prefixLen)
305330
{

0 commit comments

Comments
 (0)