How to output the name of an input variable [closed]
In writing a program, I'm having some problems to I was writing a little code to debug it. I have a number of links going on in the program, and I believe one of the problems is in the links, so I tried to write a program to output the name of the variables that failed to link. My efforts have failed, and came here for help. Here is my code:
GstElement element1, element2; //this is debugging a gstreamer program
void testforfailu开发者_如何学Cre(element1, element2)
{
if(!gst_element_link(element1, element2))
{
printf("linkage of");
printf(element1); //I want this to be the name of the element calling the function
printf(" and ");
printf(element2);
printf(" failed.");
return -1;
}
}
Example call:
GstElement *source, *sink;
testforfailure(source, sink); //if link(source, sink) fails, then this should output "linkage of source and sink failed"
If anyone could help me with this problem, that would be great.
EDIT: After seeing that it is not possible, I devised a simple solution:
testsignalforfailure(GstElement *elem1, GstElement *elem2, int id, int id2)
{
if(!gst_element_link(elem1, elem2))
{
g_print("linkage of %d and %d failed", id, id2);
return -1;
}
}
When called with "testsignalforfailure(source, sink, 1, 2)", this gives me the unique id (self-defined) of the elements that failed to link (I declare an id to each element before running). Thanks for everyone's help.
just to be clear: You have a program that is running. Sometimes calls to a particular function is failing.
However you don't know WHAT is calling that function; hence the desire for the variable names being used(?). I can't for the life of me figure out what other purpose you would possibly have for getting those names.
It's not going to be possible to get the names of the variables passed in, which won't really tell you anything anyway. So I'd suggest that you need to get the call stack of the error to know where this is coming from. Quite frankly those "names" are just syntax for the developers and the compiler, unless debug symbols are included, might change them anyway.
A better solution would be to set a breakpoint on the function and watch everything passed into it. Basically, use your debugger.
You can do something with macros. To be honest, I don't exactly know what you want, but you can debug as follows:
#define debug(x) printf(#x ": %s\n", x);
int main()
{
char *a="foo", *b = "bar";
debug(a);
debug(b);
}
It should be easy to adapt this to your needs.
I guess you want to get print-out of the string contents of the variables.
In your code snippet, you are using print with your variables as the first argument, which must be a format string.
This kind of practice seems have no problem at first, but if your variable contains formatting escape character '%', printf() will look for more arguments following, while you give only one argument, and lead to program crash.
Try change your printf statements as following:
printf("linkage of %s and %s failed.\n",element1,element2);
精彩评论