rtdbg.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_LEVEL DBG_INFO
  22. * #include <rtdbg.h> // must after of DBG_LEVEL, DBG_SECTION_NAME 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. #ifndef DBG_SECTION_NAME
  49. #define DBG_SECTION_NAME "DBG"
  50. #endif
  51. #ifdef DBG_ENABLE
  52. #ifndef DBG_LEVEL
  53. #define DBG_LEVEL DBG_WARNING
  54. #endif
  55. /*
  56. * The color for terminal (foreground)
  57. * BLACK 30
  58. * RED 31
  59. * GREEN 32
  60. * YELLOW 33
  61. * BLUE 34
  62. * PURPLE 35
  63. * CYAN 36
  64. * WHITE 37
  65. */
  66. #ifdef DBG_COLOR
  67. #define _DBG_COLOR(n) rt_kprintf("\033["#n"m")
  68. #define _DBG_LOG_HDR(lvl_name, color_n) \
  69. rt_kprintf("\033["#color_n"m["lvl_name"/"DBG_SECTION_NAME"] ")
  70. #define _DBG_LOG_X_END \
  71. rt_kprintf("\033[0m\n")
  72. #else
  73. #define _DBG_COLOR(n)
  74. #define _DBG_LOG_HDR(lvl_name, color_n) \
  75. rt_kprintf("["lvl_name"/"DBG_SECTION_NAME"] ")
  76. #define _DBG_LOG_X_END \
  77. rt_kprintf("\n")
  78. #endif /* DBG_COLOR */
  79. /*
  80. * static debug routine
  81. * NOTE: This is a NOT RECOMMENDED API. Please using LOG_X API.
  82. * It will be DISCARDED later. Because it will take up more resources.
  83. */
  84. #define dbg_log(level, fmt, ...) \
  85. if ((level) <= DBG_LEVEL) \
  86. { \
  87. switch(level) \
  88. { \
  89. case DBG_ERROR: _DBG_LOG_HDR("E", 31); break; \
  90. case DBG_WARNING: _DBG_LOG_HDR("W", 33); break; \
  91. case DBG_INFO: _DBG_LOG_HDR("I", 32); break; \
  92. case DBG_LOG: _DBG_LOG_HDR("D", 0); break; \
  93. default: break; \
  94. } \
  95. rt_kprintf(fmt, ##__VA_ARGS__); \
  96. _DBG_COLOR(0); \
  97. }
  98. #define dbg_here \
  99. if ((DBG_LEVEL) <= DBG_LOG){ \
  100. rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n", \
  101. __FUNCTION__, __LINE__); \
  102. }
  103. #define dbg_log_line(lvl, color_n, fmt, ...) \
  104. do \
  105. { \
  106. _DBG_LOG_HDR(lvl, color_n); \
  107. rt_kprintf(fmt, ##__VA_ARGS__); \
  108. _DBG_LOG_X_END; \
  109. } \
  110. while (0)
  111. #define dbg_raw(...) rt_kprintf(__VA_ARGS__);
  112. #else
  113. #define dbg_log(level, fmt, ...)
  114. #define dbg_here
  115. #define dbg_enter
  116. #define dbg_exit
  117. #define dbg_log_line(lvl, color_n, fmt, ...)
  118. #define dbg_raw(...)
  119. #endif /* DBG_ENABLE */
  120. #if (DBG_LEVEL >= DBG_LOG)
  121. #define LOG_D(fmt, ...) dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
  122. #else
  123. #define LOG_D(...)
  124. #endif
  125. #if (DBG_LEVEL >= DBG_INFO)
  126. #define LOG_I(fmt, ...) dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
  127. #else
  128. #define LOG_I(...)
  129. #endif
  130. #if (DBG_LEVEL >= DBG_WARNING)
  131. #define LOG_W(fmt, ...) dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
  132. #else
  133. #define LOG_W(...)
  134. #endif
  135. #if (DBG_LEVEL >= DBG_ERROR)
  136. #define LOG_E(fmt, ...) dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
  137. #else
  138. #define LOG_E(...)
  139. #endif
  140. #define LOG_RAW(...) dbg_raw(__VA_ARGS__)
  141. #endif /* defined(RT_USING_ULOG) && define(DBG_ENABLE) */
  142. #endif /* RT_DBG_H__ */