Skip to content

Enable conditional formatting of timestamp. #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/Arduino_DebugUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Arduino_DebugUtils::Arduino_DebugUtils() {
timestampOff();
newlineOn();
debugLabelOff();
formatTimestampOff();
setDebugLevel(DEFAULT_DEBUG_LEVEL);
setDebugOutputStream(DEFAULT_OUTPUT_STREAM);
}
Expand Down Expand Up @@ -72,6 +73,14 @@ void Arduino_DebugUtils::debugLabelOff() {
_print_debug_label = false;
}

void Arduino_DebugUtils::formatTimestampOn() {
_format_timestamp_on = true;
}

void Arduino_DebugUtils::formatTimestampOff() {
_format_timestamp_on = false;
}

void Arduino_DebugUtils::timestampOn() {
_timestamp_on = true;
}
Expand Down Expand Up @@ -146,8 +155,39 @@ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) {

void Arduino_DebugUtils::printTimestamp()
{
char timestamp[20];
snprintf(timestamp, 20, "[ %lu ] ", millis());
char timestamp[32];

if (_format_timestamp_on)
{
auto const msCount = millis();

uint16_t const milliseconds = msCount % 1000; // ms remaining when converted to seconds
uint16_t const allSeconds = msCount / 1000; // total number of seconds to calculate remaining values

uint16_t const hours = allSeconds / 3600; // convert seconds to hours
uint16_t const secondsRemaining = allSeconds % 3600; // seconds left over

uint16_t const minutes = secondsRemaining / 60 ; // convert seconds left over to minutes
uint16_t const seconds = secondsRemaining % 60; // seconds left over

snprintf(timestamp, sizeof(timestamp), // "prints" formatted output to a char array (string)
"[ "
"%02d:" //HH:
"%02d:" //MM:
"%02d." //SS.
"%03d" //MMM
" ] ",
hours,
minutes,
seconds,
milliseconds
);
}
else
{
snprintf(timestamp, sizeof(timestamp), "[ %lu ] ", millis());
}

_debug_output_stream->print(timestamp);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Arduino_DebugUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class Arduino_DebugUtils {
void debugLabelOn();
void debugLabelOff();

void formatTimestampOn();
void formatTimestampOff();

void print(int const debug_level, const char * fmt, ...);
void print(int const debug_level, const __FlashStringHelper * fmt, ...);

Expand All @@ -73,6 +76,7 @@ class Arduino_DebugUtils {
bool _timestamp_on;
bool _newline_on;
bool _print_debug_label;
bool _format_timestamp_on;
int _debug_level;
Stream * _debug_output_stream;

Expand Down