usb_log.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef _USB_LOG_H
  2. #define _USB_LOG_H
  3. #include <stdio.h>
  4. /* DEBUG level */
  5. #define USB_DBG_ERROR 0
  6. #define USB_DBG_WARNING 1
  7. #define USB_DBG_INFO 2
  8. #define USB_DBG_LOG 3
  9. #ifndef CONFIG_USB_DBG_LEVEL
  10. #define CONFIG_USB_DBG_LEVEL USB_DBG_INFO
  11. #endif
  12. #ifndef USB_DBG_TAG
  13. #define USB_DBG_TAG "USB"
  14. #endif
  15. /*
  16. * The color for terminal (foreground)
  17. * BLACK 30
  18. * RED 31
  19. * GREEN 32
  20. * YELLOW 33
  21. * BLUE 34
  22. * PURPLE 35
  23. * CYAN 36
  24. * WHITE 37
  25. */
  26. #ifndef CONFIG_USB_PRINTF
  27. #define CONFIG_USB_PRINTF printf
  28. #endif
  29. #ifdef CONFIG_USB_PRINTF_COLOR_ENABLE
  30. #define _USB_DBG_COLOR(n) CONFIG_USB_PRINTF("\033[" #n "m")
  31. #define _USB_DBG_LOG_HDR(lvl_name, color_n) \
  32. CONFIG_USB_PRINTF("\033[" #color_n "m[" lvl_name "/" USB_DBG_TAG "] ")
  33. #define _USB_DBG_LOG_X_END \
  34. CONFIG_USB_PRINTF("\033[0m")
  35. #else
  36. #define _USB_DBG_COLOR(n)
  37. #define _USB_DBG_LOG_HDR(lvl_name, color_n) \
  38. CONFIG_USB_PRINTF("[" lvl_name "/" USB_DBG_TAG "] ")
  39. #define _USB_DBG_LOG_X_END
  40. #endif
  41. #define usb_dbg_log_line(lvl, color_n, fmt, ...) \
  42. do { \
  43. _USB_DBG_LOG_HDR(lvl, color_n); \
  44. CONFIG_USB_PRINTF(fmt, ##__VA_ARGS__); \
  45. _USB_DBG_LOG_X_END; \
  46. } while (0)
  47. #if (CONFIG_USB_DBG_LEVEL >= USB_DBG_LOG)
  48. #define USB_LOG_DBG(fmt, ...) usb_dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
  49. #else
  50. #define USB_LOG_DBG(...)
  51. #endif
  52. #if (CONFIG_USB_DBG_LEVEL >= USB_DBG_INFO)
  53. #define USB_LOG_INFO(fmt, ...) usb_dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
  54. #else
  55. #define USB_LOG_INFO(...)
  56. #endif
  57. #if (CONFIG_USB_DBG_LEVEL >= USB_DBG_WARNING)
  58. #define USB_LOG_WRN(fmt, ...) usb_dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
  59. #else
  60. #define USB_LOG_WRN(...)
  61. #endif
  62. #if (CONFIG_USB_DBG_LEVEL >= USB_DBG_ERROR)
  63. #define USB_LOG_ERR(fmt, ...) usb_dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
  64. #else
  65. #define USB_LOG_ERR(...)
  66. #endif
  67. #define USB_LOG_RAW CONFIG_USB_PRINTF
  68. void usb_assert(const char *filename, int linenum);
  69. #define USB_ASSERT(f) \
  70. do { \
  71. if (!(f)) \
  72. usb_assert(__FILE__, __LINE__); \
  73. } while (0)
  74. #endif