Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!motcid!yedinak From: yedinak@motcid.UUCP (Mark A. Yedinak) Newsgroups: comp.lang.c Subject: Re: SUMMARY #define DEBUG (medium long) Message-ID: <2804@bass6.UUCP> Date: 7 May 90 15:27:34 GMT References: <1990May3.180328.12454@murdoch.acc.Virginia.EDU> <40654@cornell.UUCP> Organization: MOTOROLA INC. General Systems Sector Lines: 105 gordon@hermod.cs.cornell.edu (Jeffrey Adam Gordon) writes: : Thanks to all who have replied and posted. I'm getting a lot of : repeated suggestions at this point so here's a summary (the first is : the one I have chosen to use. I prefer it because it does not leave : any dead code (except a semicolon) lying around and does not rely on a : smart compiler optimizing things away. : Some solutions also had a debug level but this was more than I needed. I have been following the thread concerning the debug mechanisms, and have decided to include my variation. It is primarily used on UNIX systems, and uses environment variables to define the level of debugs statements which will be selected. Again, all the debug statements can be compiled out of the code simply by undefining the #define PROG_DEBUG. Following is the macro definition and an sample program. |--------------------------- Cut Here -----------------------------------------| #include #define PROG_MAIN /* Defined in the file containing main () */ /******************************************************************************* * * Macro: PROG_DEBUG * Purpose: The following definition is used to define a in line * debug mechanism which can be compiled out of the code. * In addition, when compiled in, it can be switched off * using an environment variable. If the environment * variable is set, the debug code is executed. The macro * will also allow settable modes of debug statements. The * value of the environment variable will be 'AND' with * the coded debug level, if the level was selected, the * statements will be executed. A debug macro can be forced * to be displayed simply by setting the code level to 255. * Likewise, if the envrionment variable is set to 255, all * debug statements will be executed. * * Range of ENV_DEBUG is 0 <= ENV_DEBUG <= 255 * * This allows the in line debug code to be accessed easily. * Arguments: L The coded debug level. * S The c statements desired in the debug code. These * can be any valid c constructs. I am not sure if * there is a maximium length for the lines, however * I have been able to place a significant amount of * code in a single debug macro. * Return: None * Author: Mark A. Yedinak * *******************************************************************************/ #ifdef PROG_DEBUG # define init_debug debug_flag = (char) (atoi \ (getenv ("ENV_DEBUG")==NULL\ ?"":getenv ("ENV_DEBUG"))) # # define debug(L,S) if (debug_flag & (L)) { S } # ifdef PROG_MAIN extern char *getenv (); char debug_flag; FILE *dbg_fp; # else extern char debug_flag; extern FILE *dbg_fp; # endif #else # define init_debug /**/ # define debug(L,S) /**/ #endif main () { init_debug; debug (1, dbg_fp = stdout; printf ("Executing Level 1 debug code\n"); fflush (dbg_fp); ); debug (2, dbg_fp = stdout; printf ("Executing Level 2 debug code\n"); fflush (dbg_fp); ); debug (255, dbg_fp = stdout; printf ("Forced Execution of debug code\n"); fflush (dbg_fp); ); printf ("Executing in-line code\n"); } |--------------------------- Cut Here -----------------------------------------| -- Mark A. Yedinak - uunet!motcid!yedinak * "Don't take life too Motorola - General Systems Sector * seriously, you will 3205 Wilke Road, Arlington Heights, IL 60004 * never get out of it 708-632-2874 (Usual Disclaimers) * ALIVE!"