Fog Creek Software
Discussion Board




Debug

If I want to have some codes to be included for debug version only, what should I do?

Susan Ramsbottom
Thursday, August 26, 2004

errr?

Pick up a damn book and learn some basics before you submit assinine questions to online programming forums?

muppet
Thursday, August 26, 2004

Is this for C++?

If so use #ifdef _DEBUG #endif to enclose the code in question.

Code Monkey
Thursday, August 26, 2004

As usual, it depends.

What Language and Environment are you using.

C++: Normally you would #define DEBUG and then enclose debugging code in #ifdef DEBUG and #endif preprocessor directives.

VB: Debug.Print

Those are the ones I am familiar with.  Most languages have some for of selection statements such as if that will allow you to choose what code gets executed based on variable conditions.

You might also try logging.  Which is another topic all together.

Have fun debugging.
Thursday, August 26, 2004

#ifndef NDEBUG
#endif

It's C standard! So it also works in C++ !

redguardtoo
Thursday, August 26, 2004

Never have debug only build. Test what you run
and run what you test. Put the debug code in
another library and call it from a test program
that knows it's a test program.

fourth place
Friday, August 27, 2004

You can use my debug header file. It is compliant with C standard (means you do not need configuring any compiler).

#ifndef __DEBUG_UTIL_H__
#define __DEBUG_UTIL_H__

//------------------------------------------------------------------------
// File Name: debug_util.h
// Usage:
//  use MYTRACE0 to trace a string
//  use MYTRACE1 to trace ONE variable of any type (standard C printf format)
// Example:
//  #include "debug_util.h"
//  int main(void)
//  {
//    MYTRACE0("hello world");
//    MYTRACE1("I have %d dollars",10);
//    MYASSERT(2>3);
//    return 0;
//  }
//------------------------------------------------------------------------



//-------------------configure debug utility here----------begin
#define DEBUG_LOGGING_ENABLED  1    //logging enabled. if diabled, only MYTRACE0, MYTRACE1 do nothing, MYASSERT equals standard C assert
#define DEBUG_WINDOWS    1    //call OutputDebugString (only works on windows)
#define DEBUG_ANSIC_FILE    0 //use ANSIC file logging (log file name is hardcoded as "debug.log")
                                  // ANSIC file logging works on any systems which support standard C)
//-------------------configure debug utility here----------end

                                 
#if DEBUG_WINDOWS
#include <windows.h>   
#define OUTPUT_DEBUG_STRING(x) OutputDebugString(x)    
#else
#define OUTPUT_DEBUG_STRING(x) ((void)0)
#endif

#include <ctime>
#include <cstdio>
#include <cassert>




#ifdef  NDEBUG
#define MYASSERT(x)    ((void)0)
#define MYTRACE0(x)    ((void)0)    
#define MYTRACE1(x,p)    ((void)0)
#else
    
#define MYTRACE0(arg) {        \
    if(DEBUG_LOGGING_ENABLED){            \
        char inf[2048];    \
        time_t timer;    \
        timer=time(NULL);    \
        sprintf(inf,"Time %sFile %s, Line %d,\n%s\n\n",asctime(localtime(&timer)),__FILE__,__LINE__,arg);    \
        if(DEBUG_ANSIC_FILE) {    \
            FILE *fp=NULL;    \
            fp=fopen("debug.log","a");    \
            if(fp!=NULL){ \
                fprintf(fp,"%s",inf);    \
            }    \
            fclose(fp);    \
        }        \
        if(DEBUG_WINDOWS) {    \
            OUTPUT_DEBUG_STRING(inf);    \
        }    \
    } \
}

#define MYTRACE1(fmt,arg) {        \
    if(DEBUG_LOGGING_ENABLED){    \
        char inf[2048];    \
        time_t timer;    \
        timer=time(NULL);    \
        sprintf(inf,"Time %sFile %s, Line %d,\n"##fmt##"\n\n",asctime(localtime(&timer)),__FILE__,__LINE__,arg);    \
        if(DEBUG_ANSIC_FILE) \
        {    \
            FILE *fp=NULL;    \
            fp=fopen("debug.log","a");    \
            if(fp!=NULL){ \
                fprintf(fp,"%s",inf);    \
            } \
            fclose(fp);    \
        }        \
        if(DEBUG_WINDOWS) \
        {    \
            OUTPUT_DEBUG_STRING(inf);    \
        };    \
    }        \
}

#define MYASSERT(x)      {        \
            if(!(x))    \
            {        \
                MYTRACE0("assert failed");    \
                assert(x);     \
            }    \
            else    \
                assert(x);    \
            }
            
#endif

#endif //__DEBUG_UTIL_H__

redguardtoo
Friday, August 27, 2004

or download "debug_util.h"
from http://www.d2ksoft.com/debug_util.h

redguardtoo
Friday, August 27, 2004

Anyone who answers programming questions to someone named Ramsbottom is a donkey's bottom.

Alex
Friday, August 27, 2004

C'mon guys, she only wants to add some codes to her softs.

sid
Friday, August 27, 2004

*  Recent Topics

*  Fog Creek Home