Is this a correct print macro?

D

dspfun

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
 
J

Joel C. Salomon

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
 
S

Seebs

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
 
J

Joel C. Salomon

#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
 
J

Joel C. Salomon

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");
}
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top