rtdbg.h 5.3 KB

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