View previous topic :: View next topic |
Author |
Message |
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Wed Dec 02, 2015 7:16 am |
|
|
I do something similar to that, but move the check to outside the printf. This is only practical with the version 5 compiler as it supports variadic macros
Code: |
BOOLEAN g_debugEnabled = TRUE;
#define dbg_printf(...) if(g_debugEnabled){ fprintf(DEBUG_STREAM,__VA_ARGS__);}
|
Sometimes I wrap it in a #ifdef checking for the existence of DEBUG_STREAM so I also have the option of completely removing the code via the preprocessor.
something like:
Code: |
#ifdef DEBUG_STREAM
BOOLEAN g_debugEnabled = TRUE;
#define ENABLE_DEBUG() g_debugEnabled=TRUE
#define DISABLE_DEBUG() g_debugEnabled=FALSE
#define dbg_printf(...) if(g_debugEnabled){ fprintf(DEBUG_STREAM,__VA_ARGS__);}
#else
#define ENABLE_DEBUG()
#define DISABLE_DEBUG()
#define dbg_printf(...)
#endif
|
Then I just have to remember to use the DEBUG_STREAM as a #define:
Code: |
#define DEBUG_STREAM STREAM_UART1
#use rs232(UART1,baud=9600,errors,stream=DEBUG_STREAM)
#include "debugging.h"
|
where debugging.h has all that stuff above.
EDIT: those are all typed out by hand, so it is possible I made a typo or two. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Dec 02, 2015 8:40 am |
|
|
Idea:
what are the consequences of a "Setup_Uart(0);" on a software Uart Printf?
Would this be another possible way to do this?
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Dec 02, 2015 9:59 am |
|
|
"This function is only available on devices with a built in UART." |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Fri Dec 04, 2015 5:23 am |
|
|
If I'm using the debugger monitor window with
Code: | #use rs232(DEBUGGER, stream=STREAM_MONITOR, xmit=PIN_B2, rcv=PIN_B2) |
Is this enabled only in debug mode? i.e. is the Code: | fprintf(STREAM_MONITOR, .. etc | code called in normal build? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Dec 04, 2015 9:35 am |
|
|
You have to test for being in debug mode 'round' your print, or the print code will be generated in all build modes. |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Mon Dec 07, 2015 3:52 am |
|
|
Ttelmah wrote: | You have to test for being in debug mode 'round' your print, or the print code will be generated in all build modes. |
Sadly manually I think, because the compiler doesn't automatically make a _DEBUG_ symbol when in debug mode. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon Dec 07, 2015 4:51 am |
|
|
Don't get me wrong, but it does seem as if a 'complex' solution is being sought for a simple thing.
#DEFINE DEBUG
#DEFINE DEBUG_PRINT
Then have all your debug code (printouts, the actual selection of the debugger etc.), all enabled by these defines.
Just selecting the DEBUGGER printout stream, does not automatically imply you are actually running an ICD. I'll often print to the DEBUG hardware port, while actually executing the code (other than the prints) in 'run' mode. Means you don't have to have a MAX232, and saves pins. So you don't want this controlled or linked to the ICD options.
Sometimes trying to tie things together too much is more complex/restrictive than allowing people just to select what they want....
However key is that when you are writing code, you must think about the debugging as you write. I usually set up macros like 'DEBUG_MESSAGE', which contains the tests, and outputs a message as needed. This has become much easier (used to have several supporting different message types, but the last few dozen CCS releases support variable length argument lists in macros - makes this much easier/nicer...).
The original poster seems to be looking for a complex way to do a simple thing.
Just use search and replace, to replace all the printfs that are 'debug only', with something like 'debug_printf'. Assuming they all will talk to a particular stream, a search for printf(STREAMNAME will find them all.
Then just have a single #define, that makes debug_printf into a blank statement or into the required output, depending on whether you are debugging or not. Job done. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Dec 07, 2015 11:45 am |
|
|
My original intention was to be able to switch the printfs on and off DURING RUNTIME without having to recompile in the simplest way.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon Dec 07, 2015 11:56 am |
|
|
Which the 'debug_on' flag I showed earlier does. Just route all your debug printf's through this. So
printf(debug_putc,"what you want to print");
This can then be disabled by setting 'debug_on' to FALSE; |
|
|
nuclear__
Joined: 24 Jan 2015 Posts: 63
|
adapt this on usb output |
Posted: Tue Jan 19, 2016 10:26 am |
|
|
Ttelmah, how can i modify my code to use your solution and subtitute the line below.
Printf(usb_cdc_putc,"debug test ");
?
What should be the definition on debug_stream ? |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Tue Jan 19, 2016 10:42 am |
|
|
Another option you might want to consider (it still involves a flag test around each printf) is to use an available pin with a jumper. If the jumper is in place (say to a pull-up or your choice), print else not print. Sort of a "bench mode" if you will where the board behaves differently (prints debug info) if the debug jumper is in place.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
|