C / C ++ How to extend a macro parameter into text between quotation marks

Here is what I have (message () - a specialized logging function from a third-party library):

#define LOG(fmt, ...) message("%s %s(): #fmt", __FILE__, __func__, __VA_ARGS__);

So, I want to be able to do things like:

LOG("Hello world")
LOG("Count = %d", count)

And expand it:

message("%s %s(): Hello world", __FILE__, __func__);
message("%s %s(): Count = %d", __FILE__, __func__, count);

But the #fmt function does not work. It does not evaluate the macro argument and prints as "#fmt". Can I do what I'm trying to do?

+3
source share
1 answer

Do not put #fmtin quotation marks. Just use string literal concatenation to join the two literals.

#define LOG(fmt, ...) message("%s %s(): " fmt, __FILE__, __func__, __VA_ARGS__);
+5
source

All Articles