C++: Get Function's Name at Runtime? 
Author Message
 C++: Get Function's Name at Runtime?

    Hello... When designing my C++ applications, I sometimes like to write
events to an error log when unexpected things occur. One of the parameters
that I pass to my error handling function is the name of the currently
executing function -- so that when I review the error log I'll have some
context regarding the error. As it stands, I hardcode the function name into
my code; so, if the currently executing function is CMyObject::MyFunc(), I
will pass "CMyObject::MyFunc" as a string parameter to the error handler. So
my question is: Is there some C++/Win32 function that will return the name
of the currently executing function? This would make my code a bit cleaner,
and I wouldn't have to hardcode all of these string values into my app.

--
and his name was,

David Sworder
Software Development
Jack Carl Futures / ED&F Man



Fri, 01 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?


Fri, 19 Jun 1992 00:00:00 GMT  
 C++: Get Function's Name at Runtime?
Hi,
sorry, i don't know if it exists in VC++,
but i am sure it exists with g++

you may compile your project with cygnus, or g++/win32
you can use __FUNCTION__ to get function name
and __PRETTY_FUNCTION__ to get function prototype

N.Belan



Quote:
>     Hello... When designing my C++ applications, I sometimes like to write
> events to an error log when unexpected things occur. One of the parameters
> that I pass to my error handling function is the name of the currently
> executing function -- so that when I review the error log I'll have some
> context regarding the error. As it stands, I hardcode the function name
into
> my code; so, if the currently executing function is CMyObject::MyFunc(), I
> will pass "CMyObject::MyFunc" as a string parameter to the error handler.
So
> my question is: Is there some C++/Win32 function that will return the name
> of the currently executing function? This would make my code a bit
cleaner,
> and I wouldn't have to hardcode all of these string values into my app.

> --
> and his name was,

> David Sworder
> Software Development
> Jack Carl Futures / ED&F Man



Fri, 01 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?


Fri, 19 Jun 1992 00:00:00 GMT  
 C++: Get Function's Name at Runtime?
[snip]

Quote:
> Is there some C++/Win32 function that will return the name
> of the currently executing function? This would make my code a bit cleaner,
> and I wouldn't have to hardcode all of these string values into my app.

Unfortunately there is nothing in standard C++ that would get you the
name of the current function. However, the __FILE__ and __LINE__
keywords translate to a string literal containing the name of the
current file and an integer literal containing the line in the current
file, respectively.

Try the following code:

#include <iostream>
using namespace std;

int main() {
  cout << __FILE__ << '(' << __LINE__ << ")\n";
  return 0;

Quote:
}

---
Vesa Karvonen


Fri, 01 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?
Remember that at runtime the ONLY place function names could exist are in the
executable image IF debugging information was left in it. So you would have to
distribute the huge version of your executable as well. Otherwise how would a
Win32 API function know the name of your function? In DLLs those names exist in
the export area, but I don't think there is an equivalent in an EXE.



Sat, 02 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?


Quote:

>[snip]
>> Is there some C++/Win32 function that will return the name
>> of the currently executing function? This would make my code a bit
cleaner,
>> and I wouldn't have to hardcode all of these string values into my app.

>Unfortunately there is nothing in standard C++ that would get you the
>name of the current function. However, the __FILE__ and __LINE__
>keywords translate to a string literal containing the name of the
>current file and an integer literal containing the line in the current
>file, respectively.

Doesn't VC++ support a __FUNCTION__ (or is it __METHOD__?) that will give
the name of the function you are currently in? Its not at all portable
thought and its as likely to give you the mangled name as anything else,
which is ok if that's what you want. You can try them on your compiler and
see what they do. If that's the only compiler you'll ever need, then use
them. But if you base some whole strategy on them and later someone wants it
on another compiler, you'll be SOL.

--------------------------
Dean Roddey
The CIDLib Class Libraries
Charmed Quark Software

http://www.charmedquark.com

"100% Substance Free. More cost, less content"



Sat, 02 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?

Quote:

> Doesn't VC++ support a __FUNCTION__ (or is it __METHOD__?) that will give
> the name of the function you are currently in?

    Hi, thanks for your response!

    Although I was able to track down __FILE__ and __LINE__, I was unable to
find any reference to __FUNCTION__ and/or __METHOD__ in my MSDN
documentation... I tried:

_tprintf(__FUNCTION__) and _tprintf(__METHOD__) but the compiler didn't seem
to know what I was talking about....

David



Sat, 02 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?

Quote:

> _tprintf(__FUNCTION__) and _tprintf(__METHOD__) but the compiler didn't
seem
> to know what I was talking about....

The following sucks to retrofit, but is portable and mostly harmless...

    #define DB_(funk) funk { char const FUNCTION_[] = #funk;

    void DB_(aFunction(int q)) {
        //  do something non-boring with q
        }}

It sucks because you gotta type it in onto every function, and because you
get this }} on the end of each one!

But it's good because once the DB_() is added everywhere you can now adjust
its contents easily; for example, you could preserve in a stack of character
pointers the current function call tree.

--
 Phlip at politizen dot com                  (address munged)
======= http://users.deltanet.com/~tegan/home.html =======
  --  But all the other programmers were doing it!  --



Sat, 02 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?

Quote:
>    Although I was able to track down __FILE__ and __LINE__, I was unable
to
>find any reference to __FUNCTION__ and/or __METHOD__ in my MSDN
>documentation... I tried:

>_tprintf(__FUNCTION__) and _tprintf(__METHOD__) but the compiler didn't
seem
>to know what I was talking about....

