rtdbg.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * File : rtdbg.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2016-11-12 Bernard The first version
  23. * 2018-05-25 armink Add simple API, such as LOG_D, LOG_E
  24. */
  25. /*
  26. * The macro definitions for debug
  27. *
  28. * These macros are defined in static. If you want to use debug macro, you can
  29. * use as following code:
  30. *
  31. * In your C/C++ file, enable/disable DEBUG_ENABLE macro, and then include this
  32. * header file.
  33. *
  34. * #define DBG_SECTION_NAME "MOD"
  35. * #define DBG_ENABLE // enable debug macro
  36. * #define DBG_LEVEL DBG_INFO
  37. * #include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
  38. *
  39. * Then in your C/C++ file, you can use dbg_log macro to print out logs:
  40. * dbg_log(DBG_INFO, "this is a log!\n");
  41. *
  42. * Or if you want to using the simple API, you can
  43. * LOG_D("this is a debug log!");
  44. * LOG_E("this is a error log!");
  45. *
  46. * If you want to use different color for different kinds log, you can
  47. * #define DBG_COLOR
  48. */
  49. #ifndef RT_DBG_H__
  50. #define RT_DBG_H__
  51. #include <rtconfig.h>
  52. /* DEBUG level */
  53. #define DBG_ERROR 0
  54. #define DBG_WARNING 1
  55. #define DBG_INFO 2
  56. #define DBG_LOG 3
  57. #ifndef DBG_SECTION_NAME
  58. #define DBG_SECTION_NAME "DBG"
  59. #endif
  60. #ifdef DBG_ENABLE
  61. #ifndef DBG_LEVEL
  62. #define DBG_LEVEL DBG_WARNING
  63. #endif
  64. /*
  65. * The color for terminal (foreground)
  66. * BLACK 30
  67. * RED 31
  68. * GREEN 32
  69. * YELLOW 33
  70. * BLUE 34
  71. * PURPLE 35
  72. * CYAN 36
  73. * WHITE 37
  74. */
  75. #ifdef DBG_COLOR
  76. #define _DBG_COLOR(n) rt_kprintf("\033["#n"m")
  77. #define _DBG_LOG_HDR(lvl_name, color_n) \
  78. rt_kprintf("\033["#color_n"m["lvl_name"/"DBG_SECTION_NAME"] ")
  79. #define _DBG_LOG_X_END \
  80. rt_kprintf("\033[0m\n")
  81. #else
  82. #define _DBG_COLOR(n)
  83. #define _DBG_LOG_HDR(lvl_name, color_n) \
  84. rt_kprintf("["lvl_name"/"DBG_SECTION_NAME"] ")
  85. #define _DBG_LOG_X_END \
  86. rt_kprintf("\n")
  87. #endif /* DBG_COLOR */
  88. /*
  89. * static debug routine
  90. */
  91. #define dbg_log(level, fmt, ...) \
  92. if ((level) <= DBG_LEVEL) \
  93. { \
  94. switch(level) \
  95. { \
  96. case DBG_ERROR: _DBG_LOG_HDR("E", 31); break; \
  97. case DBG_WARNING: _DBG_LOG_HDR("W", 33); break; \
  98. case DBG_INFO: _DBG_LOG_HDR("I", 32); break; \
  99. case DBG_LOG: _DBG_LOG_HDR("D", 0); break; \
  100. default: break; \
  101. } \
  102. rt_kprintf(fmt, ##__VA_ARGS__); \
  103. _DBG_COLOR(0); \
  104. }
  105. #define dbg_here \
  106. if ((DBG_LEVEL) <= DBG_LOG){ \
  107. rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n", \
  108. __FUNCTION__, __LINE__); \
  109. }
  110. #define dbg_enter \
  111. if ((DBG_LEVEL) <= DBG_LOG){ \
  112. _DBG_COLOR(32); \
  113. rt_kprintf(DBG_SECTION_NAME " Enter %s\n", \
  114. __FUNCTION__); \
  115. _DBG_COLOR(0); \
  116. }
  117. #define dbg_exit \
  118. if ((DBG_LEVEL) <= DBG_LOG){ \
  119. _DBG_COLOR(32); \
  120. rt_kprintf(DBG_SECTION_NAME " Exit %s:%d\n", \
  121. __FUNCTION__); \
  122. _DBG_COLOR(0); \
  123. }
  124. #define dbg_log_line(lvl, color_n, fmt, ...) \
  125. do \
  126. { \
  127. _DBG_LOG_HDR(lvl, color_n); \
  128. rt_kprintf(fmt, ##__VA_ARGS__); \
  129. _DBG_LOG_X_END; \
  130. } \
  131. while (0)
  132. #define dbg_raw(...) rt_kprintf(__VA_ARGS__);
  133. #else
  134. #define dbg_log(level, fmt, ...)
  135. #define dbg_here
  136. #define dbg_enter
  137. #define dbg_exit
  138. #define dbg_log_line(lvl, color_n, fmt, ...)
  139. #define dbg_raw(...)
  140. #endif /* DBG_ENABLE */
  141. #if (DBG_LEVEL >= DBG_LOG)
  142. #define LOG_D(fmt, ...) dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
  143. #else
  144. #define LOG_D(...)
  145. #endif
  146. #if (DBG_LEVEL >= DBG_INFO)
  147. #define LOG_I(fmt, ...) dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
  148. #else
  149. #define LOG_I(...)
  150. #endif
  151. #if (DBG_LEVEL >= DBG_WARNING)
  152. #define LOG_W(fmt, ...) dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
  153. #else
  154. #define LOG_W(...)
  155. #endif
  156. #if (DBG_LEVEL >= DBG_ERROR)
  157. #define LOG_E(fmt, ...) dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
  158. #else
  159. #define LOG_E(...)
  160. #endif
  161. #define LOG_RAW(...) dbg_raw(__VA_ARGS__)
  162. #endif /* RT_DBG_H__ */