diff --git a/drivers/esp-hosted/esp_hosted_hal.h b/drivers/esp-hosted/esp_hosted_hal.h index 0bc7db928235a..aa275fd28c239 100644 --- a/drivers/esp-hosted/esp_hosted_hal.h +++ b/drivers/esp-hosted/esp_hosted_hal.h @@ -29,36 +29,6 @@ #ifndef MICROPY_INCLUDED_DRIVERS_ESP_HOSTED_HAL_H #define MICROPY_INCLUDED_DRIVERS_ESP_HOSTED_HAL_H -#ifndef ESP_HOSTED_DEBUG -#define ESP_HOSTED_DEBUG (0) -#endif - -#if ESP_HOSTED_DEBUG -#define PROTOBUF_C_UNPACK_ERROR(...) error_printf(__VA_ARGS__); -#endif - -#define ANSI_C_RED "\x1B[31m" -#define ANSI_C_GREEN "\x1B[32m" -#define ANSI_C_YELLOW "\x1B[33m" -#define ANSI_C_BLUE "\x1B[34m" -#define ANSI_C_MAGENTA "\x1B[35m" -#define ANSI_C_CYAN "\x1B[36m" -#define ANSI_C_WHITE "\x1B[37m" -#define ANSI_C_DEFAULT "\x1B[0m" - -#if ESP_HOSTED_DEBUG -#define do_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) -#else -#define do_printf(...) -#endif - -// Logging macros. -#define debug_printf(...) do_printf(ANSI_C_BLUE); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); -#define info_printf(...) do_printf(ANSI_C_GREEN); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); -#define warn_printf(...) do_printf(ANSI_C_YELLOW); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); -#define error_printf(...) do_printf(ANSI_C_RED); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); -#define crit_printf(...) do_printf(ANSI_C_MAGENTA); do_printf(__VA_ARGS__); do_printf(ANSI_C_DEFAULT); - typedef enum { ESP_HOSTED_MODE_BT, ESP_HOSTED_MODE_WIFI, diff --git a/drivers/ninaw10/nina_bt_hci.c b/drivers/ninaw10/nina_bt_hci.c index f0d1b9bc8986b..1a4092b7cf49b 100644 --- a/drivers/ninaw10/nina_bt_hci.c +++ b/drivers/ninaw10/nina_bt_hci.c @@ -34,6 +34,8 @@ #include #include +#define DO_PRINTF (DO_PRINTF_SUPPRESS) + #include "py/runtime.h" #include "extmod/mpbthci.h" @@ -50,9 +52,6 @@ #define OCF_SET_EVENT_MASK (0x0001) #define OCF_RESET (0x0003) -#define error_printf(...) // mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) -#define debug_printf(...) // mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) - // Provided by the port, and also possibly shared with the stack. extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; diff --git a/drivers/ninaw10/nina_wifi_bsp.c b/drivers/ninaw10/nina_wifi_bsp.c index dc71c8260c61f..f528a9818e57b 100644 --- a/drivers/ninaw10/nina_wifi_bsp.c +++ b/drivers/ninaw10/nina_wifi_bsp.c @@ -27,6 +27,10 @@ * NINA-W10 driver BSP implementation. */ +#if NINA_DEBUG +#define DO_PRINTF (DO_PRINTF_DEBUG) +#endif + #include "py/mphal.h" #if MICROPY_PY_NETWORK_NINAW10 @@ -40,12 +44,6 @@ #include "nina_bsp.h" #include "nina_wifi_drv.h" -#if NINA_DEBUG -#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) -#else -#define debug_printf(...) -#endif - int nina_bsp_init(void) { mp_hal_pin_output(MICROPY_HW_NINA_GPIO1); mp_hal_pin_input(MICROPY_HW_NINA_ACK); diff --git a/drivers/ninaw10/nina_wifi_drv.c b/drivers/ninaw10/nina_wifi_drv.c index 6e4df84294c3d..e24a646a7ece1 100644 --- a/drivers/ninaw10/nina_wifi_drv.c +++ b/drivers/ninaw10/nina_wifi_drv.c @@ -27,6 +27,10 @@ * NINA-W10 WiFi driver. */ +#if NINA_DEBUG +#define DO_PRINTF (DO_PRINTF_DEBUG) +#endif + #include "py/mphal.h" #include "py/mperrno.h" @@ -36,6 +40,7 @@ #include #include +#include "py/mpprint.h" #include "nina_bsp.h" #include "nina_wifi_drv.h" @@ -63,12 +68,6 @@ #define NINA_SSELECT_TIMEOUT (1000) #define NINA_CONNECT_TIMEOUT (10000) -#if NINA_DEBUG -#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) -#else -#define debug_printf(...) -#endif - #ifndef __REVSH #define __REVSH(x) ((((uint16_t)x) << 8) | (((uint16_t)x) >> 8)) #endif diff --git a/extmod/network_esp_hosted.c b/extmod/network_esp_hosted.c index 1a3be3f39a1a1..1cc95844481a3 100644 --- a/extmod/network_esp_hosted.c +++ b/extmod/network_esp_hosted.c @@ -25,6 +25,10 @@ * * ESP-Hosted network interface. */ +#ifndef ESP_HOSTED_DEBUG +#define ESP_HOSTED_DEBUG (0) +#endif +#define DO_PRINTF (ESP_HOSTED_DEBUG) #include "py/mphal.h" diff --git a/extmod/network_ninaw10.c b/extmod/network_ninaw10.c index 1deb117e70689..eb12e9954fe37 100644 --- a/extmod/network_ninaw10.c +++ b/extmod/network_ninaw10.c @@ -30,6 +30,8 @@ #if MICROPY_PY_NETWORK && MICROPY_PY_NETWORK_NINAW10 +#define DO_PRINTF (DO_PRINTF_SUPPRESS) + #include #include #include @@ -77,8 +79,6 @@ typedef struct _nina_obj_t { #define is_nonblocking_error(errno) ((errno) == MP_EAGAIN || (errno) == MP_EWOULDBLOCK || (errno) == MP_EINPROGRESS) -#define debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__) - static uint16_t bind_port = BIND_PORT_RANGE_MIN; const mp_obj_type_t mod_network_nic_type_nina; static nina_obj_t network_nina_wl_sta = {{(mp_obj_type_t *)&mod_network_nic_type_nina}, false, false, MOD_NETWORK_STA_IF}; diff --git a/ports/mimxrt/sdio.c b/ports/mimxrt/sdio.c index 2fa0f228fe3a5..08b8be421af99 100644 --- a/ports/mimxrt/sdio.c +++ b/ports/mimxrt/sdio.c @@ -24,6 +24,10 @@ * THE SOFTWARE. */ +#if SDIO_DEBUG +#define DO_PRINTF (DO_PRINTF_DEBUG) +#endif + #include #include "py/mperrno.h" #include "py/mphal.h" @@ -79,12 +83,6 @@ #define SDMMC_CLOCK_25MHZ (25000000U) #define SDMMC_CLOCK_50MHZ (50000000U) -#if SDIO_DEBUG -#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) -#else -#define debug_printf(...) -#endif - #define DMA_DESCRIPTOR_BUFFER_SIZE (32U) AT_NONCACHEABLE_SECTION_ALIGN( static uint32_t sdio_adma_descriptor_table[DMA_DESCRIPTOR_BUFFER_SIZE], USDHC_ADMA2_ADDRESS_ALIGN); diff --git a/ports/renesas-ra/mpbthciport.c b/ports/renesas-ra/mpbthciport.c index 2e9f6d916698b..503a64496a402 100644 --- a/ports/renesas-ra/mpbthciport.c +++ b/ports/renesas-ra/mpbthciport.c @@ -25,6 +25,8 @@ * THE SOFTWARE. */ +#define DO_PRINTF (DO_PRINTF_ERROR) + #include "py/runtime.h" #include "py/stream.h" #include "py/mphal.h" @@ -37,9 +39,6 @@ #if MICROPY_PY_BLUETOOTH -#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) -#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) - uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; STATIC mp_sched_node_t mp_bluetooth_hci_sched_node; diff --git a/ports/rp2/mpbthciport.c b/ports/rp2/mpbthciport.c index 1aacbe881f9f0..31882a427b950 100644 --- a/ports/rp2/mpbthciport.c +++ b/ports/rp2/mpbthciport.c @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +#define DO_PRINTF (DO_PRINTF_ERROR) + #include "py/runtime.h" #include "py/stream.h" #include "py/mphal.h" @@ -36,9 +38,6 @@ #if MICROPY_PY_BLUETOOTH -#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) -#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) - uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; // Soft timer and scheduling node for scheduling a HCI poll. diff --git a/py/mpprint.h b/py/mpprint.h index 8383ea85794c2..cafcc5df1446c 100644 --- a/py/mpprint.h +++ b/py/mpprint.h @@ -79,4 +79,93 @@ int mp_printf(const mp_print_t *print, const char *fmt, ...); int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args); #endif +#define ANSI_C_MAGENTA "\x1B[35m" +#define ANSI_C_RED "\x1B[31m" +#define ANSI_C_YELLOW "\x1B[33m" +#define ANSI_C_GREEN "\x1B[32m" +#define ANSI_C_BLUE "\x1B[34m" +#define ANSI_C_CYAN "\x1B[36m" +#define ANSI_C_WHITE "\x1B[37m" +#define ANSI_C_DEFAULT "\x1B[0m" + +#define DO_PRINTF_SUPPRESS 0 // SUPPRESS all messages. Use it in the release version. +#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. +#define DO_PRINTF_ERROR 2 // ERROR requiring program restart, use message with this level before raising an exception. +#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. +#define DO_PRINTF_INFO 4 // INFO, it is interesting and useful for understanding a bug. +#define DO_PRINTF_DEBUG 5 // DEBUG, more detailed information, dig deeper. +#define DO_PRINTF_TRACE 6 // TRACE, show a flow of the algorithm, like enter/exit a function. + +#ifndef DO_PRINTF +#define DO_PRINTF (DO_PRINTF_SUPPRESS) +#endif + +#if DO_PRINTF +#define do_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) +#define do_print_str(str) mp_print_str(&mp_plat_print, str) +#define do_print_line() mp_printf(&mp_plat_print, "\t : FUNC=%s LINE=%d FILE=%s\n", __FUNCTION__, __LINE__, __FILE__) +#else +#define do_printf(...) +#define do_print_str(str) +#define do_print_line() +#endif + +// Logging macros. +#if DO_PRINTF >= DO_PRINTF_CRITICAL +#define crit_printf(...) do_print_str(ANSI_C_MAGENTA); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT); +#else +#define crit_printf(...) +#endif + +#if DO_PRINTF >= DO_PRINTF_ERROR +#define error_printf(...) do_print_str(ANSI_C_RED); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT); +#else +#define error_printf(...) +#endif + +#if DO_PRINTF >= DO_PRINTF_WARN +#define warn_printf(...) do_print_str(ANSI_C_YELLOW); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT); +#else +#define warn_printf(...) +#endif + +#if DO_PRINTF >= DO_PRINTF_INFO +#define info_printf(...) do_print_str(ANSI_C_GREEN); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT); +#else +#define info_printf(...) +#endif + +#if DO_PRINTF >= DO_PRINTF_DEBUG +#define debug_printf(...) do_print_str(ANSI_C_BLUE); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT); +#else +#define debug_printf(...) +#endif + +#if DO_PRINTF >= DO_PRINTF_TRACE +#define trace_printf(...) do_print_str(ANSI_C_CYAN); do_printf(__VA_ARGS__); do_print_line(); do_print_str(ANSI_C_DEFAULT); +#else +#define trace_printf(...) +#endif + +#if defined(DO_PRINTF) && (DO_PRINTF > 0) + +#if defined(MP_DEBUG_PRINT) +#undef MP_DEBUG_PRINT +#endif + +#define MP_DEBUG_PRINT(level, ...) \ + do { \ + if ((0 < level) && (level <= DO_PRINTF)) { \ + 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 + #endif // MICROPY_INCLUDED_PY_MPPRINT_H