Linux has this nice function dprintf
:
The functions
dprintf()
andvdprintf()
(as found in the glibc2 library) are exact analogues offprintf()
andvfprintf()
, except that they output to a file descriptor fd instead of to a given stream.
however as that same source points out:
These functions are GNU extensions, not in C or POSIX. Clearly, the names were badly chosen. Many systems (like MacOS) have incompatible functions called
dprintf()
, usually some debugging version ofprintf()
, perhaps with a prototype like
void dprintf (int level, const char *format, ...);
where the first parameter is a debugging level (and output is to
stderr
). Moreover,dprintf()
(orDPRINTF
) is also a popular macro name for a debuggingprintf
. So, probably, it is better to avoid this function in programs intended to be portable.
My question
How can I make a setup that will safely call the dprintf
that I want, if it exists, or fail to compile with some reasonably sane error message if that function doesn't exist? I guess I'd do something like this:
#ifdef SOMETHING
#define Dprintf dprintf
#else
#error "no dprintf"
#endif
but I don't know what SOMETHING
should be. I guess I could restrict it to just Linux but could I make it looser?
(_POSIX_C_SOURCE - 0) < 200809
so that it's still a valid (false) expression even when the macro is undefined or empty. – Upon