rtdbg.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #else
  78. #define _DBG_COLOR(n)
  79. #endif
  80. /*
  81. * static debug routine
  82. */
  83. #define dbg_log(level, fmt, ...) \
  84. if ((level) >= DBG_LEVEL) \
  85. { \
  86. switch(level) \
  87. { \
  88. case DBG_ERROR: _DBG_COLOR(31); break; \
  89. case DBG_WARNING: _DBG_COLOR(33); break; \
  90. case DBG_INFO: _DBG_COLOR(32); break; \
  91. default: break; \
  92. } \
  93. rt_kprintf(DBG_SECTION_NAME fmt, ##__VA_ARGS__); \
  94. _DBG_COLOR(0); \
  95. }
  96. #define dbg_here \
  97. if ((DBG_LEVEL) >= DBG_LOG){ \
  98. rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n", \
  99. __FUNCTION__, __LINE__); \
  100. }
  101. #define dbg_enter \
  102. if ((DBG_LEVEL) >= DBG_LOG){ \
  103. _DBG_COLOR(32); \
  104. rt_kprintf(DBG_SECTION_NAME " Enter %s\n", \
  105. __FUNCTION__); \
  106. _DBG_COLOR(0); \
  107. }
  108. #define dbg_exit \
  109. if ((DBG_LEVEL) >= DBG_LOG){ \
  110. _DBG_COLOR(32); \
  111. rt_kprintf(DBG_SECTION_NAME " Exit %s:%d\n", \
  112. __FUNCTION__); \
  113. _DBG_COLOR(0); \
  114. }
  115. #define dbg_log_line(level, ...) \
  116. dbg_log(level, __VA_ARGS__); \
  117. if ((level) >= DBG_LEVEL) { \
  118. rt_kprintf("\n"); \
  119. }
  120. #else
  121. #define dbg_log(level, fmt, ...)
  122. #define dbg_here
  123. #define dbg_enter
  124. #define dbg_exit
  125. #define dbg_log_line(level, ...)
  126. #endif
  127. #define LOG_D(...) dbg_log_line(DBG_LOG , __VA_ARGS__)
  128. #define LOG_I(...) dbg_log_line(DBG_INFO , __VA_ARGS__)
  129. #define LOG_W(...) dbg_log_line(DBG_WARNING, __VA_ARGS__)
  130. #define LOG_E(...) dbg_log_line(DBG_ERROR , __VA_ARGS__)
  131. #endif /* RT_DBG_H__ */