Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!ames!ucbcad!ucbvax!hplabs!cae780!leadsv!scampi!ramin From: ramin@scampi.UUCP Newsgroups: comp.lang.c Subject: Re: Can a subfunction find the name of the calling function? Message-ID: <185@scampi.UUCP> Date: Tue, 5-May-87 19:35:12 EDT Article-I.D.: scampi.185 Posted: Tue May 5 19:35:12 1987 Date-Received: Fri, 8-May-87 01:58:29 EDT References: <12620001@acf4.UUCP> Organization: Systems Control, Palo Alto, CA Lines: 83 Summary: Here's one way... In article <12620001@acf4.UUCP>, woo@acf4.UUCP (Alex C. Woo) writes: > > Is there anything comparable to getarg and iargc for subroutines > or functions? How can a function determine the name of the function > which calls it or the number of calling arguments, either in C > or f77? > > Alex Woo Here's one way of getting the function name. It might not appeal to some people's aesthetic, but it works... (I recall seeing something like it in GNU emacs in the form of DEFUN but it's been a while (:-) (more commentary at the end...) *************************************************************************** #define CALL1(func,a) func("func",a) #define CALL2(func,a,b) func("func",a,b) #define CALL3(func,a,b,c) func("func",a,b,c) #define SUB1(func,a) func(name,a) char *name; main() { CALL3(first,1,2,3); CALL1(foo,"this is a parameter"); } first(name,x,y,z) char *name; int x,y,z; { printf("%s - %d - %d - %d\n",name,x,y,z); } SUB1(foo,text) char *text; { printf("%s -- %s\n",name,text); } *************************************************************************** It was used in one project to print out debugging and manual traceback information out... It was actually used within the following context... #ifndef TRACEBACK #define CALL1(func,a) func(a) #else #define CALL1(func,a) log_name(func); func("func",a) #endif Where log_name would be a function that would write something like "Entering function: foo at [time/date]" to a temporary log-file. Taking out TRACEBACK would just force cpp to generate normal code without all the strings and function calls... In fact, the source to the log_name function would not even be #include'd if TRACEBACK was undefined... It's a fairly painless insurance policy for way down the line... Nobody complained after the first time they checked out the log-file to see where their programs had gone to pieces. Especially when you get one of those intermittent bugs that happen after 8 hours of crunching in real-time... (especially if it doesn't happen in BSD (Ultrix) but does in VMS... Yow!) Most people, in fact, customized the macro's to print out further logging info... some got carried away... But that's another story (;-) This stuff was tested on Ultrix, Sun, and VAX/VMS... Your mileage may vary... caveat emptor... 'nuff said... r... -- (insert-file ".disclaimers") ramin firoozye' {ihnp4,lll-lcc,hoptoad}!scampi!ramin systems control inc. (415) 494-1165 x-1777 1801 page mill road palo alto, ca 94304 *** Wars are not fought to decide who is right... Only who is left... ***