Debug statements

V

vivek

Hi

i have used some debug macro and called several times(hundreds) in
the code.

The purpose of the macro was to print the values at that time to the
screen

Now that i have finished debugging, i do not want to execute the
macro.

If i use

#define DEBUG1 <blank>

the code executes the statements inside the debug except that it is
not printing on the screen, which takes time.

Is there any better way(than commenting all hundreds of lines of
code)?

Thanks in Advance,
VIVEK
 
V

vippstar

Hi

i have used some debug macro and called several times(hundreds) in
the code.

The purpose of the macro was to print the values at that time to the
screen

Now that i have finished debugging, i do not want to execute the
macro.

If i use

#define DEBUG1 <blank>

the code executes the statements inside the debug except that it is
not printing on the screen, which takes time.

Is there any better way(than commenting all hundreds of lines of
code)?
I believe not, unless you come up with some regex to do the commenting
for you.
You wouldn't have to do all this work now if you used a better method,
such as

int debug(void) {
#ifdef NDEBUG
/* ... */
#endif
return 0;
}

printing values is usually a poor debugging technique.
I suggest you learn to use a real debugger.
And if you decide to do so and then have a question about it, try a
newsgroup related to your debugger or debuggers in general and not clc.
 
B

Ben Pfaff

printing values is usually a poor debugging technique.
I suggest you learn to use a real debugger.

You have been around this newsgroup long enough that you should
know that many people do not agree with this assertion.
 
J

jacob navia

vivek said:
Hi

i have used some debug macro and called several times(hundreds) in
the code.

The purpose of the macro was to print the values at that time to the
screen

Now that i have finished debugging, i do not want to execute the
macro.

If i use

#define DEBUG1 <blank>

the code executes the statements inside the debug except that it is
not printing on the screen, which takes time.

Is there any better way(than commenting all hundreds of lines of
code)?

Thanks in Advance,
VIVEK

Youy should write:

#define DEBUG1(a)

This way, ALL the expression in "a" disappears.

If you write
#define DEBUG1

you eliminate the DEBUG1, leaving its argument (a) THERE!

Do not forget that the preprocessor is just a text replacement
machine

AND

It is better and less intrusive to use a real debugger
 
B

Ben Bacarisse

vivek said:
i have used some debug macro and called several times(hundreds) in
the code.

The purpose of the macro was to print the values at that time to the
screen

Now that i have finished debugging, i do not want to execute the
macro.

If i use

#define DEBUG1 <blank>

the code executes the statements inside the debug except that it is
not printing on the screen, which takes time.

Is there any better way(than commenting all hundreds of lines of
code)?

It may be too late now, but one way is to use extra parentheses:

#define DEBUG(x) logging_function x

and in the code:

DEBUG(("x = %d\n", x));

so when you need to get rid of all the code you define:

#define DEBUG(x)

and you get nothing at all.
 
K

Keith Thompson

Ben Bacarisse said:
It may be too late now, but one way is to use extra parentheses:

#define DEBUG(x) logging_function x

and in the code:

DEBUG(("x = %d\n", x));

so when you need to get rid of all the code you define:

#define DEBUG(x)

and you get nothing at all.

