__FILE__ and __LINE__

Discussion in 'C++' started by Neo, Nov 29, 2006.

  1. Neo

    Neo Guest

    Hie,
    Can I put __FILE__ and __LINE__ macros inside the class function which
    may not be inline. And

    void function()
    {
    classobject::LogInfo(...);
    }

    Internally LogInfo should log file name and line number. Here I dont
    want user to pass those macros and also dont want to use any macros.
    Is there any solution?
    Neo, Nov 29, 2006
    #1
    1. Advertising

  2. On 2006-11-29 06:42, Neo wrote:
    > Hie,
    > Can I put __FILE__ and __LINE__ macros inside the class function which
    > may not be inline.


    Since they are macros they are expanded by the pre-processor so whether
    the functions are inline or not does not matter, it's the file and line
    on which the code is written that is "returned", not where they are called.

    > And
    >
    > void function()
    > {
    > classobject::LogInfo(...);
    > }
    >
    > Internally LogInfo should log file name and line number. Here I dont
    > want user to pass those macros and also dont want to use any macros.
    > Is there any solution?


    Do I understand you correctly in not wanting to pass the file and line
    to the LogInfo()-function but would rather like it to know the line and
    file magically in some way? Might work, depending on which filename and
    line-number you want, as I said earlier, it's the filename and line-
    number of the file where the code is written that is expanded.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Nov 29, 2006
    #2
    1. Advertising

  3. Neo

    Neo Guest

    What you wrote is correct. So is there any way to do it?

    Erik Wikström wrote:
    > On 2006-11-29 06:42, Neo wrote:
    > > Hie,
    > > Can I put __FILE__ and __LINE__ macros inside the class function which
    > > may not be inline.

    >
    > Since they are macros they are expanded by the pre-processor so whether
    > the functions are inline or not does not matter, it's the file and line
    > on which the code is written that is "returned", not where they are called.
    >
    > > And
    > >
    > > void function()
    > > {
    > > classobject::LogInfo(...);
    > > }
    > >
    > > Internally LogInfo should log file name and line number. Here I dont
    > > want user to pass those macros and also dont want to use any macros.
    > > Is there any solution?

    >
    > Do I understand you correctly in not wanting to pass the file and line
    > to the LogInfo()-function but would rather like it to know the line and
    > file magically in some way? Might work, depending on which filename and
    > line-number you want, as I said earlier, it's the filename and line-
    > number of the file where the code is written that is expanded.
    >
    > --
    > Erik Wikström
    Neo, Nov 29, 2006
    #3
  4. Neo

    Neo Guest

    What you wrote is correct. So is there any way to do it?

    Erik Wikström wrote:
    > On 2006-11-29 06:42, Neo wrote:
    > > Hie,
    > > Can I put __FILE__ and __LINE__ macros inside the class function which
    > > may not be inline.

    >
    > Since they are macros they are expanded by the pre-processor so whether
    > the functions are inline or not does not matter, it's the file and line
    > on which the code is written that is "returned", not where they are called.
    >
    > > And
    > >
    > > void function()
    > > {
    > > classobject::LogInfo(...);
    > > }
    > >
    > > Internally LogInfo should log file name and line number. Here I dont
    > > want user to pass those macros and also dont want to use any macros.
    > > Is there any solution?

    >
    > Do I understand you correctly in not wanting to pass the file and line
    > to the LogInfo()-function but would rather like it to know the line and
    > file magically in some way? Might work, depending on which filename and
    > line-number you want, as I said earlier, it's the filename and line-
    > number of the file where the code is written that is expanded.
    >
    > --
    > Erik Wikström
    Neo, Nov 29, 2006
    #4
  5. Neo

    dasjotre Guest

    Neo wrote:
    >
    > Erik Wikström wrote:
    > > On 2006-11-29 06:42, Neo wrote:
    > > > Hie,
    > > > Can I put __FILE__ and __LINE__ macros inside the class function which
    > > > may not be inline.

    > >
    > > Since they are macros they are expanded by the pre-processor so whether
    > > the functions are inline or not does not matter, it's the file and line
    > > on which the code is written that is "returned", not where they are called.
    > >
    > > > And
    > > >
    > > > void function()
    > > > {
    > > > classobject::LogInfo(...);
    > > > }
    > > >
    > > > Internally LogInfo should log file name and line number. Here I dont
    > > > want user to pass those macros and also dont want to use any macros.
    > > > Is there any solution?

    > >
    > > Do I understand you correctly in not wanting to pass the file and line
    > > to the LogInfo()-function but would rather like it to know the line and
    > > file magically in some way? Might work, depending on which filename and
    > > line-number you want, as I said earlier, it's the filename and line-
    > > number of the file where the code is written that is expanded.
    > >

    > What you wrote is correct. So is there any way to do it?


    <don't top-post please>

    What he meant is if you put the macros in the LogInfo function, like

    struct classobject
    {
    void LogInfo(char const * msg)
    {
    somefilestream << __FILE__<<":"<<__LINE__<<msg;
    }
    };

    It will always print the same __FILE__ (one where classobject is
    defined) and the same __LINE__( that is, the one above)

    You need to make LogInfo to take additional two parameters, a filename
    and a line number

    void LogInfo(char const *file, int line, char const * msg)
    {
    somefilestream << file <<":"<<line<<msg;
    }

    and a macro

    #define LOGINFO(X) classobject::LogInfo(__FILE__, __LINE__, X)

    and use that instead
    dasjotre, Nov 29, 2006
    #5
  6. Neo

    Default User Guest

    Re: __FILE__ and __LINE__ -TPA

    Neo wrote:

    > What you wrote is correct. So is there any way to do it?


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
    Default User, Nov 29, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spry
    Replies:
    1
    Views:
    924
    Richard Heathfield
    Jul 26, 2003
  2. Joakim Hove
    Replies:
    4
    Views:
    8,045
    Maric Michaud
    Aug 13, 2006
  3. jake1138
    Replies:
    5
    Views:
    1,030
    Dave Thompson
    Feb 14, 2005
  4. Kenneth Brody

    __FILE__ and __LINE__ within macros

    Kenneth Brody, Apr 4, 2006, in forum: C Programming
    Replies:
    7
    Views:
    783
    Keith Thompson
    Apr 5, 2006
  5. Bill Davy
    Replies:
    14
    Views:
    1,661
    alain
    Feb 14, 2008
Loading...

Share This Page