ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to write a wrapper around an API that prints out a message containing the name of the API function invoked, the list of arguments passed, and the return value of the call. I'm wondering if it would be possible to do this a little bit more intelligently than I am right now (I'm currently writing a wrapper function for each API function). Each wrapper function pretty much does the same thing (with a few special case exceptions):
1) Print the name of the API function being called and the values of each argument
2) Call the API function
3) Print the return value of the function
4) If the return value is now what we expected to receive, print an error message
So I can't help but feel there's a better way to do this. Maybe by providing a single generic function for doing the above steps for any API function called. Does anyone have any clever ideas about this? I've been thinking about possible approaches but haven't come up with any good leads. Also I'm trying to avoid unnecessary dependencies in this code (ie, no boost). I'm not sure if there's an answer for this or not, but thanks for any insight you can share with me.
I believe C++ doesn't cater for what you ask; however, certain compilers supply MACROs which maybe of use. GCC provides three magic variables which hold the name of the current function, as a string. These are:
If you wrote the API then you could use variables in gcc such as __FUNCTION__ and __PRETTY_FUNCTION__ and on windows the __FUNCTION__ macro. If you did not write the API then I would say you would have to use macros to generate code for you as a proxy function or to generate a template function. The following is untested yet should work, yet it assumes that the parameters and results can be inserted to a stream and they are not member functions.
There are problems with the sample code I posted such as not being able to detect pointers to types instead of types, not using epsilon range checking for floating point types, can not use member functions etc etc these can all be solved but more information would be required such as but not limited to:
How is the api linked?
Is it a c or c++ api?
what types are the parameters?
Do you have a functor library, if not could you not use loki's(headers only)?
Because I am nosey which API is it?
Sorry I've been busy the past few days and haven't gotten around to reading this thread. Thanks for the responses.
Quote:
Originally Posted by dmail
How is the api linked?
Is it a c or c++ api?
what types are the parameters?
Do you have a functor library, if not could you not use loki's(headers only)?
Because I am nosey which API is it?
- dynamically linked I believe
- C
- mostly integer types and pointers. No floating point
- I can't add any library dependencies
The API is device driver software for VXI controllers. The code I'm writing has to work for Windows, Linux, OS X, Solaris, VxWorks, Pharlap, and possibly other OSes. Of course, this means it also needs to work with various compilers. I've decided to go ahead and just do it the tedious manual way, as there are some specific scenarios where I want the wrapper function to do something atypical. Thanks for the help though
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.