rtdbg.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /*
  103. * static debug routine
  104. * NOTE: This is a NOT RECOMMENDED API. Please using LOG_X API.
  105. * It will be DISCARDED later. Because it will take up more resources.
  106. */
  107. #define dbg_log(level, fmt, ...) \
  108. if ((level) <= DBG_LEVEL) \
  109. { \
  110. switch(level) \
  111. { \
  112. case DBG_ERROR: _DBG_LOG_HDR("E", 31); break; \
  113. case DBG_WARNING: _DBG_LOG_HDR("W", 33); break; \
  114. case DBG_INFO: _DBG_LOG_HDR("I", 32); break; \
  115. case DBG_LOG: _DBG_LOG_HDR("D", 0); break; \
  116. default: break; \
  117. } \
  118. rt_kprintf(fmt, ##__VA_ARGS__); \
  119. _DBG_COLOR(0); \
  120. }
  121. #define dbg_here \
  122. if ((DBG_LEVEL) <= DBG_LOG){ \
  123. rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n", \
  124. __FUNCTION__, __LINE__); \
  125. }
  126. #define dbg_log_line(lvl, color_n, fmt, ...) \
  127. do \
  128. { \
  129. _DBG_LOG_HDR(lvl, color_n); \
  130. rt_kprintf(fmt, ##__VA_ARGS__); \
  131. _DBG_LOG_X_END; \
  132. } \
  133. while (0)
  134. #define dbg_raw(...) rt_kprintf(__VA_ARGS__);
  135. #else
  136. #define dbg_log(level, fmt, ...)
  137. #define dbg_here
  138. #define dbg_enter
  139. #define dbg_exit
  140. #define dbg_log_line(lvl, color_n, fmt, ...)
  141. #define dbg_raw(...)
  142. #endif /* DBG_ENABLE */
  143. #if (DBG_LEVEL >= DBG_LOG)
  144. #define LOG_D(fmt, ...) dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
  145. #else
  146. #define LOG_D(...)
  147. #endif
  148. #if (DBG_LEVEL >= DBG_INFO)
  149. #define LOG_I(fmt, ...) dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
  150. #else
  151. #define LOG_I(...)
  152. #endif
  153. #if (DBG_LEVEL >= DBG_WARNING)
  154. #define LOG_W(fmt, ...) dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
  155. #else
  156. #define LOG_W(...)
  157. #endif
  158. #if (DBG_LEVEL >= DBG_ERROR)
  159. #define LOG_E(fmt, ...) dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
  160. #else
  161. #define LOG_E(...)
  162. #endif
  163. #define LOG_RAW(...) dbg_raw(__VA_ARGS__)
  164. #define LOG_HEX(name, width, buf, size)
  165. #endif /* defined(RT_USING_ULOG) && define(DBG_ENABLE) */
  166. #ifdef __cplusplus
  167. }
  168. #endif
  169. #endif /* RT_DBG_H__ */