usb_log.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #define ___is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  74. static inline void usb_hexdump(const void *ptr, uint32_t buflen)
  75. {
  76. unsigned char *buf = (unsigned char *)ptr;
  77. uint32_t i, j;
  78. for (i = 0; i < buflen; i += 16) {
  79. CONFIG_USB_PRINTF("%08X:", i);
  80. for (j = 0; j < 16; j++)
  81. if (i + j < buflen) {
  82. if ((j % 8) == 0) {
  83. CONFIG_USB_PRINTF(" ");
  84. }
  85. CONFIG_USB_PRINTF("%02X ", buf[i + j]);
  86. } else
  87. CONFIG_USB_PRINTF(" ");
  88. CONFIG_USB_PRINTF(" ");
  89. for (j = 0; j < 16; j++)
  90. if (i + j < buflen)
  91. CONFIG_USB_PRINTF("%c", ___is_print(buf[i + j]) ? buf[i + j] : '.');
  92. CONFIG_USB_PRINTF("\n");
  93. }
  94. }
  95. #endif /* USB_LOG_H */