Skip to content

Commit

Permalink
py/mpprint.h: Add do_printf() macro.
Browse files Browse the repository at this point in the history
Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com>
  • Loading branch information
IhorNehrutsa authored and IhorNehrutsa committed Jan 12, 2024
1 parent 2ed976f commit d809c8b
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 60 deletions.
30 changes: 0 additions & 30 deletions drivers/esp-hosted/esp_hosted_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions drivers/ninaw10/nina_bt_hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <stdio.h>
#include <string.h>

#define DO_PRINTF (0)

#include "py/runtime.h"
#include "extmod/mpbthci.h"

Expand All @@ -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];

Expand Down
8 changes: 2 additions & 6 deletions drivers/ninaw10/nina_wifi_bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* NINA-W10 driver BSP implementation.
*/

#define DO_PRINTF (NINA_DEBUG)

#include "py/mphal.h"

#if MICROPY_PY_NETWORK_NINAW10
Expand All @@ -40,12 +42,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);
Expand Down
9 changes: 3 additions & 6 deletions drivers/ninaw10/nina_wifi_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* NINA-W10 WiFi driver.
*/

#define DO_PRINTF (NINA_DEBUG)

#include "py/mphal.h"
#include "py/mperrno.h"

Expand All @@ -36,6 +38,7 @@
#include <string.h>
#include <stdio.h>

#include "py/mpprint.h"
#include "nina_bsp.h"
#include "nina_wifi_drv.h"

Expand Down Expand Up @@ -63,12 +66,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
Expand Down
6 changes: 5 additions & 1 deletion extmod/network_esp_hosted.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -45,8 +49,8 @@
#include "extmod/modnetwork.h"
#include "modmachine.h"

#include "esp_hosted_wifi.h"
#include "esp_hosted_hal.h"
#include "esp_hosted_wifi.h"

typedef struct _esp_hosted_obj_t {
mp_obj_base_t base;
Expand Down
4 changes: 2 additions & 2 deletions extmod/network_ninaw10.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#if MICROPY_PY_NETWORK && MICROPY_PY_NETWORK_NINAW10

#define DO_PRINTF (0)

#include <string.h>
#include <stdio.h>
#include <stdarg.h>
Expand Down Expand Up @@ -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};
Expand Down
8 changes: 2 additions & 6 deletions ports/mimxrt/sdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* THE SOFTWARE.
*/

#define DO_PRINTF (SDIO_DEBUG)

#include <stdio.h>
#include "py/mperrno.h"
#include "py/mphal.h"
Expand Down Expand Up @@ -79,12 +81,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);
Expand Down
5 changes: 2 additions & 3 deletions ports/renesas-ra/mpbthciport.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* THE SOFTWARE.
*/

#define DO_PRINTF (0)

#include "py/runtime.h"
#include "py/stream.h"
#include "py/mphal.h"
Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions ports/rp2/mpbthciport.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* THE SOFTWARE.
*/

#define DO_PRINTF (0)

#include "py/runtime.h"
#include "py/stream.h"
#include "py/mphal.h"
Expand All @@ -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.
Expand Down
89 changes: 89 additions & 0 deletions py/mpprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d809c8b

Please sign in to comment.