Skip to content
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

function to print current debugLevel as string #7

Closed
MattStarfield opened this issue Aug 26, 2020 · 2 comments · Fixed by #26 or #27
Closed

function to print current debugLevel as string #7

MattStarfield opened this issue Aug 26, 2020 · 2 comments · Fixed by #26 or #27
Labels
conclusion: resolved Issue was resolved topic: code Related to content of the project itself type: enhancement Proposed improvement

Comments

@MattStarfield
Copy link

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
    }
@MattStarfield
Copy link
Author

I came up with an alternate (better method) putting the strings into PROGMEM to save RAM...

  // Enum Strings stored in PROGMEM
  //----------------------------------------------------------------------------
    // const char stringName [NUMBER_OF_ELEMENTS] [MAX_SIZE] PROGMEM = {
    // http://www.gammon.com.au/forum/?id=12615
    const char debugModeString [6] [ENUM_STRING_BUFFER_SIZE] PROGMEM =
    {
      {"DBG_NONE"},
      {"DBG_ERROR"},
      {"DBG_WARNING"},
      {"DBG_INFO"},
      {"DBG_DEBUG"},
      {"DBG_VERBOSE"},
    };  // END -- debugModeString[][]

To print the strings from PROGMEM, I have this helper function:

    char progMemStringBuffer [ENUM_STRING_BUFFER_SIZE];      // global char buffer for getProgMemString()

    // Return a string from PROGMEM directly, use with Serial.print() or sprintf()
    char* getProgMemString(const char * progMemString)
    {
      // Retrieve PROGMEM enum string for use in Debug.print message
      sprintf_P (progMemStringBuffer, PSTR ("%S"), progMemString);    // use %S (not %s) for PROGMEM string flag

      return progMemStringBuffer;
    } // END -- getProgMemString()

and call in my program like this:

Serial.print(getProgMemString(debugModeString[debugLevel+1]));  // +1 due to debugLevel enum value settings in Arduino_DebugUtils.h

@aentinger
Copy link
Contributor

Hi @MattStarfield 👋 Since you've got it all figured out already, how about a PR? Please consider that PROGMEM is something AVR-specific, consequently your PR should also support other Arduino cores (which is actually quite trivial).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment