rtdbg.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. */
  24. /*
  25. * The macro definitions for debug
  26. *
  27. * These macros are defined in static. If you want to use debug macro, you can
  28. * use as following code:
  29. *
  30. * In your C/C++ file, enable/disable DEBUG_ENABLE macro, and then include this
  31. * header file.
  32. *
  33. * #define DBG_SECTION_NAME "[ MOD]"
  34. * #define DBG_ENABLE // enable debug macro
  35. * #define DBG_LEVEL DBG_INFO
  36. * #include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
  37. *
  38. * Then in your C/C++ file, you can use dbg_log macro to print out logs:
  39. * dbg_log(DBG_INFO, "this is a log!\n");
  40. *
  41. * Or if you want to use different color for different kinds log, you can
  42. * #define DBG_COLOR
  43. */
  44. #ifndef RT_DBG_H__
  45. #define RT_DBG_H__
  46. #include <rtconfig.h>
  47. /* DEBUG level */
  48. #define DBG_LOG 0
  49. #define DBG_INFO 1
  50. #define DBG_WARNING 2
  51. #define DBG_ERROR 3
  52. #ifndef DBG_SECTION_NAME
  53. #define DBG_SECTION_NAME "[ DBG]"
  54. #endif
  55. #ifdef DBG_ENABLE
  56. #ifndef DBG_LEVEL
  57. #define DBG_LEVEL DBG_WARNING
  58. #endif
  59. /*
  60. * The color for terminal (foreground)
  61. * BLACK 30
  62. * RED 31
  63. * GREEN 32
  64. * YELLOW 33
  65. * BLUE 34
  66. * PURPLE 35
  67. * CYAN 36
  68. * WHITE 37
  69. */
  70. #ifdef DBG_COLOR
  71. #define _DBG_COLOR(n) rt_kprintf("\033["#n"m")
  72. #else
  73. #define _DBG_COLOR(n)
  74. #endif
  75. /*
  76. * static debug routine
  77. */
  78. #define dbg_log(level, fmt, ...) \
  79. if ((level) >= DBG_LEVEL) \
  80. { \
  81. switch(level) \
  82. { \
  83. case DBG_ERROR: _DBG_COLOR(31); break; \
  84. case DBG_WARNING: _DBG_COLOR(33); break; \
  85. case DBG_INFO: _DBG_COLOR(32); break; \
  86. default: break; \
  87. } \
  88. rt_kprintf(DBG_SECTION_NAME fmt, ##__VA_ARGS__); \
  89. _DBG_COLOR(0); \
  90. }
  91. #define dbg_here \
  92. if ((DBG_LEVEL) >= DBG_LOG){ \
  93. rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n", \
  94. __FUNCTION__, __LINE__); \
  95. }
  96. #define dbg_enter \
  97. if ((DBG_LEVEL) >= DBG_LOG){ \
  98. _DBG_COLOR(32); \
  99. rt_kprintf(DBG_SECTION_NAME " Enter %s\n", \
  100. __FUNCTION__); \
  101. _DBG_COLOR(0); \
  102. }
  103. #define dbg_exit \
  104. if ((DBG_LEVEL) >= DBG_LOG){ \
  105. _DBG_COLOR(32); \
  106. rt_kprintf(DBG_SECTION_NAME " Exit %s:%d\n", \
  107. __FUNCTION__); \
  108. _DBG_COLOR(0); \
  109. }
  110. #else
  111. #define dbg_log(level, fmt, ...)
  112. #define dbg_here
  113. #define dbg_enter
  114. #define dbg_exit
  115. #endif
  116. #endif