rtdbg.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * #undef DBG_SECTION_NAME // avoid the macro is already defined
  35. * #undef DBG_LEVEL
  36. * #undef DBG_COLOR
  37. * #undef DBG_ENABLE
  38. *
  39. * #define DBG_SECTION_NAME "[ MOD]"
  40. * #define DBG_ENABLE // enable debug macro
  41. * #define DBG_LEVEL DBG_INFO
  42. * #include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
  43. *
  44. * Then in your C/C++ file, you can use dbg_log macro to print out logs:
  45. * dbg_log(DBG_INFO, "this is a log!\n");
  46. *
  47. * Or if you want to using the simple API, you can
  48. * LOG_D("this is a debug log!");
  49. * LOG_E("this is a error log!");
  50. *
  51. * If you want to use different color for different kinds log, you can
  52. * #define DBG_COLOR
  53. */
  54. #ifndef RT_DBG_H__
  55. #define RT_DBG_H__
  56. #include <rtconfig.h>
  57. /* DEBUG level */
  58. #define DBG_LOG 0
  59. #define DBG_INFO 1
  60. #define DBG_WARNING 2
  61. #define DBG_ERROR 3
  62. #ifndef DBG_SECTION_NAME
  63. #define DBG_SECTION_NAME "[ DBG]"
  64. #endif
  65. #ifdef DBG_ENABLE
  66. #ifndef DBG_LEVEL
  67. #define DBG_LEVEL DBG_WARNING
  68. #endif
  69. /*
  70. * The color for terminal (foreground)
  71. * BLACK 30
  72. * RED 31
  73. * GREEN 32
  74. * YELLOW 33
  75. * BLUE 34
  76. * PURPLE 35
  77. * CYAN 36
  78. * WHITE 37
  79. */
  80. #ifdef DBG_COLOR
  81. #define _DBG_COLOR(n) rt_kprintf("\033["#n"m")
  82. #else
  83. #define _DBG_COLOR(n)
  84. #endif
  85. /*
  86. * static debug routine
  87. */
  88. #define dbg_log(level, fmt, ...) \
  89. if ((level) >= DBG_LEVEL) \
  90. { \
  91. switch(level) \
  92. { \
  93. case DBG_ERROR: _DBG_COLOR(31); break; \
  94. case DBG_WARNING: _DBG_COLOR(33); break; \
  95. case DBG_INFO: _DBG_COLOR(32); break; \
  96. default: break; \
  97. } \
  98. rt_kprintf(DBG_SECTION_NAME 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. #else
  126. #define dbg_log(level, fmt, ...)
  127. #define dbg_here
  128. #define dbg_enter
  129. #define dbg_exit
  130. #define dbg_log_line(level, ...)
  131. #endif
  132. #define LOG_D(...) dbg_log_line(DBG_LOG , __VA_ARGS__)
  133. #define LOG_I(...) dbg_log_line(DBG_INFO , __VA_ARGS__)
  134. #define LOG_W(...) dbg_log_line(DBG_WARNING, __VA_ARGS__)
  135. #define LOG_E(...) dbg_log_line(DBG_ERROR , __VA_ARGS__)
  136. #endif /* RT_DBG_H__ */