From 4bd55843e69301c77cf0eeeb01bf750b5cd87979 Mon Sep 17 00:00:00 2001
From: kbracher <k.bracher@fasttube.de>
Date: Sun, 9 Mar 2025 00:57:33 +0100
Subject: [PATCH] add: introduce timestamp logging option for SWO messages

---
 AMS_Master_Code/Core/Inc/swo_log.h | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/AMS_Master_Code/Core/Inc/swo_log.h b/AMS_Master_Code/Core/Inc/swo_log.h
index 093dd4a..1eb1e68 100644
--- a/AMS_Master_Code/Core/Inc/swo_log.h
+++ b/AMS_Master_Code/Core/Inc/swo_log.h
@@ -15,7 +15,8 @@
 
 #define MAX_MESSAGE_LENGTH 256
 #define USE_MULTIPLE_CHANNELS false // if true, each log level has its own channel (FATAL : 0, ERROR : 1, etc.)
-#define USE_ANSI_ESCAPE_CODES true // if true, log messages will be colored according to their log level, and the console can be cleared
+#define USE_ANSI_ESCAPE_CODES true  // if true, log messages will be colored according to their log level, and the console can be cleared
+#define PRINT_TIMESTAMP false       // if true, timestamp (from HAL_GetTick) is printed before each log message
 
 #if !USE_MULTIPLE_CHANNELS
 #define DEBUG_CHANNEL 0 // channel to output messages on
@@ -48,14 +49,14 @@ enum log_level_t {
 
 static inline bool __ITM_channel_enabled(uint32_t channel) {
 #if !USE_MULTIPLE_CHANNELS
-	#if USE_CHANNEL_MASK_VARIABLE
-		return ((ITM->TER & (1UL << DEBUG_CHANNEL)) != 0UL) && 
-           ((MASK_VARIABLE & (1UL << channel)) != 0UL);
-	#else
-		channel = DEBUG_CHANNEL;
-	#endif
+#if USE_CHANNEL_MASK_VARIABLE
+  return ((ITM->TER & (1UL << DEBUG_CHANNEL)) != 0UL) &&
+         ((MASK_VARIABLE & (1UL << channel)) != 0UL);
+#else
+  channel = DEBUG_CHANNEL;
 #endif
-	return ((ITM->TER & (1UL << channel)) != 0UL);
+#endif
+  return ((ITM->TER & (1UL << channel)) != 0UL);
 }
 
 // adapted from ITM_SendChar() in the CMSIS-Core
@@ -110,6 +111,18 @@ static void debug_clear_console() {
       size_t len =                                                             \
           snprintf(__swo_buffer, sizeof(__swo_buffer), msg, ##__VA_ARGS__);    \
       __swo_putc('\n', level);                                                 \
+      /* Print timestamp if enabled */                                         \
+      if (PRINT_TIMESTAMP) {                                                   \
+        char __time_buffer[16];                                                \
+        if (USE_ANSI_ESCAPE_CODES) {                                           \
+          snprintf(__time_buffer, sizeof(__time_buffer),                       \
+                   "\033[90m[%lu]\033[0m ", HAL_GetTick());                    \
+        } else {                                                               \
+          snprintf(__time_buffer, sizeof(__time_buffer), "[%lu] ",             \
+                   HAL_GetTick());                                             \
+        }                                                                      \
+        __swo_print(level, __time_buffer);                                     \
+      }                                                                        \
       __swo_print(level, log_level_names[level]);                              \
       __swo_print(level, __swo_buffer);                                        \
       if (len >= sizeof(__swo_buffer)) {                                       \