Here are some bugs:
TRC_DP(fmt, args...)
is not valid standard C.
- The icky do-while(0) trick assumes that there is no trailing semi-colon.
- You must always check if
fopen
was successful.
Something like this might solve the problems:
#define TRC_DP(fmt, ...) \
do {\
FILE * fp = fopen("/home/debug.log","a+");\
if(fp != NULL) { \
fprintf(fp,"TRC_DP(%s:%d):\t" fmt, __func__, __LINE__, __VA_ARGS__); \
printf(fmt, __VA_ARGS__);\
fclose(fp);\
} \
}while(0)
However, this is some seriously ugly code. You should replace this with a function and concatenate the format strings in run-time, if you must.
Overall, you should avoid variable argument functions or macros whenever possible. They are not only ugly but also very dangerous. The presence of variable arguments is usually a very strong indication of poor program design.