rtdbg.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2006-2018, 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. /* the debug log will force enable when RT_DEBUG macro is defined */
  32. #if defined(RT_DEBUG) && !defined(DBG_ENABLE)
  33. #define DBG_ENABLE
  34. #endif
  35. /* it will force output color log when RT_DEBUG_COLOR macro is defined */
  36. #if defined(RT_DEBUG_COLOR) && !defined(DBG_COLOR)
  37. #define DBG_COLOR
  38. #endif
  39. #if defined(RT_USING_ULOG)
  40. /* using ulog compatible with rtdbg */
  41. #include <ulog.h>
  42. #else
  43. /* DEBUG level */
  44. #define DBG_ERROR 0
  45. #define DBG_WARNING 1
  46. #define DBG_INFO 2
  47. #define DBG_LOG 3
  48. #ifdef DBG_TAG
  49. #ifndef DBG_SECTION_NAME
  50. #define DBG_SECTION_NAME DBG_TAG
  51. #endif
  52. #else
  53. /* compatible with old version */
  54. #ifndef DBG_SECTION_NAME
  55. #define DBG_SECTION_NAME "DBG"
  56. #endif
  57. #endif /* DBG_TAG */
  58. #ifdef DBG_ENABLE
  59. #ifdef DBG_LVL
  60. #ifndef DBG_LEVEL
  61. #define DBG_LEVEL DBG_LVL
  62. #endif
  63. #else
  64. /* compatible with old version */
  65. #ifndef DBG_LEVEL
  66. #define DBG_LEVEL DBG_WARNING
  67. #endif
  68. #endif /* DBG_LVL */
  69. /*
  70. * The color for terminal (foreground)
  71. * BLACK 30
  72. * RED 31
  73. * GREEN 32
  74. * YELLOW 33
  75. * BLUE 34
  76. * PURPLE 35
  77. * CYAN 36
  78. * WHITE 37
  79. */
  80. #ifdef DBG_COLOR
  81. #define _DBG_COLOR(n) rt_kprintf("\033["#n"m")
  82. #define _DBG_LOG_HDR(lvl_name, color_n) \
  83. rt_kprintf("\033["#color_n"m["lvl_name"/"DBG_SECTION_NAME"] ")
  84. #define _DBG_LOG_X_END \
  85. rt_kprintf("\033[0m\n")
  86. #else
  87. #define _DBG_COLOR(n)
  88. #define _DBG_LOG_HDR(lvl_name, color_n) \
  89. rt_kprintf("["lvl_name"/"DBG_SECTION_NAME"] ")
  90. #define _DBG_LOG_X_END \
  91. rt_kprintf("\n")
  92. #endif /* DBG_COLOR */
  93. /*
  94. * static debug routine
  95. * NOTE: This is a NOT RECOMMENDED API. Please using LOG_X API.
  96. * It will be DISCARDED later. Because it will take up more resources.
  97. */
  98. #define dbg_log(level, fmt, ...) \
  99. if ((level) <= DBG_LEVEL) \
  100. { \
  101. switch(level) \
  102. { \
  103. case DBG_ERROR: _DBG_LOG_HDR("E", 31); break; \
  104. case DBG_WARNING: _DBG_LOG_HDR("W", 33); break; \
  105. case DBG_INFO: _DBG_LOG_HDR("I", 32); break; \
  106. case DBG_LOG: _DBG_LOG_HDR("D", 0); break; \
  107. default: break; \
  108. } \
  109. rt_kprintf(fmt, ##__VA_ARGS__); \
  110. _DBG_COLOR(0); \
  111. }
  112. #define dbg_here \
  113. if ((DBG_LEVEL) <= DBG_LOG){ \
  114. rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n", \
  115. __FUNCTION__, __LINE__); \
  116. }
  117. #define dbg_log_line(lvl, color_n, fmt, ...) \
  118. do \
  119. { \
  120. _DBG_LOG_HDR(lvl, color_n); \
  121. rt_kprintf(fmt, ##__VA_ARGS__); \
  122. _DBG_LOG_X_END; \
  123. } \
  124. while (0)
  125. #define dbg_raw(...) rt_kprintf(__VA_ARGS__);
  126. #else
  127. #define dbg_log(level, fmt, ...)
  128. #define dbg_here
  129. #define dbg_enter
  130. #define dbg_exit
  131. #define dbg_log_line(lvl, color_n, fmt, ...)
  132. #define dbg_raw(...)
  133. #endif /* DBG_ENABLE */
  134. #if (DBG_LEVEL >= DBG_LOG)
  135. #define LOG_D(fmt, ...) dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
  136. #else
  137. #define LOG_D(...)
  138. #endif
  139. #if (DBG_LEVEL >= DBG_INFO)
  140. #define LOG_I(fmt, ...) dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
  141. #else
  142. #define LOG_I(...)
  143. #endif
  144. #if (DBG_LEVEL >= DBG_WARNING)
  145. #define LOG_W(fmt, ...) dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
  146. #else
  147. #define LOG_W(...)
  148. #endif
  149. #if (DBG_LEVEL >= DBG_ERROR)
  150. #define LOG_E(fmt, ...) dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
  151. #else
  152. #define LOG_E(...)
  153. #endif
  154. #define LOG_RAW(...) dbg_raw(__VA_ARGS__)
  155. #endif /* defined(RT_USING_ULOG) && define(DBG_ENABLE) */
  156. #endif /* RT_DBG_H__ */