1 /*
2 * Copyright (c) 2022, sakumisu
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #ifndef USB_LOG_H
7 #define USB_LOG_H
8
9 #include <stdio.h>
10
11 /* DEBUG level */
12 #define USB_DBG_ERROR 0
13 #define USB_DBG_WARNING 1
14 #define USB_DBG_INFO 2
15 #define USB_DBG_LOG 3
16
17 #ifndef USB_DBG_TAG
18 #define USB_DBG_TAG "USB"
19 #endif
20 /*
21 * The color for terminal (foreground)
22 * BLACK 30
23 * RED 31
24 * GREEN 32
25 * YELLOW 33
26 * BLUE 34
27 * PURPLE 35
28 * CYAN 36
29 * WHITE 37
30 */
31
32 #ifdef CONFIG_USB_PRINTF_COLOR_ENABLE
33 #define _USB_DBG_COLOR(n) CONFIG_USB_PRINTF("\033[" #n "m")
34 #define _USB_DBG_LOG_HDR(lvl_name, color_n) \
35 CONFIG_USB_PRINTF("\033[" #color_n "m[" lvl_name "/" USB_DBG_TAG "] ")
36 #define _USB_DBG_LOG_X_END \
37 CONFIG_USB_PRINTF("\033[0m")
38 #else
39 #define _USB_DBG_COLOR(n)
40 #define _USB_DBG_LOG_HDR(lvl_name, color_n) \
41 CONFIG_USB_PRINTF("[" lvl_name "/" USB_DBG_TAG "] ")
42 #define _USB_DBG_LOG_X_END
43 #endif
44
45 #define usb_dbg_log_line(lvl, color_n, fmt, ...) \
46 do { \
47 _USB_DBG_LOG_HDR(lvl, color_n); \
48 CONFIG_USB_PRINTF(fmt, ##__VA_ARGS__); \
49 _USB_DBG_LOG_X_END; \
50 } while (0)
51
52 #if (CONFIG_USB_DBG_LEVEL >= USB_DBG_LOG)
53 #define USB_LOG_DBG(fmt, ...) usb_dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
54 #else
55 #define USB_LOG_DBG(...) {}
56 #endif
57
58 #if (CONFIG_USB_DBG_LEVEL >= USB_DBG_INFO)
59 #define USB_LOG_INFO(fmt, ...) usb_dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
60 #else
61 #define USB_LOG_INFO(...) {}
62 #endif
63
64 #if (CONFIG_USB_DBG_LEVEL >= USB_DBG_WARNING)
65 #define USB_LOG_WRN(fmt, ...) usb_dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
66 #else
67 #define USB_LOG_WRN(...) {}
68 #endif
69
70 #if (CONFIG_USB_DBG_LEVEL >= USB_DBG_ERROR)
71 #define USB_LOG_ERR(fmt, ...) usb_dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
72 #else
73 #define USB_LOG_ERR(...) {}
74 #endif
75
76 #define USB_LOG_RAW(...) CONFIG_USB_PRINTF(__VA_ARGS__)
77
78 #ifndef CONFIG_USB_ASSERT_DISABLE
79 #define USB_ASSERT(f) \
80 do { \
81 if (!(f)) { \
82 USB_LOG_ERR("ASSERT FAIL [%s] @ %s:%d\r\n", #f, __FILE__, __LINE__); \
83 while (1) { \
84 } \
85 } \
86 } while (false)
87
88 #define USB_ASSERT_MSG(f, fmt, ...) \
89 do { \
90 if (!(f)) { \
91 USB_LOG_ERR("ASSERT FAIL [%s] @ %s:%d\r\n", #f, __FILE__, __LINE__); \
92 USB_LOG_ERR(fmt "\r\n", ##__VA_ARGS__); \
93 while (1) { \
94 } \
95 } \
96 } while (false)
97 #else
98 #define USB_ASSERT(f) {}
99 #define USB_ASSERT_MSG(f, fmt, ...) {}
100 #endif
101
102 #define ___is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
usb_hexdump(const void * ptr,uint32_t buflen)103 static inline void usb_hexdump(const void *ptr, uint32_t buflen)
104 {
105 unsigned char *buf = (unsigned char *)ptr;
106 unsigned int i, j;
107
108 (void)buf;
109
110 for (i = 0; i < buflen; i += 16) {
111 CONFIG_USB_PRINTF("%08x:", i);
112
113 for (j = 0; j < 16; j++)
114 if (i + j < buflen) {
115 if ((j % 8) == 0) {
116 CONFIG_USB_PRINTF(" ");
117 }
118
119 CONFIG_USB_PRINTF("%02X ", buf[i + j]);
120 } else
121 CONFIG_USB_PRINTF(" ");
122 CONFIG_USB_PRINTF(" ");
123
124 for (j = 0; j < 16; j++)
125 if (i + j < buflen)
126 CONFIG_USB_PRINTF("%c", ___is_print(buf[i + j]) ? buf[i + j] : '.');
127 CONFIG_USB_PRINTF("\n");
128 }
129 }
130
131 #endif /* USB_LOG_H */
132