• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void usb_assert(const char *filename, int linenum);
79 #define USB_ASSERT(f)                       \
80     do {                                    \
81         if (!(f))                           \
82             usb_assert(__FILE__, __LINE__); \
83     } while (0)
84 
85 #endif /* USB_LOG_H */
86