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!
The C Caller supports code generation. In the code generated from your model, each execution of a C Caller block corresponds to a call to the external C function associated with the block.
A Function object's caller property returns the function that invoked the specified function. For strict, async function, and generator function callers, accessing the caller property throws an exception.
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;
}
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