0

The best way is to show it with code. I'm compiling this as a Win32 C++ project under Visual Studio 2017:

#include <strsafe.h>
#include <stdio.h>

//Trace outout
#ifdef _DEBUG
#define TRACE(s) ::OutputDebugString(s)
#define TRACE(s, ...) \
    { WCHAR __dbg_buff[1024]; if(SUCCEEDED(::StringCchPrintf(__dbg_buff, _countof(__dbg_buff), s, __VA_ARGS__))) { ::OutputDebugString(__dbg_buff);} else ASSERT(NULL);}
#else
#define TRACE(s) ((void)0)
#define TRACE(s, ...) ((void)0)
#endif

The idea is to use the macro in these two types of cases:

TRACE(L"value=%02X", v);
TRACE(L"\n");

But my macro definition produces this error:

warning C4005: 'TRACE': macro redefinition
note: see previous definition of 'TRACE'
error C2059: syntax error: ')'

What is the way to define it so that it takes both variadic and non-variadic form?

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • 1
    You can't overload macros. I recommend that you redesign logging and tracing using C++ funcitonality instead, perhaps using stream-like syntax with `<<`. – Some programmer dude Jul 09 '19 at 07:16
  • Why you need 2 versions? 1st version is just special case of 2nd anyway... I.e. remove `s` from arguments and leave only 2nd version. – sklott Jul 09 '19 at 09:04
  • @sklott: With just the 2nd version if I do `TRACE(L"\n");` it will not compile. – c00000fd Jul 09 '19 at 09:12
  • I don't have VC to check, but for GCC something like `#define TRACE(...) printf(__VA_ARGS__)` works without problems. – sklott Jul 09 '19 at 10:23

1 Answers1

0

It seems that second defintion of TRACE macro redefines the first one, so effectively you have TRACE(x, ...) defined eventually.

Then to use VA_ARGS it may be necessary to use it with double '#' prefix. e.g.

#define TRACE(fmt, ... )   printf(fmt, ##__VA_ARGS__)

Works fine to me with gcc.