CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Using macros with a logger

    Hey.

    I want to do this:
    Code:
    void f()
    {
        LOG("Called f().");
    }
    But I only want the LOG macro to work if USE_LOG is defined... in other words, if USE_LOG isn't defined, the LOG macro won't do anything. I'm trying to write the macro for this but I can't get it right:
    Code:
    #ifndef LOG(string)
    #define LOG(string)
    #ifdef USE_LOG
    Logger::Get().Log(string)
    #endif
    #endif
    This gives me a warning:
    Code:
    warning C4067: unexpected tokens following preprocessor directive - expected a newline	c:\...\logger.h
    Also I'm just not sure if it will do what I think it will do... if USE_LOG isn't defined and the call "disappears" everywhere it is used in code, will it leave behind semicolons everywhere? If so, is this a bad thing? Will the compiler interpret these as statements and spend time processing them?

    Cheers.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Using macros with a logger

    It's simply:
    Code:
    #ifdef USE_LOG
        #define LOG(string) Logger::Get().Log(string)
    #else
        #define LOG(string)
    #endif

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Using macros with a logger

    Quote Originally Posted by Plasmator View Post
    It's simply:
    Code:
    #ifdef USE_LOG
        #define LOG(string) Logger::Get().Log(string)
    #else
        #define LOG(string)
    #endif
    Cheers.

    For the else case, what would that expand to?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Using macros with a logger

    Hello MyBowlCut,

    That's a very good question.
    I know just a little to say that parameterized macro expands its argument as text,
    which in C++ could be anything from a number to invalid expression depending on where it's used. But then again, I don't know what it would really expands to.

  5. #5
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Using macros with a logger

    Quote Originally Posted by potatoCode View Post
    Hello Mybowlcut,

    That's a very good question. I don't know what it would really expands to.
    Fixed.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Using macros with a logger

    Quote Originally Posted by Mybowlcut View Post
    Fixed.

  7. #7
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Using macros with a logger

    If anyone knows what this expands to, I'm curious to know.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  8. #8
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Using macros with a logger

    Quite simple: The preprocessor will replace ``LOG("Called f().")´´ with an empty string, thus your code will look like:
    Code:
    void f()
    {
        ;
    }
    to the compiler. The single semicolon will not translate to any code on compilation.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  9. #9
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Using macros with a logger

    Oh, and btw, you might want to lookup the __func__ macro which will automatically expand to the function name.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  10. #10
    Join Date
    Aug 2007
    Posts
    858

    Re: Using macros with a logger

    Quote Originally Posted by treuss View Post
    Oh, and btw, you might want to lookup the __func__ macro which will automatically expand to the function name.
    Note though that __func__ is not a standard macro, so it may vary depending on platform. In MSVC it is __FUNCTION__.

  11. #11
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Using macros with a logger

    Cheers treuss.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured