473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Debug statements

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
Apr 3 '08 #1
9 4273
On Apr 3, 6:58 pm, vivek <gvivek2...@gmail.comwrote:
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.
Apr 3 '08 #2
vivek wrote:
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

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Apr 3 '08 #3
vivek <gv********@gmail.comwrites:
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.

--
Ben.
Apr 3 '08 #4
Ben Bacarisse <be********@bsb.me.ukwrites:
vivek <gv********@gmail.comwrites:
> 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.
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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 3 '08 #5
"vivek" wrote:
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)?
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.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Apr 3 '08 #6
Keith Thompson <ks***@mib.orgwrites:
Ben Bacarisse <be********@bsb.me.ukwrites:
>vivek <gv********@gmail.comwrites:
>> i have used some debug macro and called several times(hundreds) in
the code.
<snip>
>>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));
<snip>
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.

--
Ben.
Apr 3 '08 #7
Ben Bacarisse <be********@bsb.me.ukwrites:
Keith Thompson <ks***@mib.orgwrites:
<snip stuff about debugging>
>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.
.... which I see you had the foresight to mention. Sorry for the noise.

--
Ben.
Apr 3 '08 #8
"Robbie Hatley" <se**************@for.my.email.addresswrites:
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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 4 '08 #9

Keith Thompson wrote:
"Robbie Hatley" <se**************@for.my.email.addresswrites:
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.
Yes. But that's why I don't do it. I like making
function-like macro invocations look DIFFERENT from actual
function calls. They're not C functions at all, they're
text-processing commands, so I prefer to remind myself of
that fact by invoking them differently. This helps me to
avoid falling into the many traps that lie in wait for
those who mistakenly think preprocessor commands are
C statements.
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.
Yes, it's legal, but I prefer not to leave extra semicolons
dangling about. Less cleanup work for the compiler to do
that way.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Apr 10 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Dan Perl | last post by:
Is there a mechanism or an idiom for adding code for debugging so that it can easily be removed in the production code? I am thinking of something similar to the C/C++ preprocessor statements with...
1
by: Robert | last post by:
Simple way of writing debug print statements in your JavaScript code. You need to have enabled popups in your browser. I liked the ability to write to the java console in Netscape 4.x. This,...
3
by: Thomas Lorenz | last post by:
Hello NG, I'm working on a larger C++ project with it's own debug functionality. The typical debug call looks like this: DEBUG(WARNING, "An error " << getErrorCategory() << " occured."); ...
2
by: Eric Mathieu | last post by:
Hi ! I have a program with almost 100 sources. The program coredump so i want debug the memory and i try memwatch but i don't get the result i want. The program crash so memwatch don't...
7
by: Srinivasa Rao | last post by:
I have read in one article that when we compile the application in release mode, all the debug classes and properties will be automatically removed from the code. I tried to implement this thing by...
5
by: MLH | last post by:
I use a fair number of debug.print statements. My apps are fairly well 'peppered' with them. I've often wondered if leaving them in the mdb, creating and rolling out an mde intended for use in the...
6
by: pauldepstein | last post by:
To help me debug, I am writing a lot of information into a stream which I call debug. However, because of the large amount of time taken to print this information, I only want this printed while...
0
by: BA | last post by:
I posted on this once before and could not get a solution, I am hoping someone can help. I have a very strange code debug behavior that I cannot make heads or tails of: I have c# code being...
2
by: mthread | last post by:
Hi, I am new to C++, But has good experience in C. In C I have used macros to enable / disable debug statements in the code. For ex : #if defined ENABLE_DEBUG #define Printf1(arg) printf(arg)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.