Expand a function via inline function or macro

Hello all

I am trying to write a debug routine.

There will be different levels of debugging as well as simple debugging messages and sensor values to debug.

The basic lay out with macros for the levels and so on work.

The idea was now to have a inline function or a macro that would expand at function call. The function call should pass the debug macro as well as the debug message or sensor value.

The version by macro didn't work at all. The inline function returned "expected primary-expression before 'if'" error for the debug macro which works fine without inline function.

Version with inline function. As I didnt know by which datatype to pass the macro I opted for template for both parameters passed to the function

#define DEBUG_SENSOR false
#define DEBUG_SENSOR_SERIAL if(DEBUG_SENSOR)Serial

#define DEBUG_ERROR true
#define DEBUG_ERROR_SERIAL if(DEBUG_ERROR)Serial

#if DEBUG_SENSOR == true || DEBUG_ERROR == true
#define DEBUG true
#else
#define DEBUG false
#endif
#define DEBUG_SERIAL if(DEBUG)Serial

const unsigned int debugOutputInterval = 1000;
unsigned long debugOutputTimestamp;
const unsigned long currentMillis;

template<typename T> inline void debugInterval (const T &debugSerial, const T &debugMsg)
{
  if ( currentMillis - debugOutputTimestamp >= debugOutputInterval )
  {
    debugOutputTimestamp = currentMillis;
    debugSerial.println(debugMsg);
  }
}

void setup()
{
  DEBUG_SERIAL.begin(115200);
  #if DEBUG == true
  while (!Serial);
  #endif
}

void loop()
{
const unsigned long currentMillis = millis();
  #if DEBUG_ERROR == true
  debugInterval(DEBUG_ERROR_SERIAL, "Hello");
  #endif
}

This would be the macro that was replaced by the inline function

#define DEBUGINTERVAL(x, y)\
do {  if ( currentMillis - debugOutputTimestamp >= debugOutputInterval )\
  {    debugOutputTimestamp = currentMillis;\
    (x).println((y))} while(false)

How could I improve the code to make it work?

Thanks you!

i think you're making this overly complicated. debug code certainly consume memory, i've run into that problem when i wrote code for an optical amplified, but i don't think you need to worry about program memory space on an arduino.

i suggest you have a global debug variable

int debug = 2;

and just put debug code inside a test

  if {1 < debug)  {
        char s [80];
        sprintf (s, "%s: %d", __func__, millis());
        Serial.println (s);
    }

you can various levels of debugging depending on value, either < or &

gcjr:
i think you're making this overly complicated. debug code certainly consume memory, i've run into that problem when i wrote code for an optical amplified, but i don't think you need to worry about program memory space on an arduino.

i suggest you have a global debug variable

int debug = 2;

and just put debug code inside a test

  if {1 < debug)  {

char s [80];
        sprintf (s, "%s: %d", func, millis());
        Serial.println (s);
    }



you can various levels of debugging depending on value, either < or &

Thank you gcjr

The sprintf idea helped me a lot.

Unfortunately I don't unterstand your 1<debug approach and the idea to use < or &.

What does that mean and how could I expand it for several levels?

you can have different levels (e.g. 1-5) levels of debug 5, in this case, being the most detailed (normally the lowest level functions) and resulting in a flood of output that can affect real-time performance.

you can also have different categories (1, 2, 4, 8, ...) of debug where each category is for a different section of code and allowing you to see debug statements from different sections at the same time.

and i'm sure there are other ways to selectively enable debugging along with real-time tracing