Skip to content

Commit

Permalink
Merge branch 'DBG_macro' into ESP32_PCNT_Encoder_Counter_HI_LOW
Browse files Browse the repository at this point in the history
  • Loading branch information
IhorNehrutsa committed Aug 18, 2023
2 parents aeed8a6 + f57a8b5 commit 5d080e1
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions py/mpprint.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,58 @@ int mp_printf(const mp_print_t *print, const char *fmt, ...);
int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args);
#endif

#if defined(MP_PRN_LEVEL) && (MP_PRN_LEVEL > 0)
// Debug messages during code developing with MP_PRN(level, ...) & MP_PRN_LEVEL.
// An approximate hierarchy of debug levels MP_PRN_LEVEL is:
#define MP_PRN_SUPPRESS 0 // SUPPRESS all messages. Use it in the release version.
#define MP_PRN_CRITICAL 1 // For the most CRITICAL errors, often requiring a system reset. Use a message with this level, if possible, raising an exception.
#define MP_PRN_ERROR 2 // ERROR requiring program restart, use message with this level before raising an exception.
#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.
// In reality, you may use your own classification of debug levels.

#if defined(MP_PRN)
#undef MP_PRN
#endif

#define MP_PRN(level, ...) \
do { \
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, " : FUNC=%s LINE=%d FILE=%s\n", __FUNCTION__, __LINE__, __FILE__); \
} \
} while (0);
#else
#define MP_PRN(level, ...)
#endif
/*
// How to use:
// Set MP_PRN_LEVEL in developed *.C or *.CPP file, for example
#define MP_PRN_LEVEL 1000 // show all messages
// Add MP_PRN() macro in code, like
void foo(int arg) {
MP_PRN(MP_PRN_TRACE, "Enter foo()")
if (arg < 0) {
MP_PRN(MP_PRN_WARNING, "arg=%d less zero", arg)
...
}
...
int value;
...
// calculate value
...
MP_PRN(MP_PRN_INFO, "See a value=%d", value)
...
MP_PRN(MP_PRN_TRACE, "Exit foo()")
}
// It is not a dogma. You may start debugging from level 3.
#define MP_PRN_LEVEL 3
// Then add MP_PRN(3, ...) and when gets too much messages then change some messages to the next level MP_PRN(4, ...), or MP_PRN(2, ...) etc.
// Then you may change MP_PRN_LEVEL to 2(reduce printing), and finally to 0(supress printing).
*/

#endif // MICROPY_INCLUDED_PY_MPPRINT_H

0 comments on commit 5d080e1

Please sign in to comment.