Skip to content

Commit 9729a5e

Browse files
committed
py/mpprint.h: Add DBG() macro.
Debugging macro for developers. py/mprint.h: Change names to MP_PRN & MP_PRN_LEVEL. py\mpprint.h: Change MP_PRN() delimiter to '|'. py\mpprint.h: MP_PRN() is off by default. py\mpprint.h: Add __LINE__ and __FILE__. py/mpprint.h: Add MP_PRN_levels
1 parent 4837ec3 commit 9729a5e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

py/mpprint.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,61 @@ int mp_printf(const mp_print_t *print, const char *fmt, ...);
7979
int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args);
8080
#endif
8181

82+
// Debug messages during code developing with MP_PRN(level, ...) & MP_PRN_LEVEL.
83+
// An approximate hierarchy of debug levels MP_PRN_LEVEL is:
84+
#define MP_PRN_SUPPRESS 0 // SUPPRESS all messages. Use it in the release version.
85+
#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.
86+
#define MP_PRN_ERROR 2 // ERROR requiring program restart, use message with this level before raising an exception.
87+
#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.
88+
#define MP_PRN_INFO 4 // INFO, it is interesting and useful for understanding a bug.
89+
#define MP_PRN_DEBUG 5 // DEBUG, more detailed information, dig deeper.
90+
#define MP_PRN_TRACE 6 // TRACE, show a flow of the algorithm, like enter/exit a function.
91+
// In reality, you may use your own classification of debug levels.
92+
#if defined(MP_PRN_LEVEL) && (MP_PRN_LEVEL > 0)
93+
#define MP_PRN(level, ...) \
94+
do { \
95+
if ((0 < level) && (level <= MP_PRN_LEVEL)) { \
96+
mp_printf(MP_PYTHON_PRINTER, " %d|| ", level); \
97+
mp_printf(MP_PYTHON_PRINTER, __VA_ARGS__); \
98+
mp_printf(MP_PYTHON_PRINTER, " ||%d %s\n", __LINE__, __FILE__); \
99+
} \
100+
} while (0);
101+
#else
102+
#define MP_PRN(level, ...)
103+
#endif
104+
/*
105+
// How to use:
106+
// Set MP_PRN_LEVEL in developed *.C or *.CPP file, for example
107+
108+
#define MP_PRN_LEVEL 1000 // show all messages
109+
110+
// Add MP_PRN() macro in code, like
111+
void foo() {
112+
MP_PRN(6, "Enter foo()")
113+
...
114+
int value;
115+
...
116+
// calculate value
117+
...
118+
MP_PRN(4, "See a value=%d", value)
119+
...
120+
MP_PRN(6, "Exit foo()")
121+
}
122+
123+
// It is not a dogma. You may start debugging from level 3.
124+
#define MP_PRN_LEVEL 3
125+
// 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.
126+
// Then you may change MP_PRN_LEVEL to 2(reduce printing), and finally to 0(supress printing).
127+
*/
128+
129+
#if 0
130+
#if MICROPY_DEBUG_VERBOSE // print debugging info
131+
#define DEBUG_PRINT (1)
132+
#define DEBUG_printf(...) MP_PRN(MP_PRN_DEBUG, __VA_ARGS__)
133+
#else // don't print debugging info
134+
#define DEBUG_PRINT (0)
135+
#define DEBUG_printf(...)
136+
#endif
137+
#endif
138+
82139
#endif // MICROPY_INCLUDED_PY_MPPRINT_H

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