__FILE__,__LINE__,__FUNCTION__ macros

N

Neo

Hi Friends,
I am planning to use "__FILE__,__LINE__,__FUNCTION__ " for a logging
component in my class.
In debug build I gets all information. I tried with release mode also
and it works. But I want to verify that will I able to get this
information at any kind of build with all possible optimizations?

Regards
Vikram S
 
I

Ian Collins

Neo said:
Hi Friends,
I am planning to use "__FILE__,__LINE__,__FUNCTION__ " for a logging
component in my class.
In debug build I gets all information. I tried with release mode also
and it works. But I want to verify that will I able to get this
information at any kind of build with all possible optimizations?
Sounds like a tool issue rather than a C++ one, try a group dedicated to
your compiler.
 
P

Pete Becker

Neo said:
Hi Friends,
I am planning to use "__FILE__,__LINE__,__FUNCTION__ " for a logging
component in my class.
In debug build I gets all information. I tried with release mode also
and it works. But I want to verify that will I able to get this
information at any kind of build with all possible optimizations?

__FILE__ and __LINE__ are always valid, and their meanings are specified
by the C++ standard. If a compiler switch disables them, the compiler
doesn't conform to the language definition when that switch is used.

__FUNCTION__ is not specified by the standard. You'll have to look at
your compiler's documentation to figure out what it does and when it
does it.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi Friends,
I am planning to use "__FILE__,__LINE__,__FUNCTION__ " for a logging
component in my class.
In debug build I gets all information. I tried with release mode also
and it works. But I want to verify that will I able to get this
information at any kind of build with all possible optimizations?

__FUNCTION__ is not part of the C++ standard, but __LINE__ and __FILE__
are. Notice however that the value of __FILE__ can be either the
relative path (name of file) or the absolute path to the file at
compile time, or perhaps something in between.
 
B

BobR

Pete Becker wrote in message ...
__FILE__ and __LINE__ are always valid, and their meanings are specified
by the C++ standard. If a compiler switch disables them, the compiler
doesn't conform to the language definition when that switch is used.

__FUNCTION__ is not specified by the standard. You'll have to look at
your compiler's documentation to figure out what it does and when it
does it.

If the compiler is C99 complient, you can use '__func__' [1].

[1] - ref: GCC docs. (Some minor restrictions on concatenation.)
 
P

Pete Becker

BobR said:
Pete Becker wrote in message ...
__FUNCTION__ is not specified by the standard. You'll have to look at
your compiler's documentation to figure out what it does and when it
does it.

If the compiler is C99 complient, you can use '__func__' [1].

That's true if you're compiling C, but the C99 standard doesn't tell you
what __func__ does in a C++ member function, nor in a template function.

[1] - ref: GCC docs. (Some minor restrictions on concatenation.)

As I said: you'll have to look at your compiler's documentation. <g>

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
B

BobR

Pete Becker wrote in message ...
BobR said:
Pete Becker wrote in message ...
__FUNCTION__ is not specified by the standard. You'll have to look at
your compiler's documentation to figure out what it does and when it
does it.

If the compiler is C99 complient, you can use '__func__' [1].

That's true if you're compiling C, but the C99 standard doesn't tell you
what __func__ does in a C++ member function, nor in a template function.
[1] - ref: GCC docs. (Some minor restrictions on concatenation.)

As I said: you'll have to look at your compiler's documentation. <g>

I just know it works in GCC g++ without turning on any special flags or
includes(yeah, some are 'automatic' <g>).

Maybe someone with a faster access than I can try this on Comeau?:

// ------------------------------------
#include <iostream> // #include <ostream>
void Test( std::eek:stream &cout ){
cout<<"\n_____[ "<<__func__<<" ]_____"<<std::endl;
// ------------------------------------
cout<<"_____[ "<<__func__<<" ]__End\n"<<std::endl;
return;
}

int main(){
Test( std::cout );
return 0;
}
// ------------------------------------

Did it make Comeau puke?

Sure is a handy 'feature' for testing, error output, etc..
 
T

Tony

BobR said:
Pete Becker wrote in message ...
__FILE__ and __LINE__ are always valid, and their meanings are specified
by the C++ standard. If a compiler switch disables them, the compiler
doesn't conform to the language definition when that switch is used.

__FUNCTION__ is not specified by the standard. You'll have to look at
your compiler's documentation to figure out what it does and when it
does it.

If the compiler is C99 complient, you can use '__func__' [1].

[1] - ref: GCC docs. (Some minor restrictions on concatenation.)

A problem with the __func__ and __FILE__ macros is that you can't
control the formatting. Sometimes you may want just a file name rather
than a full path, for example.

It's odd that the C99 __func__ macro is not __FUNC__, don't ya think?

Tony
 
B

BobR

Tony wrote in message ...
"BobR" wrote in message ...
Pete Becker wrote in message ...
__FILE__ and __LINE__ are always valid, and their meanings are specified
by the C++ standard. If a compiler switch disables them, the compiler
doesn't conform to the language definition when that switch is used.

__FUNCTION__ is not specified by the standard. You'll have to look at
your compiler's documentation to figure out what it does and when it
does it.

If the compiler is C99 complient, you can use '__func__' [1].

[1] - ref: GCC docs. (Some minor restrictions on concatenation.)

A problem with the __func__ and __FILE__ macros is that you can't
control the formatting. Sometimes you may want just a file name rather
than a full path, for example.

std::string FuncName(__func__);
// change 'FuncName' to what you want.
It's odd that the C99 __func__ macro is not __FUNC__, don't ya think?
Tony

If '__func__' was a macro, it might have an all uppercase name.

From GCC docs:

"
__func__ is defined by the ISO standard C99:
The identifier __func__ is implicitly declared by the translator
as if, immediately following the opening brace of each function
definition, the declaration
static const char __func__[] = "function-name";
appeared, where function-name is the name of the lexically-enclosing
function. This name is the unadorned name of the function.

By this definition, __func__ is a variable, not a string literal. In
particular,
__func__ does not catenate with other string literals.
In C++, __FUNCTION__ and __PRETTY_FUNCTION__ are variables,
declared in the same way as __func__.
"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top