Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can I include expansions of the __FILE__ and __LINE__ macros in a general-purpose debugging macro?

0
10 Posted

How can I include expansions of the __FILE__ and __LINE__ macros in a general-purpose debugging macro?

0
10

This question tends to reduce to question 10.26. One solution involves writing your debug macro in terms of a varargs function (see questions 15.4 and 15.5), and an auxiliary function which stashes the values of __FILE__ and __LINE__ away in static variables, as in: #include #include void debug(const char *, …); void dbginfo(int, const char *); #define DEBUG dbginfo(__LINE__, __FILE__), debug static const char *dbgfile; static int dbgline; void dbginfo(int line, const char *file) { dbgfile = file; dbgline = line; } void debug(const char *fmt, …) { va_list argp; fprintf(stderr, “DEBUG: \”%s\”, line %d: “, dbgfile, dbgline); va_start(argp, fmt); vfprintf(stderr, fmt, argp); va_end(argp); fprintf(stderr, “\n”); } With this machinery in place, a call to DEBUG(“i is %d”, i); expands to dbginfo(__LINE__, __FILE__), debug(“i is %d”, i); and prints something like DEBUG: “x.c”, line 10: i is 42 A cunning improvement is the idea of having the stashing function return a p

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123