rtdbg.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_LOG 0
  54. #define DBG_INFO 1
  55. #define DBG_WARNING 2
  56. #define DBG_ERROR 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. #else
  80. #define _DBG_COLOR(n)
  81. #define _DBG_LOG_HDR(lvl_name, color_n) \
  82. rt_kprintf("["lvl_name"/"DBG_SECTION_NAME"] ")
  83. #endif /* DBG_COLOR */
  84. /*
  85. * static debug routine
  86. */
  87. #define dbg_log(level, fmt, ...) \
  88. if ((level) >= DBG_LEVEL) \
  89. { \
  90. switch(level) \
  91. { \
  92. case DBG_ERROR: _DBG_LOG_HDR("E", 31); break; \
  93. case DBG_WARNING: _DBG_LOG_HDR("W", 33); break; \
  94. case DBG_INFO: _DBG_LOG_HDR("I", 32); break; \
  95. case DBG_LOG: _DBG_LOG_HDR("D", 0); break; \
  96. default: break; \
  97. } \
  98. rt_kprintf(fmt, ##__VA_ARGS__); \
  99. _DBG_COLOR(0); \
  100. }
  101. #define dbg_here \
  102. if ((DBG_LEVEL) >= DBG_LOG){ \
  103. rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n", \
  104. __FUNCTION__, __LINE__); \
  105. }
  106. #define dbg_enter \
  107. if ((DBG_LEVEL) >= DBG_LOG){ \
  108. _DBG_COLOR(32); \
  109. rt_kprintf(DBG_SECTION_NAME " Enter %s\n", \
  110. __FUNCTION__); \
  111. _DBG_COLOR(0); \
  112. }
  113. #define dbg_exit \
  114. if ((DBG_LEVEL) >= DBG_LOG){ \
  115. _DBG_COLOR(32); \
  116. rt_kprintf(DBG_SECTION_NAME " Exit %s:%d\n", \
  117. __FUNCTION__); \
  118. _DBG_COLOR(0); \
  119. }
  120. #define dbg_log_line(level, ...) \
  121. dbg_log(level, __VA_ARGS__); \
  122. if ((level) >= DBG_LEVEL) { \
  123. rt_kprintf("\n"); \
  124. }
  125. #define dbg_raw(...) rt_kprintf(__VA_ARGS__);
  126. #else
  127. #define dbg_log(level, fmt, ...)
  128. #define dbg_here
  129. #define dbg_enter
  130. #define dbg_exit
  131. #define dbg_log_line(level, ...)
  132. #define dbg_raw(...)
  133. #endif
  134. #define LOG_D(...) dbg_log_line(DBG_LOG , __VA_ARGS__)
  135. #define LOG_I(...) dbg_log_line(DBG_INFO , __VA_ARGS__)
  136. #define LOG_W(...) dbg_log_line(DBG_WARNING, __VA_ARGS__)
  137. #define LOG_E(...) dbg_log_line(DBG_ERROR , __VA_ARGS__)
  138. #define LOG_RAW(...) dbg_raw(__VA_ARGS__)
  139. #endif /* RT_DBG_H__ */