@@ -36,6 +36,7 @@ Arduino_DebugUtils::Arduino_DebugUtils() {
3636 timestampOff ();
3737 newlineOn ();
3838 debugLabelOff ();
39+ formatTimestampOff ();
3940 setDebugLevel (DEFAULT_DEBUG_LEVEL);
4041 setDebugOutputStream (DEFAULT_OUTPUT_STREAM);
4142}
@@ -72,6 +73,14 @@ void Arduino_DebugUtils::debugLabelOff() {
7273 _print_debug_label = false ;
7374}
7475
76+ void Arduino_DebugUtils::formatTimestampOn () {
77+ _format_timestamp_on = true ;
78+ }
79+
80+ void Arduino_DebugUtils::formatTimestampOff () {
81+ _format_timestamp_on = false ;
82+ }
83+
7584void Arduino_DebugUtils::timestampOn () {
7685 _timestamp_on = true ;
7786}
@@ -146,8 +155,39 @@ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) {
146155
147156void Arduino_DebugUtils::printTimestamp ()
148157{
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+
151191 _debug_output_stream->print (timestamp);
152192}
153193
0 commit comments