Skip to content

Commit

Permalink
py/mpprint.h: Move HOWTO upper.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
IhorNehrutsa committed Oct 6, 2023
1 parent 7c86e1b commit e88200f
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions py/mpprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,45 +82,33 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args);
// Debug messages during code developing with MP_DEBUG_PRINT(level, ...) & MP_DEBUG_PRINT_LEVEL.
// An approximate hierarchy of debug levels MP_DEBUG_PRINT_LEVEL is:
#define MP_DEBUG_PRINT_SUPPRESS 0 // SUPPRESS all messages. Use it in the release version.
#define MP_DEBUG_PRINT_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_DEBUG_PRINT_ERROR 2 // ERROR requiring program restart, use message with this level before raising an exception.
#define MP_DEBUG_PRINT_WARNING 3 // WARNING, something went wrong, but you can fix it with additional operations in code right now or may ignore it.
#define MP_DEBUG_PRINT_INFO 4 // INFO, it is interesting and useful for understanding a bug.
#define MP_DEBUG_PRINT_DEBUG 5 // DEBUG, more detailed information, dig deeper.
#define MP_DEBUG_PRINT_TRACE 6 // TRACE, show a flow of the algorithm, like enter/exit a function.
#define MP_DEBUG_PRINT_CRITICAL 10 // For the most CRITICAL errors, often requiring a system reset. Use a message with this level, if possible, raising an exception.
#define MP_DEBUG_PRINT_ERROR 20 // ERROR requiring program restart, use message with this level before raising an exception.
#define MP_DEBUG_PRINT_WARNING 30 // WARNING, something went wrong, but you can fix it with additional operations in code right now or may ignore it.
#define MP_DEBUG_PRINT_INFO 40 // INFO, it is interesting and useful for understanding a bug.
#define MP_DEBUG_PRINT_DEBUG 50 // DEBUG, more detailed information, dig deeper.
#define MP_DEBUG_PRINT_TRACE 60 // TRACE, show a flow of the algorithm, like enter/exit a function.
// In reality, you may use your own classification of debug levels.

#endif // MICROPY_INCLUDED_PY_MPPRINT_H

// This code is placed after `#endif // MICROPY_INCLUDED_PY_MPPRINT_H` to allow the developer
// to use several local `MP_DEBUG_PRINT_LEVEL` definitions in separate _.c files.
// This is not a typo or a bug.
#if defined(MP_DEBUG_PRINT_LEVEL) && (MP_DEBUG_PRINT_LEVEL > 0)

#if defined(MP_DEBUG_PRINT)
#undef MP_DEBUG_PRINT
#endif

#define MP_DEBUG_PRINT(level, ...) \
do { \
if ((0 < level) && (level <= MP_DEBUG_PRINT_LEVEL)) { \
mp_printf(MP_PYTHON_PRINTER, " MP_DEBUG_PRINT_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__); \
} \
} while (0);

#else

#define MP_DEBUG_PRINT(level, ...)

#endif
/*
// Debugging macro for developers.
// How to use:
// Set MP_DEBUG_PRINT_LEVEL in developed *.C or *.CPP file, for example
#define MP_DEBUG_PRINT_LEVEL 1000 // show all messages
// Important! Set MP_DEBUG_PRINT_LEVEL in *.c or *.cpp development file BEFORE any "MicroPython's *.h" includes.
// For example:
#define MP_DEBUG_PRINT_LEVEL MP_DEBUG_PRINT_TRACE // show all messages
// Include mpprint.h after defining the MP_DEBUG_PRINT_LEVEL
#include "py/mpprint.h"
...
#include "py/obj.h"
#include "py/runtime.h"
...
// Add MP_DEBUG_PRINT() macro in code, like
void foo(int arg) {
Expand All @@ -139,10 +127,30 @@ void foo(int arg) {
MP_DEBUG_PRINT(MP_DEBUG_PRINT_TRACE, "Exit foo()")
}
// It is not a dogma. You may start debugging from level 3.
#define MP_DEBUG_PRINT_LEVEL 3
// Then add MP_DEBUG_PRINT(3, ...) and when gets too many messages then change some messages to the next level MP_DEBUG_PRINT(4, ...), or MP_DEBUG_PRINT(2, ...) etc.
// Then you may change MP_DEBUG_PRINT_LEVEL to 2(reduce printing), and finally to 0(suppress printing).
// It is not a dogma. You may start debugging from level 30.
#define MP_DEBUG_PRINT_LEVEL 30
// Then add MP_DEBUG_PRINT(30, ...) and when gets too many messages then change some messages to the next level MP_DEBUG_PRINT(40, ...), or MP_DEBUG_PRINT(20, ...) etc.
// Then you may change MP_DEBUG_PRINT_LEVEL to 20(reduce printing), and finally to 0(suppress printing).
// Usually, you will debug one or two source files. Debug printing from other files is suppressed if MP_DEBUG_PRINT_LEVEL is 0 or undefined.
*/
#if defined(MP_DEBUG_PRINT_LEVEL) && (MP_DEBUG_PRINT_LEVEL > 0)

#if defined(MP_DEBUG_PRINT)
#undef MP_DEBUG_PRINT
#endif

#define MP_DEBUG_PRINT(level, ...) \
do { \
if ((0 < level) && (level <= MP_DEBUG_PRINT_LEVEL)) { \
mp_printf(MP_PYTHON_PRINTER, " %s: ", #level); \
mp_printf(MP_PYTHON_PRINTER, __VA_ARGS__); \
mp_printf(MP_PYTHON_PRINTER, "\t : FUNC=%s LINE=%d FILE=%s\n", __FUNCTION__, __LINE__, __FILE__); \
} \
} while (0);

#else

#define MP_DEBUG_PRINT(level, ...)

#endif

0 comments on commit e88200f

Please sign in to comment.