digital adj. Having digits.     peer n. A comrade; a companion; a fellow; an associate. inmotion    
   
Recent Articles
Handling a Subversion Repository URL Change
Sunday, May 3, 2009
If your repository URL changes, you can use the following command to fix existing snapshots.
vfat Mounts Default to Lowercase Shortnames
Tuesday, April 21, 2009
I want a "this is brain-damage" quote from Linus for this mess.
VirtualBox or VMWare Virtual Machine at Login
Sunday, April 12, 2009
How to start a virtual machine in X when a user logs in.
Dialog Progress Bar Through Pipe
Sunday, April 12, 2009
How to use dialog to display a script progress bar and communicate progress to it through a named pipe.
Mount JFFS2 Image
Saturday, October 25, 2008
Example of how to mount a JFFS2 image using mtdblock.
Ottawa Linux Symposium 2008
Sunday, July 27, 2008
Here are some pictures from the 2008 Linux Symposium.
Linux Symposium 2008
Sunday, July 20, 2008
I'll be attending the Linux Symposium this year.
Clay Shirky: Institutions vs. collaboration
Monday, July 14, 2008
This is a rather interesting talk that takes some very foundational ideas from open source software development, P2P networks, and social networking and implies that these paradigms can apply to a lot more.

The only thing constant is change itself.

Projects-Code Snippets-Debug Macros

Debug Macros

Wednesday, May 18, 2005 by digitalpeer

The following code snippet demonstrates how I use debug macros during development (along with common ones like assert). These things are a time saver during debugging. When DEBUG isn't defined they simply go away. One of the macros demonstrates how to use variable list arguments in macros. This is useful for doing exactly what I'm showing- wrapping fprintf or printf with a macro. Forgive the mixture of cout and printf - wanted to demonstrate using both.

#include <cstdio>
#include <iostream>

#ifdef DEBUG

/* just a helper for code location */
#define LOC std::cout << "debug:" << __FILE__ << ":" << __LINE__ << " ";

/* macro using var args */
#define DEBUG_PRINT(fmt,...) LOC printf(fmt,__VA_ARGS__);

/* macro for general debug print statements. */
#define DEBUG_VAL(text) LOC std::cout << text << std::endl;

/* macro that dumps a variable name and its actual value */
#define DEBUG_VAR(text) LOC std::cout << (#text) << "=" << text << std::endl;

#else

/* when debug isn't defined all the macro calls do absolutely nothing */
#define DEBUG_PRINT(fmt,...)
#define DEBUG_VAL(text)
#define DEBUG_VAR(text)

#endif

int main()
{
	int x = 10;

	DEBUG_PRINT("hello %s times %dn","world",x)

	DEBUG_VAL(x)
	
	DEBUG_VAR(x)

	return 0;
}


Comment Sunday, June 13, 2010 by  Johannes
Just found this old article while looking for a debug macro. I didn't know that macros could be defined with ... and __VA_ARGS__. Great to know this. Thanks a lot.

Submit Comment to This Article
Please post a comment if you have something to add, find something wrong, or would like more information on the topic at hand. Do not use the comment form to contact the author about unrelated concerns!

Name: Email (optional):
Enter verification number here: