Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question on: debug printf macro

222 views
Skip to first unread message

Enrico

unread,
Oct 28, 2011, 2:59:21 AM10/28/11
to
Hi,

I'm writing a C program and I would like to know if the following
thing is feasable?


1) My program is made of 10 files.


2) Each module can print a debug string over the standard output. For
example:

debug_printf (MODULE_1, "The value of temperature is %d",
temperature);
debug_printf (MODULE_2, "I received the following char - %c", ch);
debug_printf (MODULE_3, "Ok, I'm done");


3) I want to be able to:

- switch off all the printf call. That means that the "debug_printf"
call must be removed by the preprocessor
- switch on selected printf call


Thanks in advance,
Enrico Migliore
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

fasked

unread,
Oct 28, 2011, 3:19:13 PM10/28/11
to
> - switch off all the printf call. That means that the "debug_printf"
> call must be removed by the preprocessor

Just make `debug_printf' an macros.

#ifdef DEBUG_LOG
#define debug_printf(module_id, fmt, ...) \
fprintf(stderr, "[module: %d]: "fmt, module_id, ##__VA_ARGS__);
#else
#define debug_printf(module_id, fmt, ...)
#endif

Define the `DEBUG_LOG' if you want to use debug logging or undef it
otherwise. In second case `debug_printf' macros will expanded as
nothing :) You can test it if you will run just only preprocessor.

> - switch on selected printf call
What do you mean by `switch on selected'? Any example please.

Thomas Richter

unread,
Oct 28, 2011, 3:19:43 PM10/28/11
to
On 28.10.2011 08:59, Enrico wrote:

> 2) Each module can print a debug string over the standard output. For
> example:
>
> debug_printf (MODULE_1, "The value of temperature is %d",
> temperature);
> debug_printf (MODULE_2, "I received the following char - %c", ch);
> debug_printf (MODULE_3, "Ok, I'm done");
>
>
> 3) I want to be able to:
>
> - switch off all the printf call. That means that the "debug_printf"
> call must be removed by the preprocessor
> - switch on selected printf call

A typical solution to such problems is to define a macro that, depending
on debugging support turned on or off expands to its argument or nothing:

#ifdef DEBUG
#define D(a) a
#else
#define D(a)
#endif

Then, use the following in your program instead:

D(printf (MODULE_2, "I received the following char - %c", ch));

HTHH,
Thomas

James Kuyper

unread,
Oct 28, 2011, 3:20:13 PM10/28/11
to
On 10/28/2011 02:59 AM, Enrico wrote:
> Hi,
>
> I'm writing a C program and I would like to know if the following
> thing is feasable?
>
>
> 1) My program is made of 10 files.
>
>
> 2) Each module can print a debug string over the standard output. For
> example:
>
> debug_printf (MODULE_1, "The value of temperature is %d",
> temperature);
> debug_printf (MODULE_2, "I received the following char - %c", ch);
> debug_printf (MODULE_3, "Ok, I'm done");
>
>
> 3) I want to be able to:
>
> - switch off all the printf call. That means that the "debug_printf"
> call must be removed by the preprocessor
> - switch on selected printf call

Define the actual function to be real_debug_printf().

#ifdef DEBUG_PRINTF
#define debug_printf(args) real_debug_printf(args)
#else
#define debug_printf(args)
#endif

To switch a particular call on, when all of the others are turned off by
the above method, then just add the "real_" prefix.
--
James Kuyper

Dag-Erling Smørgrav

unread,
Oct 28, 2011, 3:20:44 PM10/28/11
to
Enrico <zig...@libero.it> writes:
> 2) Each module can print a debug string over the standard output. For
> example:
>
> debug_printf (MODULE_1, "The value of temperature is %d", temperature);
> debug_printf (MODULE_2, "I received the following char - %c", ch);
> debug_printf (MODULE_3, "Ok, I'm done");
>
> 3) I want to be able to:
>
> - switch off all the printf call. That means that the "debug_printf"
> call must be removed by the preprocessor
> - switch on selected printf call

Something like

int real_debug_printf(int, const char *fmt, ...);
#ifndef NDEBUG
#define debug_printf(module, ...) \
real_debug_printf((module), __VA_ARGS__)
#else
#define debug_printf(module, ...) 0
#endif

DES
--
Dag-Erling Smørgrav - d...@des.no

Hans-Bernhard Bröker

unread,
Oct 28, 2011, 3:21:44 PM10/28/11
to
On 28.10.2011 08:59, Enrico wrote:

> - switch off all the printf call. That means that the "debug_printf"
> call must be removed by the preprocessor
> - switch on selected printf call

If that's what you want to do, then you cannot implement your debug
printf the way you planned. You need

DEBUG_PRINTF_MODULE1(("The value of temperature is %d", temperature));

instead, so you can then

#ifdef DEBUGGING_MODULE1
#define DEBUG_PRINTF_MODULE1(args) printf(args)
#else
#define DEBUG_PRINTF_MODULE1(args) /*nothing*/
#endif

Note that I'm assuming no C99, so you need the double () trick to feed
variable argument lists through a macro.

Dag-Erling Smørgrav

unread,
Nov 2, 2011, 1:49:42 PM11/2/11
to
James Kuyper <james...@verizon.net> writes:
> #ifdef DEBUG_PRINTF
> #define debug_printf(args) real_debug_printf(args)
> #else
> #define debug_printf(args)
> #endif

That won't work. He needs a macro that takes at least two arguments:
the module ID and the format string plus zero or more substitution
parameters.

DES
--
Dag-Erling Smørgrav - d...@des.no
0 new messages