On Wed, 23 Dec 1998, James [on his mailserver] wrote:

> i'm writing a program and have created a generic error routine that writes
> text to stderr called warn() which takes a parameter of type char* which
> is the error to display, it then writes out 'Warning:
> [whatever-char*-was]'. What i'd like to do is have it prefix it with the
> name of the function that called it (i.e so if i was in some func called
> foo() and it called warn() the error would look like this : 'foo()
> Warning: blah').
> 
> this possible?... without having to manually write the name of the
> function in myself?
 
First you need an array of structures containing the address and
length of functions that may call such a bitch, and the ASCII name 
of that said function. Then you have to know how the nasty little 
routine is going to be called. Find out where the return address is 
stored and calculate which function called it. Then search the 
array.

If you're got time to do something like this, you have too much
time on your hands. It will also not be portable and require a shit
load of debugging to get things right. 


You're much better off doing it the Right Way<tm>, use something
like:

#define fukd_up(X) fprintf(stderr, "Error:%s\n.In file %s ~line %d\n", \
        X, __FILE__, __LINE__);

Example of use:

if(read(FD, NULL, 0))
        {
        fukd_up("read() failure");
        return -1;
        }

The out put is also more useful than just having a function
name, especically if you start writing large or lots of 
source files.


  • Errors James [on his mailserver]
    • Ken Dunn

Reply via email to