rtdbg.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2016-11-12 Bernard The first version
  9. * 2018-05-25 armink Add simple API, such as LOG_D, LOG_E
  10. */
  11. /*
  12. * The macro definitions for debug
  13. *
  14. * These macros are defined in static. If you want to use debug macro, you can
  15. * use as following code:
  16. *
  17. * In your C/C++ file, enable/disable DEBUG_ENABLE macro, and then include this
  18. * header file.
  19. *
  20. * #define DBG_TAG "MOD_TAG"
  21. * #define DBG_LVL DBG_INFO
  22. * #include <rtdbg.h> // must after of DBG_LVL, DBG_TAG or other options
  23. *
  24. * Then in your C/C++ file, you can use LOG_X macro to print out logs:
  25. * LOG_D("this is a debug log!");
  26. * LOG_E("this is a error log!");
  27. */
  28. #ifndef RT_DBG_H__
  29. #define RT_DBG_H__
  30. #include <rtconfig.h>
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* the debug log will force enable when RT_USING_DEBUG macro is defined */
  35. #if defined(RT_USING_DEBUG) && !defined(DBG_ENABLE)
  36. #define DBG_ENABLE
  37. #endif
  38. /* it will force output color log when RT_DEBUGING_COLOR macro is defined */
  39. #if defined(RT_DEBUGING_COLOR) && !defined(DBG_COLOR)
  40. #define DBG_COLOR
  41. #endif
  42. /* for dlog */
  43. #ifdef PKG_USING_DLOG
  44. #include <dlog.h>
  45. #else
  46. #define DLOG(...)
  47. #endif
  48. #if defined(RT_USING_ULOG)
  49. /* using ulog compatible with rtdbg */
  50. #include <ulog.h>
  51. #else
  52. /* DEBUG level */
  53. #define DBG_ERROR 0
  54. #define DBG_WARNING 1
  55. #define DBG_INFO 2
  56. #define DBG_LOG 3
  57. #ifdef DBG_TAG
  58. #ifndef DBG_SECTION_NAME
  59. #define DBG_SECTION_NAME DBG_TAG
  60. #endif
  61. #else
  62. /* compatible with old version */
  63. #ifndef DBG_SECTION_NAME
  64. #define DBG_SECTION_NAME "DBG"
  65. #endif
  66. #endif /* DBG_TAG */
  67. #ifdef DBG_ENABLE
  68. #ifdef DBG_LVL
  69. #ifndef DBG_LEVEL
  70. #define DBG_LEVEL DBG_LVL
  71. #endif
  72. #else
  73. /* compatible with old version */
  74. #ifndef DBG_LEVEL
  75. #define DBG_LEVEL DBG_WARNING
  76. #endif
  77. #endif /* DBG_LVL */
  78. /*
  79. * The color for terminal (foreground)
  80. * BLACK 30
  81. * RED 31
  82. * GREEN 32
  83. * YELLOW 33
  84. * BLUE 34
  85. * PURPLE 35
  86. * CYAN 36
  87. * WHITE 37
  88. */
  89. #ifdef DBG_COLOR
  90. #define _DBG_COLOR(n) rt_kprintf("\033["#n"m")
  91. #define _DBG_LOG_HDR(lvl_name, color_n) \
  92. rt_kprintf("\033["#color_n"m[" lvl_name "/" DBG_SECTION_NAME "] ")
  93. #define _DBG_LOG_X_END \
  94. rt_kprintf("\033[0m\n")
  95. #else
  96. #define _DBG_COLOR(n)
  97. #define _DBG_LOG_HDR(lvl_name, color_n) \
  98. rt_kprintf("[" lvl_name "/" DBG_SECTION_NAME "] ")
  99. #define _DBG_LOG_X_END \
  100. rt_kprintf("\n")
  101. #endif /* DBG_COLOR */
  102. #define dbg_log_line(lvl, color_n, fmt, ...) \
  103. do \
  104. { \
  105. _DBG_LOG_HDR(lvl, color_n); \
  106. rt_kprintf(fmt, ##__VA_ARGS__); \
  107. _DBG_LOG_X_END; \
  108. } \
  109. while (0)
  110. #define dbg_raw(...) rt_kprintf(__VA_ARGS__);
  111. #else
  112. #define dbg_log_line(lvl, color_n, fmt, ...)
  113. #define dbg_raw(...)
  114. #endif /* DBG_ENABLE */
  115. #if (DBG_LEVEL >= DBG_LOG)
  116. #define LOG_D(fmt, ...) dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
  117. #else
  118. #define LOG_D(...)
  119. #endif
  120. #if (DBG_LEVEL >= DBG_INFO)
  121. #define LOG_I(fmt, ...) dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
  122. #else
  123. #define LOG_I(...)
  124. #endif
  125. #if (DBG_LEVEL >= DBG_WARNING)
  126. #define LOG_W(fmt, ...) dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
  127. #else
  128. #define LOG_W(...)
  129. #endif
  130. #if (DBG_LEVEL >= DBG_ERROR)
  131. #define LOG_E(fmt, ...) dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
  132. #else
  133. #define LOG_E(...)
  134. #endif
  135. #define LOG_RAW(...) dbg_raw(__VA_ARGS__)
  136. #define LOG_HEX(name, width, buf, size)
  137. #endif /* defined(RT_USING_ULOG) && define(DBG_ENABLE) */
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #endif /* RT_DBG_H__ */