LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Detecting the name of the callback or function executing

In my applications, I often add warning messages for sw or application errors. The messages have always this format:

Error [code] received in [functionname] (line xx):
[description of the error]

Is it possible to automatically detect the name of the callback or user-defined function executing (something as the __LINE__ macro to detect the line in the source code)?
This could be helpful to automate the warning function instead of manually writing the message.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 1 of 6
(2,829 Views)
I don't think there is any built-in way of getting the function name. You could do it (rather messily) using a macro such as:

#define FUNCTION_WITH_NAME(a,b,c) a b c \
{ static const char FUNCNAME[] = #b


And then invoke it something like this:

FUNCTION_WITH_NAME(
int CVICALLBACK,
OKButton,
(int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
);
// Note, no '{' required here!
switch (event)
{
case EVENT_COMMIT:
MessagePopup("Function name is...",FUNCNAME);
QuitUserInterface (0);
break;
}
return 0;
}


--
Martin
--
Martin
Certified CVI Developer
0 Kudos
Message 2 of 6
(2,829 Views)
Not that I am aware of, but there is one to tell you in which file you are: __FILE__

The C language does not have the RTTI of C++

I am using this a lot in one application under CVI 6.0.
But be aware of on thing, this is valid only from the current function like __LINE__

behind you should treat __FILE__ as a null terminated string : char *


For example:


void main(void)
{
DisplayErrorMessage(__FILE__, __LINE__, "Error Msg");
}

void DisplayErrorMessage(char * SourceFile, int SourceLine , char * Msg)
{
DebugPrintf("Msg=%s",Msg);
DebugPrintf("File=%s",SourceFile);
DebugPrintf("Line=%d",SourceLine );
}

Best Regards,
Farid.
0 Kudos
Message 3 of 6
(2,829 Views)
The __FUNCTION__ macro returns a string with the function name. I learned this from the CVI Tip of the Day and I started using it immediately!
Message 4 of 6
(2,829 Views)
Thanks, Al S: it was so so easy!

BTW, where can be found a list of all pre-defined macros like __FILE__ and __FUNCTION__? Guess there's a lot of useful functions that are waiting only for us to know them...


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 6
(2,829 Views)
__FILE__ and __LINE__ are part of ANSI C, but I don't think __FUNCTION__ is. I can't find any CVI documentation for these macros, though there is plenty of info on the web (once you know what you are looking for).

--
Martin
--
Martin
Certified CVI Developer
0 Kudos
Message 6 of 6
(2,829 Views)