Commenting out debug code

Discussion in 'C++' started by Michael, May 27, 2004.

  1. Michael

    Michael Guest

    OK Guys, lets say that I've got some code that I want from time when I'm
    debuging but want to be able to turn off quickly.
    At the moment I'm doing:


    #define DEBUG_TEXTURE
    #define DEBUG_BSP
    #define DEBUG_COLLISION



    //Do Stuff:

    #ifdef DEBUG_TEXTURE
    //Debuging code;
    //OutPuting to file/console
    #endif

    //More Stuff

    #ifdef DEBUG_BSP
    //Debuging code;
    #endif

    however my debug lines are normally only 1 line, so I end up with really
    messy code.

    What I'd like to do is:


    #define DEB_BSP //

    //then in code
    DEB_BSP cout <<" Debug Info";


    but the double forward slash wouldn't work with the #define.

    What do others do to get around this? Is my original method the only way?

    I could do

    #define DEB_BSP 1 //make 0 to disable


    if( DEB_BSP) cout << "Debug output";
    but this isn't really great because I'll incur quite a performance hit at
    runtime with lots of if statements!

    Thanks in advance



    Mike
    Michael, May 27, 2004
    #1
    1. Advertising

  2. Michael wrote:
    > OK Guys, lets say that I've got some code that I want from time when I'm
    > debuging but want to be able to turn off quickly.
    > At the moment I'm doing:
    >
    >
    > #define DEBUG_TEXTURE
    > #define DEBUG_BSP
    > #define DEBUG_COLLISION
    >
    >
    >
    > //Do Stuff:
    >
    > #ifdef DEBUG_TEXTURE
    > //Debuging code;
    > //OutPuting to file/console
    > #endif
    >
    > //More Stuff
    >
    > #ifdef DEBUG_BSP
    > //Debuging code;
    > #endif
    >
    > however my debug lines are normally only 1 line, so I end up with really
    > messy code.
    >
    > What I'd like to do is:
    >
    >
    > #define DEB_BSP //
    >
    > //then in code
    > DEB_BSP cout <<" Debug Info";
    >
    >
    > but the double forward slash wouldn't work with the #define.
    >
    > What do others do to get around this? Is my original method the only way?
    >
    > I could do
    >
    > #define DEB_BSP 1 //make 0 to disable
    >
    >
    > if( DEB_BSP) cout << "Debug output";
    > but this isn't really great because I'll incur quite a performance hit at
    > runtime with lots of if statements!


    No you don't, because most compilers are smart enough to optimise out an
    if(1) or if(0).

    My standard debug macro is

    #ifdef DEBUG
    #define DOUT std::cerr
    #else
    #define DOUT if (false) std::cerr
    #endif


    Jacques.
    Jacques Labuschagne, May 27, 2004
    #2
    1. Advertising

  3. if( DEB_BSP) cout << "Debug output";
    > but this isn't really great because I'll incur quite a performance

    hit at
    > runtime with lots of if statements!



    I do this:
    #ifdef _DEBUG
    #define _dcout(a) cout << a
    #else
    #define _dcout(a)
    #endif

    _dcout("Hello" << 5 << "World");

    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
    Gernot Frisch, May 27, 2004
    #3
  4. Michael

    ak Guest

    On Thu, 27 May 2004 10:35:22 +0000 (UTC), "Michael" <>
    wrote:

    >>if( DEB_BSP) cout << "Debug output";
    >>but this isn't really great because I'll incur quite a performance hit at
    >>runtime with lots of if statements


    well I dont know about you but when I run in debug it is because
    i want verify something or find a bug so getting a perfomance hit
    isn't normally a real problem.

    nowadays I always have debugging info in but can turn it off with
    a switch or have varios levels of tracing info. Yes, the release
    code is then slightly slower than what it would have been but i
    find the benefit of always having the possibility to turn on
    logging info by a customer when a problem arises to be worth it.

    also there is always a risk with having a release and debug version
    of a program - they have different code and could behave differently.

    a.k.a. "but it works in debug mode"

    /ak
    ak, May 27, 2004
    #4
    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. =?Utf-8?B?U2FuZHk=?=

    Commenting out VB code

    =?Utf-8?B?U2FuZHk=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    51,828
    =?Utf-8?B?Sm9uWg==?=
    Feb 3, 2005
  2. patrick

    java commenting out of code

    patrick, Jan 11, 2006, in forum: Java
    Replies:
    4
    Views:
    5,212
    Malte Christensen
    Jan 11, 2006
  3. Paul McNett

    Re: commenting out blocks of code

    Paul McNett, Feb 18, 2006, in forum: Python
    Replies:
    1
    Views:
    618
    Cameron Laird
    Feb 18, 2006
  4. Peter Hansen

    Re: commenting out blocks of code

    Peter Hansen, Feb 18, 2006, in forum: Python
    Replies:
    8
    Views:
    1,319
    Neil Hodgson
    Feb 20, 2006
  5. Jean-Paul Calderone

    Re: commenting out blocks of code

    Jean-Paul Calderone, Feb 18, 2006, in forum: Python
    Replies:
    0
    Views:
    479
    Jean-Paul Calderone
    Feb 18, 2006
Loading...

Share This Page