Is there a way to have debug code like print statements eliminated after debugging? #9683
Replies: 3 comments 2 replies
-
There is a recent pull request that enables this scenario: #8970 And I suppose it could work in older versions with an const int instead of a bool (follow link for example). |
Beta Was this translation helpful? Give feedback.
-
There is a debug constant in MicroPython :) if __DEBUG__:
print("this will be eliminated by the compiler at optimisation level 1 or higher. You can set the optimisation level for the compiler with
this will apply to any subsequent code compiled at runtime (i.e. if you put this at the top of main.py it will apply to any future imports). Or if compiling with mpy-cross, with |
Beta Was this translation helpful? Give feedback.
-
I normally do something like this: if DEBUG_MODE_ENABLED:
def debug(..args...):
..whatever you want to do for debug output..
else:
def debug(..args..):
pass Then you just have the overhead of a function call. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
What I mean is something like I have done before with the C preprocessor, for example, to easily include and exclude print debugging statements:
#define DEBUG 0
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
int addOne(int inX){
debug("Received value:");
debugln(inX);
...
}
Is there a way to do something similar in micropython? If there was a debug constant in micropython and an
if debug
referencing that constant, would micropython optimize the code out if debug was false or would it still be executed as normal?Is a logging module or something else a better way to do something like this?
Or do people just manually strip out the debugging code they used when creating the code? I'm just trying to figure out a good workflow for micropython.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions