Skip to content

Commit 5950e47

Browse files
committed
py/mpprint.h: Add MP_PRN() macro.
1 parent 4837ec3 commit 5950e47

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

py/mpprint.h

100644100755
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,59 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args);
8080
#endif
8181

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

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