__FILE__,__LINE__,__FUNCTION__ macros

Discussion in 'C++' started by Neo, Dec 21, 2006.

  1. Neo

    Neo Guest

    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
     
    Neo, Dec 21, 2006
    #1
    1. Advertisements

  2. Neo

    Ian Collins Guest

    Sounds like a tool issue rather than a C++ one, try a group dedicated to
    your compiler.
     
    Ian Collins, Dec 21, 2006
    #2
    1. Advertisements

  3. Neo

    Pete Becker Guest

    __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)
     
    Pete Becker, Dec 21, 2006
    #3
  4. __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.
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 21, 2006
    #4
  5. Neo

    BobR Guest

    Pete Becker wrote in message ...
    If the compiler is C99 complient, you can use '__func__' [1].

    [1] - ref: GCC docs. (Some minor restrictions on concatenation.)
     
    BobR, Dec 21, 2006
    #5
  6. Neo

    Pete Becker Guest

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Dec 21, 2006
    #6
  7. Neo

    Pete Becker Guest

    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.

    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)
     
    Pete Becker, Dec 21, 2006
    #7
  8. Neo

    BobR Guest

    Pete Becker wrote in message ...
    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..
     
    BobR, Dec 21, 2006
    #8
  9. Neo

    Tony Guest

    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
     
    Tony, Dec 22, 2006
    #9
  10. Neo

    BobR Guest

    Tony wrote in message ...
    std::string FuncName(__func__);
    // change 'FuncName' to what you want.
    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__.
    "
     
    BobR, Dec 22, 2006
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.