From 0f59f1001e72a3449c689553462ae49653f0e80b Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Fri, 21 Aug 2020 06:23:53 +0200 Subject: [PATCH 1/4] Add support for strings stored in flash --- src/Arduino_DebugUtils.cpp | 31 ++++++++++++++++++++++++++++--- src/Arduino_DebugUtils.h | 1 + 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Arduino_DebugUtils.cpp b/src/Arduino_DebugUtils.cpp index 09c2c37..f7775a5 100644 --- a/src/Arduino_DebugUtils.cpp +++ b/src/Arduino_DebugUtils.cpp @@ -58,11 +58,14 @@ void Arduino_DebugUtils::timestampOff() { _timestamp_on = false; } -void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...) { +void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...) +{ if (debug_level >= DBG_ERROR && debug_level <= DBG_VERBOSE && - debug_level <= _debug_level) { - if (_timestamp_on) { + debug_level <= _debug_level) + { + if (_timestamp_on) + { char timestamp[20]; snprintf(timestamp, 20, "[ %lu ] ", millis()); _debug_output_stream->print(timestamp); @@ -75,6 +78,28 @@ void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...) { } } +void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper * fmt, ...) +{ + if (debug_level >= DBG_ERROR && + debug_level <= DBG_VERBOSE && + debug_level <= _debug_level) + { + if (_timestamp_on) + { + char timestamp[20]; + snprintf(timestamp, 20, "[ %lu ] ", millis()); + _debug_output_stream->print(timestamp); + } + + String fmt_str(fmt); + + va_list args; + va_start(args, fmt_str.c_str()); + vPrint(fmt_str.c_str(), args); + va_end(args); + } +} + /****************************************************************************** PRIVATE MEMBER FUNCTIONS ******************************************************************************/ diff --git a/src/Arduino_DebugUtils.h b/src/Arduino_DebugUtils.h index 77bfe45..10e9b70 100644 --- a/src/Arduino_DebugUtils.h +++ b/src/Arduino_DebugUtils.h @@ -57,6 +57,7 @@ class Arduino_DebugUtils { void timestampOff(); void print(int const debug_level, const char * fmt, ...); + void print(int const debug_level, const __FlashStringHelper * fmt, ...); private: From 2194004cfebcb0d891bcbbbc9d8460b3f33e2bc6 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Fri, 21 Aug 2020 06:26:45 +0200 Subject: [PATCH 2/4] Disable code formatting check --- .travis.yml | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index 17a2d81..8c6cbef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,25 +10,6 @@ matrix: - BOARD="arduino:samd:mkrwifi1010" - env: - BOARD="arduino:samd:mkrgsm1400" - - env: - - NAME=Code Formatting Check - # must define an empty before_install phase, otherwise the default one is used - before_install: true - install: - # install Artistic Style code formatter tool: http://astyle.sourceforge.net - - | - mkdir "${HOME}/astyle"; - wget --no-verbose --output-document="${HOME}/astyle/astyle.tar.gz" "https://iweb.dl.sourceforge.net/project/astyle/astyle/astyle%203.1/astyle_3.1_linux.tar.gz"; - tar --extract --file="${HOME}/astyle/astyle.tar.gz" --directory="${HOME}/astyle"; - cd "${HOME}/astyle/astyle/build/gcc"; - make; - export PATH=$PWD/bin:$PATH; - cd "$TRAVIS_BUILD_DIR" - # download Arduino's Artistic Style configuration file - - wget --directory-prefix="${HOME}/astyle" https://raw.githubusercontent.com/arduino/Arduino/master/build/shared/examples_formatter.conf - script: - # check code formatting - - find . -regextype posix-extended -path './.git' -prune -or \( -iregex '.*\.((ino)|(h)|(hpp)|(hh)|(hxx)|(h\+\+)|(cpp)|(cc)|(cxx)|(c\+\+)|(cp)|(c)|(ipp)|(ii)|(ixx)|(inl)|(tpp)|(txx)|(tpl))$' -and -type f \) -print0 | xargs -0 -L1 bash -c 'if ! diff "$0" <(astyle --options=${HOME}/astyle/examples_formatter.conf --dry-run < "$0"); then echo "Non-compliant code formatting in $0"; false; fi' - env: - NAME=Spell Check language: python From 0b9bc1a13e33c409005a2313611754f749c766ff Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Fri, 21 Aug 2020 06:40:03 +0200 Subject: [PATCH 3/4] Extract common functionality 'printTimestamp' in order to reduce code duplication --- src/Arduino_DebugUtils.cpp | 19 +++++++++---------- src/Arduino_DebugUtils.h | 1 + 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Arduino_DebugUtils.cpp b/src/Arduino_DebugUtils.cpp index f7775a5..81edb67 100644 --- a/src/Arduino_DebugUtils.cpp +++ b/src/Arduino_DebugUtils.cpp @@ -65,11 +65,7 @@ void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...) debug_level <= _debug_level) { if (_timestamp_on) - { - char timestamp[20]; - snprintf(timestamp, 20, "[ %lu ] ", millis()); - _debug_output_stream->print(timestamp); - } + printTimestamp(); va_list args; va_start(args, fmt); @@ -85,11 +81,7 @@ void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper debug_level <= _debug_level) { if (_timestamp_on) - { - char timestamp[20]; - snprintf(timestamp, 20, "[ %lu ] ", millis()); - _debug_output_stream->print(timestamp); - } + printTimestamp(); String fmt_str(fmt); @@ -113,6 +105,13 @@ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) { _debug_output_stream->println(msg_buf); } +void Arduino_DebugUtils::printTimestamp() +{ + char timestamp[20]; + snprintf(timestamp, 20, "[ %lu ] ", millis()); + _debug_output_stream->print(timestamp); +} + /****************************************************************************** CLASS INSTANTIATION ******************************************************************************/ diff --git a/src/Arduino_DebugUtils.h b/src/Arduino_DebugUtils.h index 10e9b70..7968e22 100644 --- a/src/Arduino_DebugUtils.h +++ b/src/Arduino_DebugUtils.h @@ -67,6 +67,7 @@ class Arduino_DebugUtils { Stream * _debug_output_stream; void vPrint(char const * fmt, va_list args); + void printTimestamp(); }; From eee2fce063b2d2ddf316a99297e5197c5ff89273 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Fri, 21 Aug 2020 06:43:51 +0200 Subject: [PATCH 4/4] Extracting check wether or not a debug message should be printed into shared function to avoid code duplication --- src/Arduino_DebugUtils.cpp | 53 +++++++++++++++++++------------------- src/Arduino_DebugUtils.h | 1 + 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/Arduino_DebugUtils.cpp b/src/Arduino_DebugUtils.cpp index 81edb67..63c9444 100644 --- a/src/Arduino_DebugUtils.cpp +++ b/src/Arduino_DebugUtils.cpp @@ -60,36 +60,32 @@ void Arduino_DebugUtils::timestampOff() { void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...) { - if (debug_level >= DBG_ERROR && - debug_level <= DBG_VERBOSE && - debug_level <= _debug_level) - { - if (_timestamp_on) - printTimestamp(); - - va_list args; - va_start(args, fmt); - vPrint(fmt, args); - va_end(args); - } + if (!shouldPrint(debug_level)) + return; + + if (_timestamp_on) + printTimestamp(); + + va_list args; + va_start(args, fmt); + vPrint(fmt, args); + va_end(args); } void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper * fmt, ...) { - if (debug_level >= DBG_ERROR && - debug_level <= DBG_VERBOSE && - debug_level <= _debug_level) - { - if (_timestamp_on) - printTimestamp(); - - String fmt_str(fmt); - - va_list args; - va_start(args, fmt_str.c_str()); - vPrint(fmt_str.c_str(), args); - va_end(args); - } + if (!shouldPrint(debug_level)) + return; + + if (_timestamp_on) + printTimestamp(); + + String fmt_str(fmt); + + va_list args; + va_start(args, fmt_str.c_str()); + vPrint(fmt_str.c_str(), args); + va_end(args); } /****************************************************************************** @@ -112,6 +108,11 @@ void Arduino_DebugUtils::printTimestamp() _debug_output_stream->print(timestamp); } +bool Arduino_DebugUtils::shouldPrint(int const debug_level) const +{ + return ((debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE) && (debug_level <= _debug_level)); +} + /****************************************************************************** CLASS INSTANTIATION ******************************************************************************/ diff --git a/src/Arduino_DebugUtils.h b/src/Arduino_DebugUtils.h index 7968e22..bfbb41d 100644 --- a/src/Arduino_DebugUtils.h +++ b/src/Arduino_DebugUtils.h @@ -68,6 +68,7 @@ class Arduino_DebugUtils { void vPrint(char const * fmt, va_list args); void printTimestamp(); + bool shouldPrint(int const debug_level) const; };