Reputation: 3691
I'm trying to know in a function its caller name.
If you look to the following link, it's not duplicated because I add a difference: the ellipsis usage in function declaration.
I've tried to do, starting from this solution How can we know the caller function's name? to do that, but I cannot get the solution.
This works to me:
void a(int deb, char *str)
{
printf("%s\n", str);
}
void a_special(int deb, char const * caller_name, char *str)
{
printf( "[%d] blablabla [%s] ", deb, caller_name);
a(deb, str);
}
#define a(deb, str) a_special(deb, __func__, str)
int main()
{
a(1, "my log");
return 0;
}
But when I add the ellipsis (I say: "...") I don't know how to achieve it with the macro definition. Is possible in standard-C?
void a(int deb, char *str, ...)
{
va_list args;
va_start(args,str);
vprintf(str,args);
va_end(args);
}
void a_special(int deb, char const * caller_name, char *str, ...)
{
printf( "[%d] blablabla [%s] ", deb, caller_name);
a(deb, str, ...);
}
#define a(deb, str) a_special(deb, __func__, str)
int main()
{
a(1, "mylog %d %s", 1, "param2");
return 0;
}
I've also tried to get it using backtrace compiling with -rdynamic without success, but anyway I'd prefer to know how to include ellipsis (3 dots) in macro. Thanks in advance!
Upvotes: 7
Views: 377
Reputation: 3461
If all you are asking for is how to forward the ellipses on to the macro and then from the macro to the function, then the following should be sufficient.
Basically, you pass the macro also the ellipses ...
and inside the macro you can use __VA_ARGS__
.
void a(int deb, char *str, ...)
{
va_list args;
va_start(args,str);
vprintf(str,args);
va_end(args);
}
void a_special(int deb, char const * caller_name, char *str, ...)
{
printf( "[%d] blablabla [%s] ", deb, caller_name);
a(deb, str);
}
#define a(deb, str, ...) a_special(deb, __func__, str, __VA_ARGS__)
int main()
{
a(1, "mylog %d %s", 1, "param2");
return 0;
}
Upvotes: 7