That's necessary only if DEBUG takes a variable number of arguments
(say, if it's a wrapper for fprintf).

Suppose DEBUG1 takes a single argument, and a typical call is

DEBUG1(some_expression);

Then this:

#define DEBUG1

causes the call to be replaced with

(some_expression);

which still evaluates the expression. But this:

#define DEBUG1(arg)

makes DEBUG1 a function-like macro, so the call is replaced with just this:

;

If you have several DEBUG*() macros taking various fixed numbers of
arguments, you can define an empty function-like macro for each. If
DEBUG() is variadic, you might be able to use a C99 variadic macro
definition *if* your compiler supports it (or supports an extension
that does something similar); I'm not familiar enough with that C99
feature to comment further.

If you actually need to make several hundred changes to your source
code, some of the more advanced features of your favorite text editor
or scripting language might be helpful. Of course that's a topic for
another newsgroup.
 
K

Keith Thompson

Robbie Hatley said:
What I do is use a function-like macro called "BLAT", defined
like so:

// In an included header file:
#ifdef BLAT_ENABLE
#define BLAT(X) printf ( X ) ;
#else
#define BLAT(X)
#endif

// In some source file:
#define BLAT_ENABLE
... some code...
BLAT("Length = %f and Height = %f\n", L, H)

After debugging, change "#define BLAT_ENABLE" to "#undef BLAT_ENABLE",
and the "BLAT" statements go away.

And if you maintain, expand, or refactor the code later, and it has
bugs, just #define BLAT_ENABLE and all your BLATs are re-activated.

That gives me complaints about passing too many arguments to BLAT. I
think you need to shift the parentheses from the macro definition to
the invocation, so it looks (to the preprocessor) like you're always
invoking BLAT with a single argument:

#ifdef BLAT_ENABLE
#define BLAT(X) printf X
#else
#define BLAT(X)
#endif

....

BLAT(("Length = %f and Height = %f\n", L, H));

Note that I've deleted the semicolon from the macro definition.
 
B

Ben Bacarisse

That's necessary only if DEBUG takes a variable number of arguments
(say, if it's a wrapper for fprintf).

Yes, and even then only if one does not have the use of C99's variadic
macros. Still, it is an old technique that is worth knowing if, for
no other reason, one might be puzzled on seeing it in existing code.
 
B

Ben Bacarisse

Ben Bacarisse said:
Keith Thompson <[email protected]> writes:

Yes, and even then only if one does not have the use of C99's variadic
macros. Still, it is an old technique that is worth knowing if, for
no other reason, one might be puzzled on seeing it in existing code.

.... which I see you had the foresight to mention. Sorry for the noise.
 
K

Keith Thompson

Robbie Hatley said:
Keith Thompson wrote: [...]
#ifdef BLAT_ENABLE
#define BLAT(X) printf X
#else
#define BLAT(X)
#endif

...

BLAT(("Length = %f and Height = %f\n", L, H));

Note that I've deleted the semicolon from the macro definition.

That works. Thanks for the tweak! But I'd leave the semicolon
in the macro definition, not the invocation. The reason is,
that way if you "#undef BLAT_ENABLE", even the semicolon
dissappears, and the compiler just sees an empty line.
[...]

I'd definitely leave the semicolon out of the macro definition.
Without the semicolon, an invocation of BLAT has the syntax of a
function call. With your method, everything *except* a BLAT()
invocation has a semicolon. The lone semicolon that remains when
BLAT(X) reduces to nothing is harmless; it's a null statement that's
legal wherever any statement is legal.

Of course you can do whatever you like with the preprocessor, but some
things are just good habits.
 
P

Philip Potter

You wouldn't have to do all this work now if you used a better method,
such as

int debug(void) {
#ifdef NDEBUG
/* ... */
#endif

#ifndef surely? That way, when you define NDEBUG your debug printouts
disappear along with your assert()s.
 
V

vippstar

#ifndef surely? That way, when you define NDEBUG your debug printouts
disappear along with your assert()s.

Ah, yeah, I had something else to suggest to OP then I thought of
NDEBUG but did not change the #ifdef line.
Thanks for spotting this.
 
P

Peter Nilsson

Hi

 i have used some debug macro and called several times(hundreds) in
the code.

The purpose of the macro was to print the values at that time to the
screen

Now that i have finished debugging, i do not want to execute the
macro.

If i use

#define DEBUG1 <blank>

the code executes the statements inside the debug except that it is
not printing on the screen, which takes time.

Is there any better way(than commenting all hundreds of lines of
code)?

I use a header that has something like...

#ifndef NDEBUG
#define debug
#define debugf debugf_
int debugf_(const char *, ...);
#else
#define debug while(0)
#define debugf (void)
#endif

With a library file containing something like...

int debugf_(const char *msg, ...)
{
int r;
va_list ap;
va_start(ap, msg);
r = vfprintf(stderr, msg, ap);
va_end(ap);
return r;
}

Usage samples...

debugf("x = %d\n", x);

debug
{
size_t i;
for (i = 0; i < N; i++)
assert(a > 0);
}

free(ptr);
debug ptr = NULL;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top