[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Is this a correct print macro?

dspfun

5/17/2011 12:51:00 PM

Hi,

Is the following print macro "DEBUGPRINT" ok?

// debugging macros so we can pin down message origin at a glance
#define WHERESTR "[file %s, line %d]: "
#define WHEREARG __FILE__, __LINE__
#define DEBUGPRINT2(...) fprintf(stderr, __VA_ARGS__)
#define DEBUGPRINT(_fmt, ...) DEBUGPRINT2(WHERESTR _fmt, WHEREARG,
__VA_ARGS__)

Brs,
Markus
4 Answers

Joel C. Salomon

5/17/2011 5:47:00 PM

0

On May 17, 8:50 am, dspfun <dsp...@hotmail.com> wrote:
> Is the following print macro "DEBUGPRINT" ok?

What do you mean by "ok"? It works, if that's what you mean.

If you're using C99, you might also want to add __function__ to the
debug output.

Given the output format, each DEBUGPRINT belongs on its own output
line -- so don't make the user include the newline.

Also note that you cannot use this version without additional
arguments; e.g.,
DEBUGPRINT("got here, no variables to display\n");
is invalid.

Here's some code I've used to accomplish much the same idea:

/* debug_print.h */
#ifndef DEBUG_PRINT_H
#define DEBUG_PRINT_H

extern void debug_print(char const *file, long line, char const *func,
char const *fmt, ...);

#define debug_print(...) \
debug_print(__FILE__, __LINE__, __func__, __VA_ARGS__)

#endif
/* end debug_print.h */

/* debug_print.c */
#include <stdio.h>
#include <stdarg.h>
#include "debug_print.h"

#undef debug_print

void
debug_print(char const *file, long line, char const *func,
char const *fmt, ...)
{
fprintf(stderr, "[%s:%ld, %s()] ", file, line, func);

va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);

fprintf(stderr, "\n");
}

/* end debug_print.h */

(Actually, my code style include saving argv[0] in a global variable,
and I include that as well.)

Share & enjoy.

--Joel

Seebs

5/17/2011 6:33:00 PM

0

On 2011-05-17, dspfun <dspfun@hotmail.com> wrote:
> Hi,
>
> Is the following print macro "DEBUGPRINT" ok?

Maybe, but... why?

> // debugging macros so we can pin down message origin at a glance
> #define WHERESTR "[file %s, line %d]: "

#define WHERESTR "[file " __FILE__ ", line " ...

For fun, figure out how to get the line number in there as a string so
you can use string concatenation instead of formatting.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Joel C. Salomon

5/17/2011 8:02:00 PM

0

On May 17, 2:32 pm, Seebs <usenet-nos...@seebs.net> wrote:
> #define WHERESTR "[file " __FILE__ ", line " ...
>
> For fun, figure out how to get the line number in there as a string so
> you can use string concatenation instead of formatting.

Of course, the expand-and-stringify trick doesn't help with __func__.

An advantage of Seeb's suggested version is that you can define the
macro to work on both these lines:

DEBUGPRINT("foo");
DEBUGPRINT("%s", "bar");

without using the oh-so-clever PP_NARG hack, and without defining an
extra function.

You need some extra cleverness to get rid of the need for the explicit
"\n" this way, though.

--Joel

Joel C. Salomon

5/17/2011 8:27:00 PM

0

On May 17, 4:01 pm, "Joel C. Salomon" <joelcsalo...@gmail.com> wrote:
> An advantage of Seeb's suggested version is that you can define the
> macro to work on both these lines:
>
>     DEBUGPRINT("foo");
>     DEBUGPRINT("%s", "bar");
>
> without using the oh-so-clever PP_NARG hack, and without defining an
> extra function.
>
> You need some extra cleverness to get rid of the need for the explicit
> "\n" this way, though.

I nerd-sniped myself; couldn't stop till I'd solved the problem. Both
versions below my signature; as before, share & enjoy.

--Joel


#include <stdio.h>
#include <stdarg.h>


extern void debug_print(char const *file, long line, char const *func,
char const *fmt, ...)
/* __attribute__((format(printf, 4, 5))) */;

#define debug_print(...) \
debug_print(__FILE__, __LINE__, __func__, __VA_ARGS__)


#define PP_STR_(x) #x
#define PP_STR(x) PP_STR_(x)

#define PP_LOCATION __FILE__ ":" PP_STR(__LINE__) ": "

#define DEBUG_PRINT(...) do { \
fprintf(stderr, PP_LOCATION __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while (0)


int
main(void)
{
debug_print("foo");
debug_print("%s", "bar");

DEBUG_PRINT("foo");
DEBUG_PRINT("%s", "bar");

return 0;
}


void
(debug_print)(char const *file, long line, char const *func,
char const *fmt, ...)
{
fprintf(stderr, "%s:%ld, %s(): ", file, line, func);

va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);

fprintf(stderr, "\n");
}