473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

determining function name in runtime

Hi all

is there any way the determine which function currently is executing in
a process ?

For example we can find the file name using __FILE__
and line number using __LINE__ macros

simillarly is there any macro which gives the function name

thanks shyam

Mar 2 '06 #1
12 2772

shyam wrote:
Hi all

is there any way the determine which function currently is executing in
a process ?
I assume you mean program...

For example we can find the file name using __FILE__
and line number using __LINE__ macros

simillarly is there any macro which gives the function name


Yes, and it's __func__ (note double underscores before and after).

--
BR, Vladimir

Mar 2 '06 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote:
shyam wrote:
Hi all

is there any way the determine which function currently is executing in
a process ?


I assume you mean program...
For example we can find the file name using __FILE__
and line number using __LINE__ macros

simillarly is there any macro which gives the function name


Yes, and it's __func__ (note double underscores before and after).


Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.
- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEBwFDagVFX4UWr64RAlQdAJ9XT4qQS65GzFFRPO0RDF qxskDA+wCdGWGd
evG1lEWtpMvGxVdvP/Y97RM=
=vy6r
-----END PGP SIGNATURE-----
Mar 2 '06 #3

"Lew Pitcher" <Le*********@tdsecurities.com> wrote in message
news:8j*******************@news20.bellglobal.com.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote: -snip

, and it's __func__ (note double underscores before and after).
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.


