Skip to content

Commit fa7922a

Browse files
IhorNehrutsaIhorNehrutsa
IhorNehrutsa
authored and
IhorNehrutsa
committed
py/mpprint.h: Add do_printf() macro.
Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com>
1 parent 2ed976f commit fa7922a

File tree

10 files changed

+109
-60
lines changed

10 files changed

+109
-60
lines changed

drivers/esp-hosted/esp_hosted_hal.h

-30
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,6 @@
2929
#ifndef MICROPY_INCLUDED_DRIVERS_ESP_HOSTED_HAL_H
3030
#define MICROPY_INCLUDED_DRIVERS_ESP_HOSTED_HAL_H
3131

32-
#ifndef ESP_HOSTED_DEBUG
33-
#define ESP_HOSTED_DEBUG (0)
34-
#endif
35-
36-
#if ESP_HOSTED_DEBUG
37-
#define PROTOBUF_C_UNPACK_ERROR(...) error_printf(__VA_ARGS__);
38-
#endif
39-
40-
#define ANSI_C_RED "\x1B[31m"
41-
#define ANSI_C_GREEN "\x1B[32m"
42-
#define ANSI_C_YELLOW "\x1B[33m"
43-
#define ANSI_C_BLUE "\x1B[34m"
44-
#define ANSI_C_MAGENTA "\x1B[35m"
45-
#define ANSI_C_CYAN "\x1B[36m"
46-
#define ANSI_C_WHITE "\x1B[37m"
47-
#define ANSI_C_DEFAULT "\x1B[0m"
48-
49-
#if ESP_HOSTED_DEBUG
50-
#define do_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__)
51-
#else
52-
#define do_printf(...)
53-
#endif
54-
55-
// Logging macros.
56-
#define debug_printf(...) do_printf(ANSI_C_BLUE); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT);
57-
#define info_printf(...) do_printf(ANSI_C_GREEN); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT);
58-
#define warn_printf(...) do_printf(ANSI_C_YELLOW); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT);
59-
#define error_printf(...) do_printf(ANSI_C_RED); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT);
60-
#define crit_printf(...) do_printf(ANSI_C_MAGENTA); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT);
61-
6232
typedef enum {
6333
ESP_HOSTED_MODE_BT,
6434
ESP_HOSTED_MODE_WIFI,

drivers/ninaw10/nina_bt_hci.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <stdio.h>
3535
#include <string.h>
3636

37+
// #define DO_PRINTF (1)
38+
3739
#include "py/runtime.h"
3840
#include "extmod/mpbthci.h"
3941

@@ -50,9 +52,6 @@
5052
#define OCF_SET_EVENT_MASK (0x0001)
5153
#define OCF_RESET (0x0003)
5254

53-
#define error_printf(...) // mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__)
54-
#define debug_printf(...) // mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__)
55-
5655
// Provided by the port, and also possibly shared with the stack.
5756
extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
5857

drivers/ninaw10/nina_wifi_bsp.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* NINA-W10 driver BSP implementation.
2828
*/
2929

30+
#define DO_PRINTF (NINA_DEBUG)
31+
3032
#include "py/mphal.h"
3133

3234
#if MICROPY_PY_NETWORK_NINAW10
@@ -40,12 +42,6 @@
4042
#include "nina_bsp.h"
4143
#include "nina_wifi_drv.h"
4244

43-
#if NINA_DEBUG
44-
#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__)
45-
#else
46-
#define debug_printf(...)
47-
#endif
48-
4945
int nina_bsp_init(void) {
5046
mp_hal_pin_output(MICROPY_HW_NINA_GPIO1);
5147
mp_hal_pin_input(MICROPY_HW_NINA_ACK);

drivers/ninaw10/nina_wifi_drv.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* NINA-W10 WiFi driver.
2828
*/
2929

30+
#define DO_PRINTF (NINA_DEBUG)
31+
3032
#include "py/mphal.h"
3133
#include "py/mperrno.h"
3234

@@ -36,6 +38,7 @@
3638
#include <string.h>
3739
#include <stdio.h>
3840

41+
#include "py/mpprint.h"
3942
#include "nina_bsp.h"
4043
#include "nina_wifi_drv.h"
4144

@@ -63,12 +66,6 @@
6366
#define NINA_SSELECT_TIMEOUT (1000)
6467
#define NINA_CONNECT_TIMEOUT (10000)
6568

66-
#if NINA_DEBUG
67-
#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__)
68-
#else
69-
#define debug_printf(...)
70-
#endif
71-
7269
#ifndef __REVSH
7370
#define __REVSH(x) ((((uint16_t)x) << 8) | (((uint16_t)x) >> 8))
7471
#endif

extmod/network_esp_hosted.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
*
2626
* ESP-Hosted network interface.
2727
*/
28+
#ifndef ESP_HOSTED_DEBUG
29+
#define ESP_HOSTED_DEBUG (0)
30+
#endif
31+
#define DO_PRINTF (ESP_HOSTED_DEBUG)
2832

2933
#include "py/mphal.h"
3034

@@ -45,8 +49,8 @@
4549
#include "extmod/modnetwork.h"
4650
#include "modmachine.h"
4751

48-
#include "esp_hosted_wifi.h"
4952
#include "esp_hosted_hal.h"
53+
#include "esp_hosted_wifi.h"
5054

5155
typedef struct _esp_hosted_obj_t {
5256
mp_obj_base_t base;

extmod/network_ninaw10.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#if MICROPY_PY_NETWORK && MICROPY_PY_NETWORK_NINAW10
3232

33+
// #define DO_PRINTF (1)
34+
3335
#include <string.h>
3436
#include <stdio.h>
3537
#include <stdarg.h>
@@ -77,8 +79,6 @@ typedef struct _nina_obj_t {
7779

7880
#define is_nonblocking_error(errno) ((errno) == MP_EAGAIN || (errno) == MP_EWOULDBLOCK || (errno) == MP_EINPROGRESS)
7981

80-
#define debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__)
81-
8282
static uint16_t bind_port = BIND_PORT_RANGE_MIN;
8383
const mp_obj_type_t mod_network_nic_type_nina;
8484
static nina_obj_t network_nina_wl_sta = {{(mp_obj_type_t *)&mod_network_nic_type_nina}, false, false, MOD_NETWORK_STA_IF};

ports/mimxrt/sdio.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#define DO_PRINTF (SDIO_DEBUG)
28+
2729
#include <stdio.h>
2830
#include "py/mperrno.h"
2931
#include "py/mphal.h"
@@ -79,12 +81,6 @@
7981
#define SDMMC_CLOCK_25MHZ (25000000U)
8082
#define SDMMC_CLOCK_50MHZ (50000000U)
8183

82-
#if SDIO_DEBUG
83-
#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__)
84-
#else
85-
#define debug_printf(...)
86-
#endif
87-
8884
#define DMA_DESCRIPTOR_BUFFER_SIZE (32U)
8985
AT_NONCACHEABLE_SECTION_ALIGN(
9086
static uint32_t sdio_adma_descriptor_table[DMA_DESCRIPTOR_BUFFER_SIZE], USDHC_ADMA2_ADDRESS_ALIGN);

ports/renesas-ra/mpbthciport.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
// #define DO_PRINTF (1)
29+
2830
#include "py/runtime.h"
2931
#include "py/stream.h"
3032
#include "py/mphal.h"
@@ -37,9 +39,6 @@
3739

3840
#if MICROPY_PY_BLUETOOTH
3941

40-
#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
41-
#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
42-
4342
uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
4443

4544
STATIC mp_sched_node_t mp_bluetooth_hci_sched_node;

ports/rp2/mpbthciport.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
// #define DO_PRINTF (1)
28+
2729
#include "py/runtime.h"
2830
#include "py/stream.h"
2931
#include "py/mphal.h"
@@ -36,9 +38,6 @@
3638

3739
#if MICROPY_PY_BLUETOOTH
3840

39-
#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
40-
#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
41-
4241
uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
4342

4443
// Soft timer and scheduling node for scheduling a HCI poll.

py/mpprint.h

+89
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,93 @@ 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+
#define ANSI_C_MAGENTA "\x1B[35m"
83+
#define ANSI_C_RED "\x1B[31m"
84+
#define ANSI_C_YELLOW "\x1B[33m"
85+
#define ANSI_C_GREEN "\x1B[32m"
86+
#define ANSI_C_BLUE "\x1B[34m"
87+
#define ANSI_C_CYAN "\x1B[36m"
88+
#define ANSI_C_WHITE "\x1B[37m"
89+
#define ANSI_C_DEFAULT "\x1B[0m"
90+
91+
#define DO_PRINTF_SUPPRESS 0 // SUPPRESS all messages. Use it in the release version.
92+
#define DO_PRINTF_CRIT 1 // For the most CRITICAL errors, often requiring a system reset. Use a message with this level, if possible, raising an exception.
93+
#define DO_PRINTF_ERROR 2 // ERROR requiring program restart, use message with this level before raising an exception.
94+
#define DO_PRINTF_WARN 3 // WARNING, something went wrong, but you can fix it with additional operations in code right now or may ignore it.
95+
#define DO_PRINTF_INFO 4 // INFO, it is interesting and useful for understanding a bug.
96+
#define DO_PRINTF_DEBUG 5 // DEBUG, more detailed information, dig deeper.
97+
#define DO_PRINTF_TRACE 6 // TRACE, show a flow of the algorithm, like enter/exit a function.
98+
99+
#ifndef DO_PRINTF
100+
#define DO_PRINTF (DO_PRINTF_SUPPRESS)
101+
#endif
102+
103+
#if DO_PRINTF
104+
#define do_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__)
105+
#define do_print_str(str) mp_print_str(&mp_plat_print, str)
106+
#define do_print_line() mp_printf(&mp_plat_print, "\t : FUNC=%s LINE=%d FILE=%s\n", __FUNCTION__, __LINE__, __FILE__)
107+
#else
108+
#define do_printf(...)
109+
#define do_print_str(str)
110+
#define do_print_line()
111+
#endif
112+
113+
// Logging macros.
114+
#if DO_PRINTF >= DO_PRINTF_CRITICAL
115+
#define crit_printf(...) do_print_str(ANSI_C_MAGENTA); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT);
116+
#else
117+
#define crit_printf(...)
118+
#endif
119+
120+
#if DO_PRINTF >= DO_PRINTF_ERROR
121+
#define error_printf(...) do_print_str(ANSI_C_RED); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT);
122+
#else
123+
#define error_printf(...)
124+
#endif
125+
126+
#if DO_PRINTF >= DO_PRINTF_WARN
127+
#define warn_printf(...) do_print_str(ANSI_C_YELLOW); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT);
128+
#else
129+
#define warn_printf(...)
130+
#endif
131+
132+
#if DO_PRINTF >= DO_PRINTF_INFO
133+
#define info_printf(...) do_print_str(ANSI_C_GREEN); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT);
134+
#else
135+
#define info_printf(...)
136+
#endif
137+
138+
#if DO_PRINTF >= DO_PRINTF_DEBUG
139+
#define debug_printf(...) do_print_str(ANSI_C_BLUE); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT);
140+
#else
141+
#define debug_printf(...)
142+
#endif
143+
144+
#if DO_PRINTF >= DO_PRINTF_TRACE
145+
#define trace_printf(...) do_print_str(ANSI_C_CYAN); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT);
146+
#else
147+
#define trace_printf(...)
148+
#endif
149+
150+
#if defined(DO_PRINTF) && (DO_PRINTF > 0)
151+
152+
#if defined(MP_DEBUG_PRINT)
153+
#undef MP_DEBUG_PRINT
154+
#endif
155+
156+
#define MP_DEBUG_PRINT(level, ...) \
157+
do { \
158+
if ((0 < level) && (level <= DO_PRINTF)) { \
159+
mp_printf(MP_PYTHON_PRINTER, " %s: ", #level); \
160+
mp_printf(MP_PYTHON_PRINTER, __VA_ARGS__); \
161+
mp_printf(MP_PYTHON_PRINTER, "\t : FUNC=%s LINE=%d FILE=%s\n", __FUNCTION__, __LINE__, __FILE__); \
162+
} \
163+
} while (0);
164+
165+
#else
166+
167+
#define MP_DEBUG_PRINT(level, ...)
168+
169+
#endif
170+
82171
#endif // MICROPY_INCLUDED_PY_MPPRINT_H

0 commit comments

Comments
 (0)