Is there a cross-platform approach to wrapping fprintf()
so I that I can have a simple logging function for dumping logs to either files or the console, and utilizes a simple C printf()
style format string and arguments list?
Right now, I'm doing something like so:
#define logIssue(fmt, ...) do { \
fprintf(stderr, "MY_PREFIX:%s:%s:%d > " fmt "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
} while(0);
This works in GCC, and if I remove the leading ##
, it works in GCC and Visual Studio 2005 (yes, I have to have this working on VS2005 through 2015), but it fails in Visual Studio versions prior to 2012 when I don't actually have a variable arguments list, ie:
logIssue("Problem with test#%d.", iTestID); // Succeeds
logIssue("General problem."); // Fails
I've read into this issue at length, including the following:
- Standard alternative to GCC's ##VA_ARGS trick?
- MSVC doesn't expand VA_ARGS correctly
- the problem about different treatment to VA_ARGS when using VS 2008 and GCC
These offer potential solutions, but I can't seem to integrate them with my fprintf()
macro.
Is there any straightforward way to have this wrapper macro work in Linux (GCC) and Windows (MSVC 2005 and onward) via macro tricks?
Thank you.