Hmmm... Maybe I'm hallucinating, wouldn't be the first time :-) Maybe it was
Visual Age or some other compiler that supported that one. I'm pretty sure
that I tried it at one time, but even without the portability issues it
wasn't much useful since it resolved to the mangled name.

--------------------------
Dean Roddey
The CIDLib Class Libraries
Charmed Quark Software

http://www.charmedquark.com

"100% Substance Free. More cost, less content"



Sat, 02 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?
Quote:

> Doesn't VC++ support a __FUNCTION__ (or is it __METHOD__?) that will give

First time I hear of it. I don't think such macros are available.

I'm not sure what he needs such a thing for, one could create a global
array of function names, a 'current name" pointer, and a little chunk of
code at the beginning of every function that would set it to point to
its own name by ordinal. All of which could be done with a piece of perl
that would be run every time the code is compiled. But, again, all
depends on why he needed such a thing. A compiler doesn't support this
functionality.
--
len
if you must email, reply to:



Sat, 02 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?
The C9X standard supports (as of 1/99) a predefined identifier "__func__"
which is implicitely declared as if , imediately following the open
brace, it has been declared as:
static const char __func__[] = "funcname";

C++ AFAIK doesn't have one....

As far as Im concerned using the function name for error reporting
is not the way to go.. I use a generic error handling exception
routines...

---

Least Senior Software Developer   | Garbage In -- Gospel Out
Low Man on the Totem Pole         | Bus Error (passengers dumped)
Minister of Obnoxious Signatures  | BAAWA, Long live her majesty IPU
Holder of Past Knowledge          | AA # lost in move to real job
Pioneer Preference Status O-      | <plonk!>

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS/M d---(pu) s++:- a-- C++ UILSH+++$ P--- L++ E--- W+
N++ o-- K? w--(++$) O- M-- V- PS+ !PE Y PGP t(+) 5+++ X+
R- tv+(-) b+(++) DI++++ D G e++>++++ h!>--- r% y+>+++++**
------END GEEK CODE BLOCK------

Quote:


> > Doesn't VC++ support a __FUNCTION__ (or is it __METHOD__?) that will give
> First time I hear of it. I don't think such macros are available.

> I'm not sure what he needs such a thing for, one could create a global
> array of function names, a 'current name" pointer, and a little chunk of
> code at the beginning of every function that would set it to point to
> its own name by ordinal. All of which could be done with a piece of perl
> that would be run every time the code is compiled. But, again, all
> depends on why he needed such a thing. A compiler doesn't support this
> functionality.
> --
> len
> if you must email, reply to:




Sat, 02 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?
[snip]

Quote:
> It sucks because you gotta type it in onto every function, and because you
> get this }} on the end of each one!
> But it's good because once the DB_() is added everywhere you can now adjust
> its contents easily; for example, you could preserve in a stack of character
> pointers the current function call tree.

Just get a good de{*filter*}, and let the de{*filter*} do that for you. I have
tried similar approaches and in my opinnion they simply are too much
trouble. Design by contract, break on the throw, examine the context,
and fix the bug.

---
Vesa Karvonen



Sun, 03 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?

C9X will be the next version of ISO standard 9899 (Programming
Languages - C).  The current standard was made official in 1990 (you
may know it better as "C89"). It's called C9X cause it was anticipated
to be ratified in the 90's but right now, it looks like it may not be
ratified till 2000 :(.  Committee Draft 2 was released for comments
at the beginning of the year...Committee drafts are the drafts which
will be sent to the ISO body for consideration; more often than
not, committee draft 2.  They still look like they have a few comments
to iron out yet, but we'll see...

FMI: http://www.dkuug.dk/JTC1/SC22/WG14

(note, alot of ISO working group's have their websites here...a font
of information :))

---

Least Senior Software Developer   | Garbage In -- Gospel Out
Low Man on the Totem Pole         | Bus Error (passengers dumped)
Minister of Obnoxious Signatures  | BAAWA, Long live her majesty IPU
Holder of Past Knowledge          | AA # lost in move to real job
Pioneer Preference Status O-      | <plonk!>

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS/M d---(pu) s++:- a-- C++ UILSH+++$ P--- L++ E--- W+
N++ o-- K? w--(++$) O- M-- V- PS+ !PE Y PGP t(+) 5+++ X+
R- tv+(-) b+(++) DI++++ D G e++>++++ h!>--- r% y+>+++++**
------END GEEK CODE BLOCK------

Quote:


> > The C9X standard supports (as of 1/99) a predefined identifier "__func__"
> At the risk of looking stupid: what's "C9X"?
> --
> len
> if you must email, reply to:




Sun, 03 Feb 2002 03:00:00 GMT  
 C++: Get Function's Name at Runtime?
Quote:

> The C9X standard supports (as of 1/99) a predefined identifier "__func__"

At the risk of looking stupid: what's "C9X"?
--
len
if you must email, reply to:



Mon, 04 Feb 2002 03:00:00 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. C++: Get Function's Name at Runtime?

2. getting names of functions at runtime

3. Getting function name at runtime?

4. DLL function calling problems (explicit - only knowing function name at runtime)

5. Getting an item by it's name (instead of it's index#)

6. Getting a fn's address at runtime.

7. Q:removing 'public' functions names

8. Getting the executable's name without using argv[0]

9. getting the name of a <'d in file

10. Getting the executable's name without using argv[0]

11. Getting files' name and extension

12. Getting the document' file name

 

 
Powered by phpBB® Forum Software