rtdbg.h 5.4 KB

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