473,229 Members | 1,760 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,229 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 4263
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.