@@ -36,6 +36,7 @@ Arduino_DebugUtils::Arduino_DebugUtils() {
36
36
timestampOff ();
37
37
newlineOn ();
38
38
debugLabelOff ();
39
+ formatTimestampOff ();
39
40
setDebugLevel (DEFAULT_DEBUG_LEVEL);
40
41
setDebugOutputStream (DEFAULT_OUTPUT_STREAM);
41
42
}
@@ -72,6 +73,14 @@ void Arduino_DebugUtils::debugLabelOff() {
72
73
_print_debug_label = false ;
73
74
}
74
75
76
+ void Arduino_DebugUtils::formatTimestampOn () {
77
+ _format_timestamp_on = true ;
78
+ }
79
+
80
+ void Arduino_DebugUtils::formatTimestampOff () {
81
+ _format_timestamp_on = false ;
82
+ }
83
+
75
84
void Arduino_DebugUtils::timestampOn () {
76
85
_timestamp_on = true ;
77
86
}
@@ -146,8 +155,39 @@ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) {
146
155
147
156
void Arduino_DebugUtils::printTimestamp ()
148
157
{
149
- char timestamp[20 ];
150
- snprintf (timestamp, 20 , " [ %lu ] " , millis ());
158
+ char timestamp[32 ];
159
+
160
+ if (_format_timestamp_on)
161
+ {
162
+ auto const msCount = millis ();
163
+
164
+ uint16_t const milliseconds = msCount % 1000 ; // ms remaining when converted to seconds
165
+ uint16_t const allSeconds = msCount / 1000 ; // total number of seconds to calculate remaining values
166
+
167
+ uint16_t const hours = allSeconds / 3600 ; // convert seconds to hours
168
+ uint16_t const secondsRemaining = allSeconds % 3600 ; // seconds left over
169
+
170
+ uint16_t const minutes = secondsRemaining / 60 ; // convert seconds left over to minutes
171
+ uint16_t const seconds = secondsRemaining % 60 ; // seconds left over
172
+
173
+ snprintf (timestamp, sizeof (timestamp), // "prints" formatted output to a char array (string)
174
+ " [ "
175
+ " %02d:" // HH:
176
+ " %02d:" // MM:
177
+ " %02d." // SS.
178
+ " %03d" // MMM
179
+ " ] " ,
180
+ hours,
181
+ minutes,
182
+ seconds,
183
+ milliseconds
184
+ );
185
+ }
186
+ else
187
+ {
188
+ snprintf (timestamp, sizeof (timestamp), " [ %lu ] " , millis ());
189
+ }
190
+
151
191
_debug_output_stream->print (timestamp);
152
192
}
153
193
0 commit comments