Fine tuning fprintf()

There are a couple of problems with the definition of DBG() defined in previous section.

To solve these problems, we need to dynamically control the debug output. To solve the first problem, we modify the definition of DBG() as follows:


	static int lcl_dbg_int_stack = 0;
	#if( DEBUG_INT_STACK )
	  #define DBG(_x)	(lcl_dbg_int_stack?(void)(_x):(void)0)
        #else
	  #define DBG(_x)	((void)0)
	#endif

If DEBUG_INT_STACK is defined, then debug output is still not produced by default. Instead, we have to also set lcl_dbg_int_stack to non-0 before we will start seeing output. (In a later section, I will show how we control the value of variables like lcl_dbg_int_stack).

We can also use different values of lcl_dbg_int_stack to control the amount or kinds of output produced. For instance, if we wanted to have more or less output based on the value of lcl_dbg_int_stack, we could define a set of DBG macros.


	#if( DEBUG_INT_STACK )
	  #define DBG0(_x)	((lcl_dbg_int_stack>0)?(void)(_x):(void)0)
	  #define DBG1(_x)	((lcl_dbg_int_stack>1)?(void)(_x):(void)0)
	  #define DBG2(_x)	((lcl_dbg_int_stack>2)?(void)(_x):(void)0)
        #else
	  #define DBG0(_x)	((void)0)
	  #define DBG1(_x)	((void)0)
	  #define DBG2(_x)	((void)0)
	#endif

By judicious use of DBG0(), DBG1() and DBG2(), we can control the output that is always produced if lcl_dbg_int_stack is non-0, output that is only produced at debug levels 1 and above, and output that is only produced at debug level 2.

Another way to control the output is based on the use of flags. In this case, lcl_dbg_int_stack is treated as a bit vector of debug enables, instead of a debug level.


	static unsigned lcl_dbg_int_stack = 0;
	#if( DEBUG_INT_STACK )
	  #define DBG0(_x)	((lcl_dbg_int_stack&1)?(void)(_x):(void)0)
	  #define DBG1(_x)	((lcl_dbg_int_stack&2)?(void)(_x):(void)0)
	  #define DBG2(_x)	((lcl_dbg_int_stack&4)?(void)(_x):(void)0)
        #else
	  #define DBG0(_x)	((void)0)
	  #define DBG1(_x)	((void)0)
	  #define DBG2(_x)	((void)0)
	#endif


Next Prev Main Top Feedback