Hi
I am following a text, and in it, the author suggests implementing multi level debug messages with a macro definition so you can execute the program as follows:
And it would provide a more verbose level of debug as opposed to level 1. He says I can put the following code in a header file
Code:
#ifdef DEBON
#define DEBUG(level,fmt, ...) if (Debug >= level) fprintf (stderr, fmt, __VA_ARGS__)
#else
# define DEBUG(level, fmt, ...)
#endif
I just put the code in my sample program as follows:
Code:
#include <stdio.h>
#include <stdlib.h>
// int Debug;
//#define DEBUG(fmt, ...) fprintf (stderr, fmt, __VA_ARGS__ )
#ifdef DEBON
#define DEBUG(level,fmt, ...) if (Debug >= level) fprintf (stderr, fmt, __VA_ARGS__)
#else
# define DEBUG(level, fmt, ...)
#endif
int process ( int i1, int i2)
{
int val;
DEBUG (1, "process (%i, %i)\n", i1, i2);
val = i1 * i2;
DEBUG (3, "return %i\n", val);
return val;
}
int main ( int argc, char *argv[])
{
int arg1 = 0, arg2 = 0;
if (argc > 1)
arg1 = atoi (argv[1]);
if ( argc == 3)
arg2 = atoi (argv[2]);
DEBUG (1, "processed %i arguments\n", argc -1 );
DEBUG (3, "arg1 = %i, arg2 = %i\n" , arg1, arg2);
printf ("%d\n", process (arg1, arg2));
return 0;
}
But I get the following error messages:
Code:
$ gcc -g -D DEBON 173.c -o 173
173.c: In function 'process':
173.c:6:39: error: expected expression before 'if'
#define DEBUG(level,fmt, ...) (if (Debug >= level) fprintf (stderr, fmt, __VA_ARGS__))
^
173.c:14:3: note: in expansion of macro 'DEBUG'
DEBUG (1, "process (%i, %i)\n", i1, i2);
^
173.c:6:39: error: expected expression before 'if'
#define DEBUG(level,fmt, ...) (if (Debug >= level) fprintf (stderr, fmt, __VA_ARGS__))
^
173.c:16:3: note: in expansion of macro 'DEBUG'
DEBUG (3, "return %i\n", val);
^
173.c: In function 'main':
173.c:6:39: error: expected expression before 'if'
#define DEBUG(level,fmt, ...) (if (Debug >= level) fprintf (stderr, fmt, __VA_ARGS__))
^
173.c:27:1: note: in expansion of macro 'DEBUG'
DEBUG (1, "processed %i arguments\n", argc -1 );
^
173.c:6:39: error: expected expression before 'if'
#define DEBUG(level,fmt, ...) (if (Debug >= level) fprintf (stderr, fmt, __VA_ARGS__))
^
173.c:28:1: note: in expansion of macro 'DEBUG'
DEBUG (3, "arg1 = %i, arg2 = %i\n" , arg1, arg2);
^
I have seen some similar posts about this. One of them was Good practice of writing debug print statements
But I can not quite follow the advice. What am I doing wrong?