Giter VIP home page Giter VIP logo

arduino_debugutils's Introduction

Arduino_DebugUtils

Check Arduino status Compile Examples status Spell Check status

This class provides functionality useful for debugging sketches via printf-style statements.

How-To-Use Basic

Arduino_DebugUtils has 6 different debug levels (described descending from highest to lowest priority):

  • DBG_NONE - no debug output is shown
  • DBG_ERROR - critical errors
  • DBG_WARNING - non-critical errors
  • DBG_INFO - information
  • DBG_DEBUG - more information
  • DBG_VERBOSE - most information

The desired debug level can be set via setDebugLevel(DBG_WARNING).

Debug messages are written via print which supports printf-style formatted output.

Example:

int i = 1;
float pi = 3.1459;
DEBUG_VERBOSE("i = %d, pi = %f", i, pi);

Note: The output of floating point numbers (%f) does NOT work on ArduinoCore-avr.

If desired, timestamps can be prefixed to the debug message. Timestamp output can be enabled and disabled via timestampOn and timestampOff.

How-To-Use Advanced

Normally all debug output is redirected to the primary serial output of each board (Serial). In case you want to redirect the output to another output stream you can make use of setDebugOutputStream(&Serial2).

Documentation

Debug :

Arduino_DebugUtils Object that will be used for calling member functions.

Debug.setDebugLevel(int const debug_level) :

Parameter debug_level in order of lowest to highest priority are : DBG_NONE, DBG_ERROR, DBG_WARNING, DBG_INFO (default), DBG_DEBUG, and DBG_VERBOSE.

Return type: void.

Example:

Debug.setDebugLevel(DBG_VERBOSE);

Debug.setDebugOutputStream(Stream * stream) :

By default, Output Stream is Serial. In advanced cases other objects could be other serial ports (if available), or can be a Software Serial object.

Return type: void.

Example:

SoftwareSerial mySerial(10, 11); // RX, TX
Debug.setDebugOutputStream(&mySerial);

Debug.timestampOn() :

Calling this function switches on the timestamp in the Debug.print() function call; By default, printing timestamp is off, unless turned on using this function call.

Return type: void.

Example:

Debug.timestampOn();
DBG_VERBOSE("i = %d", i); //Output looks like : [ 21007 ] i = 21

Debug.timestampOff() :

Calling this function switches off the timestamp in the Debug.print() function call;

Return type: void.

Example:

Debug.timestampOff();
DEBUG_VERBOSE("i = %d", i); //Output looks like : i = 21

Debug.newlineOn() :

Calling this function ensures that a newline will be sent at the end of the Debug.print() function call; By default, a newline is sent Return type: void.

Example:

Debug.newlineOn();

Debug.newlineOff() :

Calling this function ensure that a newline will NOT be sent at the end of the Debug.print() function call; By default a newline is sent. Call this to shut that functionality off. Return type: void.

Example:

Debug.timestampOff();

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

This function prints the message if parameter debug_level in the Debug.print(debug_level, ...) function call belongs to the range: DBG_ERROR <= debug_level <= (<DBG_LEVEL> that has been set using setDebugLevel() function).

Return type: void.

Example:

Debug.setDebugLevel(DBG_VERBOSE);
int i = 0;
DEBUG_VERBOSE("DBG_VERBOSE i = %d", i);

arduino_debugutils's People

Contributors

abhaybd avatar aentinger avatar dependabot[bot] avatar highpower avatar pennam avatar per1234 avatar sanujkul avatar ubidefeo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

arduino_debugutils's Issues

Option to format timestamp as [ HH:MM:SS.MMM ]

Showing the timestamp as a raw ms count can be helpful for doing time-math, but for visual inspection, it can be much easier to have the timestamp formatted as something like [ HH:MM:SS.MMM ]. For more than 100 hours, an extra H digit can be added.

I suggest the addition of a format switch function, something like formatTimestampOn();

Allow getting the debug_level

It would be nice for the Debug Utils to be orthogonal so that once a debug level is set, functionality in other parts of the code can be enabled or disabled programmatically.

Add int getDebugMessageLevel(void); and int getDebugLevel(void); to Arduino_DebugUtils.h, complimenting the setDebugMessageLevel() and setDebugLevel() functions.

Add

int getDebugMessageLevel(void) {
  return Debug.getDebugLevel();
}

to Arduino_DebugUtils.cpp

Compiler warning/error for "va_start"

There is a compiler warning/error that occurs on build. It is:

...\Arduino_DebugUtils.cpp: In member function 'void Arduino_DebugUtils::print(int, const __FlashStringHelper*, ...)':
...\Arduino_DebugUtils.cpp:86:3: warning: second parameter of 'va_start' not last named argument [-Wvarargs]
va_start(args, fmt_str.c_str());

From my investigation, is seems that line 86 (of "Arduino_DebugUtils.cpp") is:

va_start(args, fmt_str.c_str());

and should instead be:

va_start(args, fmt);

I'm not overly familiar with that macro, but this version does stop the error and seems to function correctly when I run the examples.

Adding Debug.info() style functions

Hi, has there been any discussion around adding individual functions for each debug logging level, instead of passing in the level explicitly, similar to how the logging module in python works? I believe something like this should work:

void Arduino_DebugUtils::info(const char * fmt, ...)
{
  va_list args;
  va_start(args, fmt);
  print(DBG_INFO, fmt, args);
  va_end(args);
}

I would be more than happy to open a PR if this feels like something that should be added

function to print current debugLevel as string

At times, it can be helpful to print the current debug level to the serial monitor as a string. I've implemented this in my own code, but it could be helpful to include in the library to be accessible to all...

// Enum to String Inline Function
//----------------------------------------------------------------------------
  // * Translates enum value to a printable string
  // * enums are mapped to strings by order in list
  // * use static inline keywords to allow function definition in .h file   

 static inline char* debugLevelToString(int debugLevel)
    {
      char* str[] =
      {
        "DBG_NONE",
        "DBG_ERROR",
        "DBG_WARNING",
        "DBG_INFO",
        "DBG_DEBUG",
        "DBG_VERBOSE"
      };

      return str[debugLevel+1];   // DBG_NONE assigned to -1, so add 1 for correct output string
    }

New release?

Is there a new release planned that includes the recent introduction of DEBUG_* macros for Debug.print()?

Example doesn't seem to work properly.

The example:
int i = 1; float pi = 3.1459; Debug.print(DBG_VERBOSE, "i = %d, pi = %f", i, pi);

Results in:
i = 1, pi = ?

It looks like pi=%f doesn't work.

Option to show debugLevel of each Debug.print() statement (e.g. [V] ...)

When looking at a wall of debug print statements, it can be useful to see what debug level each statement was printed at so that you can decide if a particular statement should be moved to a different debug level. This could be achieved with as little as a single character, e.g. DBG_ERROR = E, DBG_WARNING = W, etc...

I suggest adding a switch to include printing the debug level of each Debug.print statement, e.g.

showDebugLevelOn()

Debug.print statements could then look like:

[ timestamp ] [E] "rest of print statement..."

or

[ timestamp E ] "rest of print statement..."

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.