diff --git a/README.md b/README.md index 8f08544..bd8dcd6 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ int i = 1; float pi = 3.1459; Debug.print(DBG_VERBOSE, "i = %d, pi = %f", i, pi); ``` +**Note**: The output of floating point numbers (`%f`) does NOT work on [ArduinoCore-avr](https://github.com/arduino/ArduinoCore-avr). If desired, timestamps can be prefixed to the debug message. Timestamp output can be enabled and disabled via `timestampOn` and `timestampOff`. diff --git a/library.properties b/library.properties index 8bbc786..c1099ca 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Arduino_DebugUtils -version=1.2.0 +version=1.3.0 author=Arduino maintainer=Arduino sentence=Debugging module with different debug levels, timestamps and printf-style output. diff --git a/src/Arduino_DebugUtils.cpp b/src/Arduino_DebugUtils.cpp index 7111715..3ffde7b 100644 --- a/src/Arduino_DebugUtils.cpp +++ b/src/Arduino_DebugUtils.cpp @@ -35,6 +35,8 @@ static Stream * DEFAULT_OUTPUT_STREAM = &Serial; Arduino_DebugUtils::Arduino_DebugUtils() { timestampOff(); newlineOn(); + debugLabelOff(); + formatTimestampOff(); setDebugLevel(DEFAULT_DEBUG_LEVEL); setDebugOutputStream(DEFAULT_OUTPUT_STREAM); } @@ -47,6 +49,10 @@ void Arduino_DebugUtils::setDebugLevel(int const debug_level) { _debug_level = debug_level; } +int Arduino_DebugUtils::getDebugLevel() const { + return _debug_level; +} + void Arduino_DebugUtils::setDebugOutputStream(Stream * stream) { _debug_output_stream = stream; } @@ -59,6 +65,22 @@ void Arduino_DebugUtils::newlineOff() { _newline_on = false; } +void Arduino_DebugUtils::debugLabelOn() { + _print_debug_label = true; +} + +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; } @@ -72,6 +94,9 @@ void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...) if (!shouldPrint(debug_level)) return; + if (_print_debug_label) + printDebugLabel(debug_level); + if (_timestamp_on) printTimestamp(); @@ -86,6 +111,9 @@ void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper if (!shouldPrint(debug_level)) return; + if (_print_debug_label) + printDebugLabel(debug_level); + if (_timestamp_on) printTimestamp(); @@ -127,11 +155,60 @@ 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); } +void Arduino_DebugUtils::printDebugLabel(int const debug_level) +{ + static char const * DEBUG_MODE_STRING[5] = + { + "[DBG_ERROR ] ", + "[DBG_WARNING] ", + "[DBG_INFO ] ", + "[DBG_DEBUG ] ", + "[DBG_VERBOSE] ", + }; + + bool is_valid_debug_level = (debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE); + if (!is_valid_debug_level) + return; + + _debug_output_stream->print(DEBUG_MODE_STRING[debug_level]); +} + bool Arduino_DebugUtils::shouldPrint(int const debug_level) const { return ((debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE) && (debug_level <= _debug_level)); @@ -142,6 +219,11 @@ bool Arduino_DebugUtils::shouldPrint(int const debug_level) const ******************************************************************************/ Arduino_DebugUtils Debug; + void setDebugMessageLevel(int const debug_level) { Debug.setDebugLevel(debug_level); } + +int getDebugMessageLevel() { + return Debug.getDebugLevel(); +} diff --git a/src/Arduino_DebugUtils.h b/src/Arduino_DebugUtils.h index b759534..335f1c5 100644 --- a/src/Arduino_DebugUtils.h +++ b/src/Arduino_DebugUtils.h @@ -38,6 +38,7 @@ static int const DBG_DEBUG = 3; static int const DBG_VERBOSE = 4; void setDebugMessageLevel(int const debug_level); +int getDebugMessageLevel(); /****************************************************************************** CLASS DECLARATION @@ -50,6 +51,7 @@ class Arduino_DebugUtils { Arduino_DebugUtils(); void setDebugLevel(int const debug_level); + int getDebugLevel() const; void setDebugOutputStream(Stream * stream); @@ -59,6 +61,12 @@ class Arduino_DebugUtils { void newlineOn(); void newlineOff(); + 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, ...); @@ -67,11 +75,14 @@ class Arduino_DebugUtils { bool _timestamp_on; bool _newline_on; + bool _print_debug_label; + bool _format_timestamp_on; int _debug_level; Stream * _debug_output_stream; void vPrint(char const * fmt, va_list args); void printTimestamp(); + void printDebugLabel(int const debug_level); bool shouldPrint(int const debug_level) const; };