diff --git a/py/mpprint.h b/py/mpprint.h old mode 100644 new mode 100755 index 8383ea85794c2..249d8b0b71326 --- a/py/mpprint.h +++ b/py/mpprint.h @@ -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