Pages: [1]   Go Down
Author Topic: Serial Debug Macro  (Read 1003 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For debugging, it's handy to print debug messages to the serial port and track down problems. But if you want to trigger the debugging on and off it becomes annoying to comment, and uncomment those Serial.print and Serial.println statements. I wrote this macro that can help in triggering the debugging by simply changing the #define SERIAL_DEBUG_ENABLED  value to 0 or 1. The macro code is :
Code:
#define SERIAL_DEBUG_ENABLED 1

#define GET_NUM_ARGS(...) GET_NUM_ARGS_ACT(__VA_ARGS__, 5,4,3,2,1)
#define GET_NUM_ARGS_ACT(_1,_2,_3,_4,_5,N,...) N

#define macro_dispatcher(func, ...) \
            macro_dispatcher_(func, GET_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(func, nargs) \
            macro_dispatcher__(func, nargs)
#define macro_dispatcher__(func, nargs) \
            func ## nargs
           
#if SERIAL_DEBUG_ENABLED
  #define DebugPrint(...) macro_dispatcher(DebugPrint, __VA_ARGS__)(__VA_ARGS__)
  #define DebugPrintln(...) macro_dispatcher(DebugPrintln, __VA_ARGS__)(__VA_ARGS__) 
  #define DebugPrint2(str,modifier)  \
        Serial.print(millis());     \
        Serial.print(": ");    \
        Serial.print(__PRETTY_FUNCTION__); \
        Serial.print(' ');      \
        Serial.print(__LINE__);     \
        Serial.print(' ');      \
        Serial.print(str,modifier);
  #define DebugPrint1(str)  \
        Serial.print(millis());     \
        Serial.print(": ");    \
        Serial.print(__PRETTY_FUNCTION__); \
        Serial.print(' ');      \
        Serial.print(__LINE__);     \
        Serial.print(' ');      \
        Serial.print(str);       
  #define DebugPrintln2(str,modifier)  \
        Serial.print(millis());     \
        Serial.print(": ");    \
        Serial.print(__PRETTY_FUNCTION__); \
        Serial.print(' ');      \
        Serial.print(__LINE__);     \
        Serial.print(' ');      \
        Serial.println(str,modifier);
  #define DebugPrintln1(str)  \
        Serial.print(millis());     \
        Serial.print(": ");    \
        Serial.print(__PRETTY_FUNCTION__); \
        Serial.print(' ');      \
        Serial.print(__LINE__);     \
        Serial.print(' ');      \
        Serial.println(str);         
#else
  #define DebugPrint(...) macro_dispatcher(DebugPrint, __VA_ARGS__)(__VA_ARGS__)
  #define DebugPrintln(...) macro_dispatcher(DebugPrintln, __VA_ARGS__)(__VA_ARGS__) 
  #define DebugPrint1(str)
  #define DebugPrintln1(str)
  #define DebugPrint2(str,modifier)
  #define DebugPrintln2(str,modifier)
#endif

and in your code all you need to do is something like this:

Code:
DebugPrint("The value of x is:");
DebugPrintln(x,DEC);

I hope this helps, let me know if you have any suggestions.


Tarek
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
let me know if you have any suggestions.
Use PROGMEM for the strings?

Code:
#if SERIAL_DEBUG_ENABLED
even simpler would be
Code:
#ifdef SERIAL_DEBUG_ENABLED
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: