Skip to content

Commit 901abd1

Browse files
authored
Enable conditional formatting of timestamp. (#28)
This fixes #5.
1 parent c7eec9a commit 901abd1

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Diff for: src/Arduino_DebugUtils.cpp

+42-2
Original file line numberDiff line numberDiff line change
@@ -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+
7584
void Arduino_DebugUtils::timestampOn() {
7685
_timestamp_on = true;
7786
}
@@ -146,8 +155,39 @@ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) {
146155

147156
void 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

Diff for: src/Arduino_DebugUtils.h

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class Arduino_DebugUtils {
6464
void debugLabelOn();
6565
void debugLabelOff();
6666

67+
void formatTimestampOn();
68+
void formatTimestampOff();
69+
6770
void print(int const debug_level, const char * fmt, ...);
6871
void print(int const debug_level, const __FlashStringHelper * fmt, ...);
6972

@@ -73,6 +76,7 @@ class Arduino_DebugUtils {
7376
bool _timestamp_on;
7477
bool _newline_on;
7578
bool _print_debug_label;
79+
bool _format_timestamp_on;
7680
int _debug_level;
7781
Stream * _debug_output_stream;
7882

0 commit comments

Comments
 (0)