rtdbg.h 5.2 KB

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