Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get caller function name in C with ellipsis in function declaration?

Tags:

c

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!

like image 617
Alejandro Galera Avatar asked Jan 09 '19 13:01

Alejandro Galera


People also ask

What is the caller in C?

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.

What is the caller of a function?

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.


1 Answers

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;
}
like image 69
Duck Dodgers Avatar answered Dec 08 '22 09:12

Duck Dodgers