usb_log.h 2.1 KB

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