EDIT: The code marked as "not working" was actually working. It was because of a syntax problems in my tests, not detected by the compiler. So the question is already solved, thank you.

C++ is not a language I use everyday, so it is possible that the solution is trivial.

About the context first. I use C++ to develop on a microcontroller (Arduino-based, AVR microcontroller), so I do not use the STL, printf-like functions, new/malloc should be avoided and C++ <string> too.

I have an object called Serial similar to the C++ cout iostream, to communicate with the microcontroller with a serial interface. I have overloaded the "<<" operator of the class from which Serial is an instance so I can do something like that:

This kind of function/macro would allow me to easily output strings on my serial interface with a specific "string protocol" without having to repeat the "debug" string at the start. It would also allow me to easily disable the print of debug message by not setting the DEBUG macro. I also have only one "#ifdef DEBUG" instead of several ones in my code.

I managed to do something like that with variadic arguments, but I hate this solution because it is dangerous to use (I do not want to specify the number of arguments), and I cannot mix different type of data:

@J.N: 2 problems for printf: first it adds lots of weight in the binary (I work on a microcontroller), and second it is not compatible with a "String" object provided by the Arduino library. The "Serial" object already knows how to process Arduino String, char, int, ...
–
Vincent HiribarrenFeb 22 '12 at 13:26

1

I checked PRINT_DEBUG() macro appraoach and it worked fine on VS2010 (qualifing std::endl and using std::cout were the only changes I made.)
–
hmjdFeb 22 '12 at 13:27

2

I'm almost inclined to say "your operator<< has some problem" (without ever having seen it). The reason for that is that a macro does nothing but paste text, and if you manually copy/paste the text in the "does not work" example above (substituting data with `"sensor1 value:" << value1 << " other sensor value " << value2´, you get a line that should very clearly work.
–
DamonFeb 22 '12 at 13:29

Interesting, I also prefer avoiding the use of macro, but I am not comfortable enough with templates to have this kind of solution in mind. The only thing I do not like is that this solution does not append automatically std::endl at the end of a chain of "<<". Is it possible without macros?
–
Vincent HiribarrenFeb 22 '12 at 14:09

@vincent No, I don't know of any way to automatically append std::endl at the end with this approach.
–
FrancescoFeb 22 '12 at 14:43

If I understand your problems correctly...
Looks to me like you need to overload operator<< for all types you're going to send to the debug interface.

The var args macro has to have a way to deduce the types of its arguments. The way you have it implemented it's expecting all C type strings. You'd be better off with printf or a library like fastformat.

If your operator<< is not returning a reference to the class that allows operator<< to be chained, you'll get errors like "I have the following error for the line "DEBUG("hello" << " " << "world");" : invalid operands of types 'const char [6]' and 'const char [2]' to binary 'operator<<' ". I do not believe DEBUG("hello" << " " << "world"); can be made to work. DEBUG( "hello", "world"); might work.