0

I'm using this simple macro for logging events of my program:

#define _ERROR(format, args...)  \
    { \
    time_t t = time(0);\
    struct tm * now = localtime( & t );\
    fprintf(stderr, "[ERROR %d-%d-%d %d:%d:%d]: ",now->tm_year + 1900,\
            now->tm_mon + 1, now->tm_mday,\
            now->tm_hour, now->tm_min, now->tm_sec); \
    fprintf(stderr, format , ## args);\
    printf("\n");\
    fflush(stderr);\
    }

It compiles happily on Linux and Windows using GCC and MinGW. Though MSVC 11 fails to compile.

Note: I'm experiencing difficulties understanding this and this answers on previous SO questions. I think I'm missing something about macros and/or variadic argument count. So:

  1. What is __VA_ARGS__? Do I have to use it?
  2. Do I have to use it only for MSVC-compilable code?
  3. Is above code wrong / none-standard / inelegant because of lack of those __VA_ARGS__ staff?
Community
  • 1
  • 1
sorush-r
  • 10,490
  • 17
  • 89
  • 173
  • How does that compile in GCC? Perhaps it's that I compile pedantically, but named variadic macros are not standard. It also uses a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Jul 24 '13 at 09:54
  • That's a lot of code for a macro. Why not just a create a global `printerror(const char *fmt, ...)` function and use that instead? – trojanfoe Jul 24 '13 at 09:58
  • @chris What you mean by __how__? It compiles normally. Now warnings, no errors. Tested compilers are g++ 4.6.3 on Ubuntu and g++ 4.8.0 on Windows (MinGW). – sorush-r Jul 24 '13 at 09:58
  • @soroush, http://coliru.stacked-crooked.com/view?id=a83b963c515f4f5f6e2ee8abd64156da-3b440a87a52fe2ae7c853c82f4c5144f – chris Jul 24 '13 at 09:59
  • @trojanfoe Because I'm so lazy to change old codes :) – sorush-r Jul 24 '13 at 09:59
  • You don't have to; `#define _ERROR printerror`. – trojanfoe Jul 24 '13 at 09:59
  • Any refactoring tool should be able to rename the uses of it. – chris Jul 24 '13 at 10:01
  • @chris There are lots of changes needed. But currently I just need to a working library on Windows, that can be linked against an MSVC-compiled executable. It's strange that variadic argument counts for macros are not allowed with this online compiler. It can be switched on with compiler flags I think. – sorush-r Jul 24 '13 at 10:04
  • @soroush, The point is, it's not standard, so you shouldn't be surprised to hear it doesn't work with a different compiler. What's wrong with using `__VA_ARGS__`? – chris Jul 24 '13 at 10:06
  • @chris I don't know what is `__VA_ARGS__`. Also I found that `-pedantic-errors` flag prevents GCC from compiling VAC for macros ([See here](http://coliru.stacked-crooked.com/view?id=1bb7208622c84bb2cf534f99dcc5e395-df8dcf008f5684150551e8d7e183dfe4)). So I'm gonna get my hands dirty and change codes. – sorush-r Jul 24 '13 at 10:08
  • @soroush, It's the solution for using a variadic macro when you have no name to use. It subs in as the variadic arguments. – chris Jul 24 '13 at 10:12
  • @soroush It's almost the same exact code - http://coliru.stacked-crooked.com/view?id=39bd749007440dff940ce8ed828e912c-3b440a87a52fe2ae7c853c82f4c5144f – catscradle Jul 24 '13 at 11:24
  • @catscradle still not working in MSVC. I realized that Visual Studio 2012 + November CTP even can't compile initializer lists and deleted functions. I have a couple of lexer/parser pairs generated with flexc++/bisonc++ which there is a lot of C++11 code in them. So it seems MSVC is far from being a decent C++ compiler. I'll use MinGW instead. – sorush-r Jul 24 '13 at 12:48

0 Answers0