I have the following macro:
#define LOG(level,text)
I want to define macro which includes both level and text:
#define MY_LOG_MESSAGE LEVEL1,"This is my log"
so latter I can run:
LOG(MY_LOG_MESSAGE);
gcc issues preprocess error:
error: macro "LOG" requires 2 arguments, but only 1 given
Any ideas?
You have to convince the preprocesor to expand the MY_LOG_MESSAGE
macro before it tries to expand LOG()
. This can be done by using a simple helper macro:
#define LOG1(x) LOG(x)
LOG1(MY_LOG_MESSAGE);
The arguments given to LOG1()
are expanded in it's body, leading to a valid call for LOG()
.
This is very similar to sth's answer, but allows using either one or two parameters:
#define LOG_(level,text) implementation
#define LOG(...) LOG_(__VA_ARGS__)
#define MY_LOG_MESSAGE LEVEL1,"This is my log"
LOG(MY_LOG_MESSAGE);
LOG(LEVEL2, "Another log");
The point is that LOG
causes the parameter to be expanded before calling LOG_
, thus giving it two parameters in both cases.
If you use a define for each log message anyways, maybe do it like this:
#define LOG_MY_MESSAGE LOG(LEVEL1, "This is my log")
And use in code simply as
LOG_MY_MESSAGE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With