-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
py/mpprint.h: Add MP_DEBUG_PRINT() macro and MP_DEBUG_PRINT_LEVEL. #8050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
py/mpprint.h
Outdated
// 4 - DEBUG, more detaled info, dig deeper | ||
// 5 - TRACE, show algorithm flow | ||
// In real you may use own classification of debug levels. | ||
#define DBG(level, ...) \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be guarded by #ifndef DBG
because this is a super common name and people might want to change what it does, disable it, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be PRN() & PRN_LEVEL
or MSG() & MSG_LEVEL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's still fairly common. Could prefix it with mp_
. Still should be a no-op by default I guess, as are all other macros like this (DEBUG_printf etc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both pair (PRN() & PRN_LEVEL) and (MSG() & MSG_LEVEL) not used in MicroPython.
There is
#define debug(lvl, x...) do { if (lvl <= DEBUG_LEVEL) { printf(x); } } while (0)
at https://github.com/atgreen/libffi/blob/e9de7e35f2339598b16cbb375f9992643ed81209/src/pa/ffi.c#L52
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both pair (PRN() & PRN_LEVEL) and (MSG() & MSG_LEVEL) not used in MicroPython.
Indeed, but mpprint.h is part of the public API so any definitions it in might clash with external software. So if someone includes mpprint.h, in that same file it would not be possible to also include another file which has MSG defined (unless working around it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to PM_PRN() & PM_PRN_LEVEL
Codecov Report
@@ Coverage Diff @@
## master #8050 +/- ##
=======================================
Coverage 98.39% 98.39%
=======================================
Files 158 158
Lines 20939 20939
=======================================
Hits 20602 20602
Misses 337 337 |
@dpgeorge |
Tested in PR esp32\machine_pwm.c: HOTFIX PWM duty #8075 |
This has been on my mind for a while because I noticed #if MICROPY_DEBUG_VERBOSE // print debugging info
#define DEBUG_PRINT (1)
#define DEBUG_printf DEBUG_printf
#else // don't print debugging info
#define DEBUG_PRINT (0)
#define DEBUG_printf(...) (void)0
#endif |
Code size report:
|
don't consider refresh areas for hidden groups or tilegrids
@robert-hh Example in #10854 |
py/mpprint.h
Outdated
@@ -80,3 +80,52 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args); | |||
#endif | |||
|
|||
#endif // MICROPY_INCLUDED_PY_MPPRINT_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line must be at the end of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And yet no.
We need to redefine MP_DEBUG_PRINT in each individual file.
@dpgeorge @stinos @iabdalkader @all_world |
There's a bunch of details I'd like to see different (the name 'PRN' is vague and a weird abbreviation of 'print', the levels not having gaps between them, the content of the message not being configurable, example code not using the macros but level numbers which is rather confusing) but: so far only one person expressed interest, I'd first like to see feedback from others as to whether they think this would be a useful addition. |
I will agree with any name (PRN/PRINT/LOG/LOG/DBG/DEBUG etc), but to finally have it.
The levels may be as follows:
|
9729a5e
to
db3a443
Compare
@andrewleech |
db3a443
to
5950e47
Compare
I previously thought I'd want something like this, but in practice I've only ever wanted debug prints to be on or off in just one file or set of files at a time, when I'm working on low level features in that one file / module. I definitely would not want any kind of global debug printing because the messages for the module under development would be lost in the noise. It would be handy to be able to turn debug printing on/off on a file by file basis without having to modify the file, just to avoid accidentally committing the enabled debug printing. Not sure how this would look in practice though. Personally I don't really see the value in global debug levels, but maybe that's just me. |
MP_PRN_LEVEL and MP_PRN() are not MP_PRN_LEVEL and MP_PRN() are outside the
in file1.c:
in file2.c
so there is no printing from file1.c and there is printing from file2.c |
py/mpprint.h
Outdated
if ((0 < level) && (level <= MP_PRN_LEVEL)) { \ | ||
mp_printf(MP_PYTHON_PRINTER, " MP_PRN_LEVEL=%d : ", level); \ | ||
mp_printf(MP_PYTHON_PRINTER, __VA_ARGS__); \ | ||
mp_printf(MP_PYTHON_PRINTER, "\t : FUNC=%s LINE=%d FILE=%s\n", __FUNCTION__, __LINE__, __FILE__); \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very verbose line to be printing at all levels; on many boards the uart output can be quite slow so more text here can seriously slow down the program
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My experience with debugging two files showed satisfactory speed. Most of the time I didn't print too much information by lowering MP_DEBUG_PRINT_LEVEL or pushing MP_DEBUG_PRINT to a level above MP_DEBUG_PRINT(MP_DEBUG_PRINT_XXX +1, ...) or two
MP_DEBUG_PRINT(MP_DEBUG_PRINT_XXX +2, ...).
In extreme cases, a developer can correct MP_DEBUG_PRINT as he wishes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is highly dependent on the particular board in use, and the importance of speed in the code bring tested. The existing debug prints lines in much of the Bluetooth code for instance are very short because it breaks Bluetooth if they're much longer ...
py/mpprint.h
Outdated
#define MP_PRN_WARNING 3 // WARNING, something went wrong, but you can fix it with additional operations in code right now or may ignore it. | ||
#define MP_PRN_INFO 4 // INFO, it is interesting and useful for understanding a bug. | ||
#define MP_PRN_DEBUG 5 // DEBUG, more detailed information, dig deeper. | ||
#define MP_PRN_TRACE 6 // TRACE, show a flow of the algorithm, like enter/exit a function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These defines would need to be above the #ifdef so they can be used to define the desired level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved upper
py/mpprint.h
Outdated
#endif | ||
/* | ||
// How to use: | ||
// Set MP_PRN_LEVEL in developed *.C or *.CPP file, for example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd make sense to have any docs like this above the code rather than under it, and set by level name not number
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, numbers are more convenient than names in debugging. You may try...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll just have to agree to disagree on this point then. The python logging module can have its levels defined in numbers... but the vast majority of users (myself incurred) prefer to use level names.
Also, a new developer coming across a C file with some debug "info" prints already in it will logically want to "enable info level" to see them. They wouldn't know what number this is without hunting through headers to get here.
py/mpprint.h
Outdated
@@ -80,3 +80,59 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args); | |||
#endif | |||
|
|||
#endif // MICROPY_INCLUDED_PY_MPPRINT_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the end of file include guard, and should remain at the end of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And yet no.
We need to redefine MP_DEBUG_PRINT in each individual file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this sort of non-standard coding pattern to be confusing, there's a fair chance a future programmer looking at it will think so too and "fix" it, breaking your intended behaviour without realising it.
I believe the print functionality really should be written / used in a way that doesn't take this.
Keep in mind that all C files are compiled in isolation, they all get their own copies of defines & macros included separately.
Also please don't mark concerns as resolved unless the reviewer says as such. I see now I'm not the first person to raise this particular issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep in mind that all C files are compiled in isolation, they all get their own copies of defines & macros included separately.
I tried to move #endif // MICROPY_INCLUDED_PY_MPPRINT_H
to the end of the file
and set MP_DEBUG_PRINT_LEVEL BEFORE any "MicroPython's *.h" includes in two _.c files.
The MP_DEBUG_PRINT() worked only in one file. :(
It may be cmake
particularity, I don't know.
I keep it as is.
py/mpprint.h
Outdated
// In reality, you may use your own classification of debug levels. | ||
|
||
#if defined(MP_PRN) | ||
#undef MP_PRN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this under should be removed, and instead define the command below only if it's not already defined. It should ideally be possible for people to define their own command in mpconfigboard.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mprint.h
was included early in files and MP_DEBUG_PRINT
was defined previously as nothing
#define MP_DEBUG_PRINT(level, ...)
so we need undef and then define MP_DEBUG_PRINT again
#if defined(MP_DEBUG_PRINT)
#undef MP_DEBUG_PRINT
#endif
#define MP_DEBUG_PRINT(level, ...) \
do { \
...
Unfortunately, this blocks redefining in mpconfigboard.h
@projectgus |
@IhorNehrutsa Sure, I'm happy to! I have some questions about the implementation, but first I have two "big picture" questions:
To summarise my understanding of the current situation for extra printf debug output, the choices are:
Speaking for myself, I don't use option 1 much (too much output to be useful). I use option 2 sometimes. Difficulty mixing the simple printf hack with (I see @andrewleech essentially said similar things, above.)
Given how hacky option 2 is, and the fact |
5f85d61
to
214d7b6
Compare
When I set
I get Guru Meditation Error
|
1_694_032 is much smaller than 16_777_216! |
Oops! You are right! My guess is wrong. |
When I set MICROPY_DEBUG_VERBOSE and MICROPY_DEBUG_PRINTERS in micropython\ports\esp32\mpconfigport.h to
and add in single *.c source file
DEBUG_printf() works as expected in that file. |
Move MP_DEBUG_PRINT_XXX constants upper to the #if MICROPY_INCLUDED_PY_MPPRINT_H/#endif block. Co-Authored-By: Andrew Leech <3318786+andrewleech@users.noreply.github.com>
Add gaps between MP_DEBUG_PRINT_XXX levels. Move `How to use:` upper before #define MP_DEBUG_PRINT(). Print level names instead of the numbers. Signed-off-by: Ihor Nehrutsa <IhorNehrutsa@gmail.com>
214d7b6
to
e2acba6
Compare
Closed in favor #13400 |
Debugging macro for developers.
How to use: