Skip to content

Commit e88200f

Browse files
committed
py/mpprint.h: Move HOWTO upper.
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>
1 parent 7c86e1b commit e88200f

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

py/mpprint.h

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,45 +82,33 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args);
8282
// Debug messages during code developing with MP_DEBUG_PRINT(level, ...) & MP_DEBUG_PRINT_LEVEL.
8383
// An approximate hierarchy of debug levels MP_DEBUG_PRINT_LEVEL is:
8484
#define MP_DEBUG_PRINT_SUPPRESS 0 // SUPPRESS all messages. Use it in the release version.
85-
#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.
86-
#define MP_DEBUG_PRINT_ERROR 2 // ERROR requiring program restart, use message with this level before raising an exception.
87-
#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.
88-
#define MP_DEBUG_PRINT_INFO 4 // INFO, it is interesting and useful for understanding a bug.
89-
#define MP_DEBUG_PRINT_DEBUG 5 // DEBUG, more detailed information, dig deeper.
90-
#define MP_DEBUG_PRINT_TRACE 6 // TRACE, show a flow of the algorithm, like enter/exit a function.
85+
#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.
86+
#define MP_DEBUG_PRINT_ERROR 20 // ERROR requiring program restart, use message with this level before raising an exception.
87+
#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.
88+
#define MP_DEBUG_PRINT_INFO 40 // INFO, it is interesting and useful for understanding a bug.
89+
#define MP_DEBUG_PRINT_DEBUG 50 // DEBUG, more detailed information, dig deeper.
90+
#define MP_DEBUG_PRINT_TRACE 60 // TRACE, show a flow of the algorithm, like enter/exit a function.
9191
// In reality, you may use your own classification of debug levels.
9292

9393
#endif // MICROPY_INCLUDED_PY_MPPRINT_H
9494

9595
// This code is placed after `#endif // MICROPY_INCLUDED_PY_MPPRINT_H` to allow the developer
9696
// to use several local `MP_DEBUG_PRINT_LEVEL` definitions in separate _.c files.
9797
// This is not a typo or a bug.
98-
#if defined(MP_DEBUG_PRINT_LEVEL) && (MP_DEBUG_PRINT_LEVEL > 0)
99-
100-
#if defined(MP_DEBUG_PRINT)
101-
#undef MP_DEBUG_PRINT
102-
#endif
10398

104-
#define MP_DEBUG_PRINT(level, ...) \
105-
do { \
106-
if ((0 < level) && (level <= MP_DEBUG_PRINT_LEVEL)) { \
107-
mp_printf(MP_PYTHON_PRINTER, " MP_DEBUG_PRINT_LEVEL=%d : ", level); \
108-
mp_printf(MP_PYTHON_PRINTER, __VA_ARGS__); \
109-
mp_printf(MP_PYTHON_PRINTER, "\t : FUNC=%s LINE=%d FILE=%s\n", __FUNCTION__, __LINE__, __FILE__); \
110-
} \
111-
} while (0);
112-
113-
#else
114-
115-
#define MP_DEBUG_PRINT(level, ...)
116-
117-
#endif
11899
/*
100+
// Debugging macro for developers.
101+
119102
// How to use:
120-
// Set MP_DEBUG_PRINT_LEVEL in developed *.C or *.CPP file, for example
121-
#define MP_DEBUG_PRINT_LEVEL 1000 // show all messages
103+
// Important! Set MP_DEBUG_PRINT_LEVEL in *.c or *.cpp development file BEFORE any "MicroPython's *.h" includes.
104+
// For example:
105+
#define MP_DEBUG_PRINT_LEVEL MP_DEBUG_PRINT_TRACE // show all messages
122106
// Include mpprint.h after defining the MP_DEBUG_PRINT_LEVEL
123107
#include "py/mpprint.h"
108+
...
109+
#include "py/obj.h"
110+
#include "py/runtime.h"
111+
...
124112
125113
// Add MP_DEBUG_PRINT() macro in code, like
126114
void foo(int arg) {
@@ -139,10 +127,30 @@ void foo(int arg) {
139127
MP_DEBUG_PRINT(MP_DEBUG_PRINT_TRACE, "Exit foo()")
140128
}
141129
142-
// It is not a dogma. You may start debugging from level 3.
143-
#define MP_DEBUG_PRINT_LEVEL 3
144-
// 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.
145-
// Then you may change MP_DEBUG_PRINT_LEVEL to 2(reduce printing), and finally to 0(suppress printing).
130+
// It is not a dogma. You may start debugging from level 30.
131+
#define MP_DEBUG_PRINT_LEVEL 30
132+
// 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.
133+
// Then you may change MP_DEBUG_PRINT_LEVEL to 20(reduce printing), and finally to 0(suppress printing).
146134
147135
// 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.
148136
*/
137+
#if defined(MP_DEBUG_PRINT_LEVEL) && (MP_DEBUG_PRINT_LEVEL > 0)
138+
139+
#if defined(MP_DEBUG_PRINT)
140+
#undef MP_DEBUG_PRINT
141+
#endif
142+
143+
#define MP_DEBUG_PRINT(level, ...) \
144+
do { \
145+
if ((0 < level) && (level <= MP_DEBUG_PRINT_LEVEL)) { \
146+
mp_printf(MP_PYTHON_PRINTER, " %s: ", #level); \
147+
mp_printf(MP_PYTHON_PRINTER, __VA_ARGS__); \
148+
mp_printf(MP_PYTHON_PRINTER, "\t : FUNC=%s LINE=%d FILE=%s\n", __FUNCTION__, __LINE__, __FILE__); \
149+
} \
150+
} while (0);
151+
152+
#else
153+
154+
#define MP_DEBUG_PRINT(level, ...)
155+
156+
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy