|
|
View previous topic :: View next topic |
Author |
Message |
gribas
Joined: 21 Feb 2008 Posts: 21
|
Debug / trace macro |
Posted: Thu Aug 30, 2012 11:20 am |
|
|
Hi,
Is there a better way to print debug info? I wasn't able to hide the stream information inside the macro (RS232_STREAM), so I end up with this:
Code: |
#define DEBUG 1
#define TRACE(x) \
do { if (DEBUG) fprintf x ; } while (0)
void main() {
TRACE((RS232_STREAM,"Test %d %s \r\n",1,"..."));
}
|
Regards, |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
gribas
Joined: 21 Feb 2008 Posts: 21
|
|
Posted: Thu Aug 30, 2012 2:53 pm |
|
|
Thanks for the info! After reading that thread I came up with some alternatives that seem to work:
If you want to be able to switch to/from debug mode (debug will be present in production code)
Code: |
#define TRACE(x) \
do { if (debug_var) fprintf x ; } while (0)
void main() {
debug_var = 1;
TRACE((DBG,"Debug information: a=%u, b=%u",
variable_name_a,
variable_name_b));
debug_var = 0;
TRACE((DBG,"No Output"));
}
|
If you want to remove all debug info from production code
Code: |
#ifdef DEBUG
#define TRACE(x) fprintf x
#else
#define TRACE(x)
#endif
void main() {
TRACE((DBG,"Debug information: a=%u, b=%u %s",
variable_name_a,
variable_name_b,
"\n"));
}
|
If you want to create switchable debug blocks
Code: |
#define _D if(debug_var)
void main() {
debug_var = 1;
_D printf("This is a debug statement");
_D {
a += 10;
b += 11;
}
}
|
If you want to create debug blocks that will go away when in production
Code: |
#ifdef DEBUG
#define _D if (1)
#else
#define _D if (0)
#endif
void main() {
_D {
a += 10; printf("Test");
}
}
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|