Let me guess, __func__ is a C99 feature?
<OT> (which means I can't use it with my compiler msvc 6.0).<OT>

--
MrG{DRGN}
Mar 2 '06 #4

MrG{DRGN} wrote:
"Lew Pitcher" <Le*********@tdsecurities.com> wrote in message
news:8j*******************@news20.bellglobal.com.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote:

-snip

, and it's __func__ (note double underscores before and after).

Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.


Let me guess, __func__ is a C99 feature?
<OT> (which means I can't use it with my compiler msvc 6.0).<OT>


Yes it is.
<OT>I know nothing about M$VC, but it may provide it as an
extension?<OT>

Mar 2 '06 #5
Ico
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?

--
:wq
^X^Cy^K^X^C^C^C^C
Mar 2 '06 #6

Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?


It is easy for the preprocessor to know the current file and line.
However, it can not known the function name (even it can not known what
is a function) if it doesn't knowns C syntax (and the preprocessor
doesn't).

Mar 2 '06 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?


My copy of the draft C99 standard doesn't give a reason.

My /guess/ is that __FILE__ and __LINE__ can be implemented as macros
because they do not require any interpretation of the C code (they are
strictly derived from the mechanics of reading the source files).

OTOH, __func__ requires the compiler's interpretation of the code in
order to work, so it cannot be implemented in the same manner as __FILE_
and __LINE__.
- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEByGZagVFX4UWr64RAjQJAJ9unyR7P+JPi14YwCi+eK aYF+VeMgCgs0N/
yRzUKfTTmLVMySmRWJUv6us=
=KubY
-----END PGP SIGNATURE-----
Mar 2 '06 #8
"tmp123" <tm****@menta.net> writes:
Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?


It is easy for the preprocessor to know the current file and line.
However, it can not known the function name (even it can not known what
is a function) if it doesn't knowns C syntax (and the preprocessor
doesn't).


s/doesn't/might not/

Several implementations have provided such a macro. While they /may/
have been handled differently from other macros (I don't actually
know), they were still handled as string literals, and thus you could
make use of the concatenation feature, etc.

But I think the reason you give above is at least why the standard
didn't want to /require/ __func__ to be a macro (and so they required
it to be something else).

-Micah
Mar 2 '06 #9
On 2006-03-02, MrG{DRGN} <Ia****@here.com> wrote:

"Lew Pitcher" <Le*********@tdsecurities.com> wrote in message
news:8j*******************@news20.bellglobal.com.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote:

-snip

, and it's __func__ (note double underscores before and after).

Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.


Let me guess, __func__ is a C99 feature?
<OT> (which means I can't use it with my compiler msvc 6.0).<OT>


No, but i have a suggestion:

#if __STDC_VERSION__ < 199901l
#define WHERE_FMT __FILE__ ":%d"
#define WHERE_ARGS __LINE__
#else
#define WHERE_FMT __FILE__ ":%s:%d"
#define WHERE_ARGS __LINE__,__func__
#endif

fprintf(stderr,"Line printed to stderr at "WHERE_FMT"\n",WHERE_ARGS);

will print file.c:function:42 on c99 systems, file.c:42 on others.
Mar 2 '06 #10
On 2006-03-02, Lew Pitcher <Le*********@tdsecurities.com> wrote:

Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?


My copy of the draft C99 standard doesn't give a reason.


That's because that's not the right place to look
http://www.open-std.org/jtc1/sc22/wg...onaleV5.10.pdf

lines 29-31 on page 50 [PDF page 57] read as follows:

A new feature of C99: C99 introduces predefined identifiers, which have
block scope (as distinct 30 from predefined macros which have file
scope), and one such predefined identifier, __func__, which allows the
function name to be used at execution time.

That seems to be saying that the reason is because a macro has file
scope, and thus couldn't have a different value on different lines
unless undefined and redefined by the user.

Note, however, that string literal concatenation takes place in phase 6,
whereas semantic analysis [which is when { } becomes a block scope, etc]
is phase 7.
Mar 2 '06 #11
On Thu, 02 Mar 2006 21:48:10 +0000, Jordan Abel wrote:
No, but i have a suggestion:

#if __STDC_VERSION__ < 199901l
#define WHERE_FMT __FILE__ ":%d"
#define WHERE_ARGS __LINE__
#else
#define WHERE_FMT __FILE__ ":%s:%d"
Micro correction: swap %s and %d (or __LINE__ and __func__ below)
#define WHERE_ARGS __LINE__,__func__
#endif

fprintf(stderr,"Line printed to stderr at "WHERE_FMT"\n",WHERE_ARGS);


--
Ben.
Mar 3 '06 #12
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-02, Lew Pitcher <Le*********@tdsecurities.com> wrote:

Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:

Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.
Is there a reason why __func__ is not a macro ?


My copy of the draft C99 standard doesn't give a reason.


That's because that's not the right place to look
http://www.open-std.org/jtc1/sc22/wg...onaleV5.10.pdf

lines 29-31 on page 50 [PDF page 57] read as follows:

A new feature of C99: C99 introduces predefined identifiers, which have
block scope (as distinct 30 from predefined macros which have file
scope), and one such predefined identifier, __func__, which allows the
function name to be used at execution time.

That seems to be saying that the reason is because a macro has file
scope, and thus couldn't have a different value on different lines
unless undefined and redefined by the user.


__LINE__ has a different value on different lines, and the
preprocessor has no problem handling that.

__func__ isn't a macro because making it one would have required the
preprocessor (more precisely, translation phase 4) to understand more
of the language grammar than is currently required.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 3 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
1
by: RWC | last post by:
Hi Folks, I'm looking for a way to determine if the client machine has access installed and if so, what version. The reason I need this is to determine (programatically) if the Access Runtime...
0
by: Stephen Nesbitt | last post by:
All: Here's my implementation problem. I have a base class which has the responsibility for providing entry into the logging system. Part of the class responsibility is to ensure that lagger...
5
by: Lyn | last post by:
Is there any way of determining in VBA the name and arguments of the currently executed procedure? To assist in debugging, I would like to be able to trace the procedures as they are executed. ...
1
by: Susan Bricker | last post by:
Greetings. I am trying to position opened forms so that they are cascaded on the screen. I have discovered the movesize action (for the DoCmd) and Move property of a form (for Acc 2002/2003). ...
0
by: CTDev Team | last post by:
Hi, We are using Exchange Server 5.5, and have applications written in VB6 and C# that read and process emails. We are experiencing intermittent errors similar to C# Application ...
2
by: Garrett | last post by:
Need any help in determining which groups have been given security access to a folder. Searched DirectoryServices to no avail... Any Help?
1
by: roddles | last post by:
Hi I need to at runtime determine the name of the function I am currently in, and the namespace of the class the function is executing from. I need this information for some logging routines i am...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.