Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How can I include expansions of the macros .....
How can I include expansions of the __FILE__ and __LINE__ macros in a general-purpose debugging macro?
✍: Guest
One solution involves writing your debug macro in terms of a varargs function , and an auxiliary function which stashes the values of __FILE__ and __LINE__ away in static variables, as in:
#include <stdio.h>
#include <stdarg.h>
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 pointer to the bona-fide varargs function:
void debug(const char *, ...);
void (*dbginfo(int, const char *))(const char *, ...);
#define DEBUG (*dbginfo(__LINE__, __FILE__))
void (*dbginfo(int line, const char *file))(const char *, ...)
{
dbgfile = file;
dbgline = line;
return debug;
}
With these definitions,
DEBUG("i is %d", i);
gets expanded to
(*dbginfo(__LINE__, __FILE__))("i is %d", i);
Another, perhaps easier way might simply be to
#define DEBUG printf("DEBUG: "%s", line %d: ", \
__FILE__,__LINE__),printf
Now,
DEBUG("i is %d", i);
simply expands to
printf("DEBUG: "%s", line %d: ",
__FILE__,__LINE__),printf("i is %d", i);
Finally, you may be able to use the
#define _ ,
2016-01-15, 1670👍, 0💬
Popular Posts:
How To Merge Cells in a Row? - XHTML 1.0 Tutorials - Understanding Tables and Table Cells If you wan...
How To Enter Boolean Values in SQL Statements? - MySQL FAQs - Introduction to SQL Basics If you want...
What CLASSPATH Settings Are Needed to Run JUnit? It doesn't matter if you run your JUnit tests from ...
What will happen in these three cases? if (a=0) { //somecode } if (a==0) { //do something } if (a===...
Can Two Forms Be Nested? - XHTML 1.0 Tutorials - Understanding Forms and Input Fields Can two forms ...