dbg_log.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef __DBG_LOG_H
  2. #define __DBG_LOG_H
  3. #include <stdio.h>
  4. /* DEBUG level */
  5. #define DBG_ERROR 0
  6. #define DBG_WARNING 1
  7. #define DBG_INFO 2
  8. #define DBG_LOG 3
  9. #ifndef DBG_SECTION_NAME
  10. #define DBG_SECTION_NAME "DBG"
  11. #endif
  12. #ifdef DBG_ENABLE
  13. #ifndef DBG_LEVEL
  14. #define DBG_LEVEL DBG_WARNING
  15. #endif
  16. /*
  17. * The color for terminal (foreground)
  18. * BLACK 30
  19. * RED 31
  20. * GREEN 32
  21. * YELLOW 33
  22. * BLUE 34
  23. * PURPLE 35
  24. * CYAN 36
  25. * WHITE 37
  26. */
  27. #ifdef DBG_COLOR
  28. #define _DBG_COLOR(n) printf("\033[" #n "m")
  29. #define _DBG_LOG_HDR(lvl_name, color_n) \
  30. printf("\033[" #color_n "m[" lvl_name "/" DBG_SECTION_NAME "] ")
  31. #define _DBG_LOG_X_END \
  32. printf("\033[0m\n")
  33. #else
  34. #define _DBG_COLOR(n)
  35. #define _DBG_LOG_HDR(lvl_name, color_n) \
  36. printf("[" lvl_name "/" DBG_SECTION_NAME "] ")
  37. #define _DBG_LOG_X_END \
  38. printf("\n")
  39. #endif /* DBG_COLOR */
  40. /*
  41. * static debug routine
  42. * NOTE: This is a NOT RECOMMENDED API. Please using LOG_X API.
  43. * It will be DISCARDED later. Because it will take up more resources.
  44. */
  45. #define dbg_log(level, fmt, ...) \
  46. if ((level) <= DBG_LEVEL) { \
  47. switch (level) { \
  48. case DBG_ERROR: \
  49. _DBG_LOG_HDR("E", 31); \
  50. break; \
  51. case DBG_WARNING: \
  52. _DBG_LOG_HDR("W", 33); \
  53. break; \
  54. case DBG_INFO: \
  55. _DBG_LOG_HDR("I", 32); \
  56. break; \
  57. case DBG_LOG: \
  58. _DBG_LOG_HDR("D", 0); \
  59. break; \
  60. default: \
  61. break; \
  62. } \
  63. printf(fmt, ##__VA_ARGS__); \
  64. _DBG_COLOR(0); \
  65. }
  66. #define dbg_here \
  67. if ((DBG_LEVEL) <= DBG_LOG) { \
  68. printf(DBG_SECTION_NAME " Here %s:%d\n", \
  69. __FUNCTION__, __LINE__); \
  70. }
  71. #define dbg_enter \
  72. if ((DBG_LEVEL) <= DBG_LOG) { \
  73. _DBG_COLOR(32); \
  74. printf(DBG_SECTION_NAME " Enter %s\n", \
  75. __FUNCTION__); \
  76. _DBG_COLOR(0); \
  77. }
  78. #define dbg_exit \
  79. if ((DBG_LEVEL) <= DBG_LOG) { \
  80. _DBG_COLOR(32); \
  81. printf(DBG_SECTION_NAME " Exit %s:%d\n", \
  82. __FUNCTION__); \
  83. _DBG_COLOR(0); \
  84. }
  85. #define dbg_log_line(lvl, color_n, fmt, ...) \
  86. do { \
  87. _DBG_LOG_HDR(lvl, color_n); \
  88. printf(fmt, ##__VA_ARGS__); \
  89. _DBG_LOG_X_END; \
  90. } while (0)
  91. #define dbg_raw(...) printf(__VA_ARGS__);
  92. #else
  93. #define dbg_log(level, fmt, ...)
  94. #define dbg_here
  95. #define dbg_enter
  96. #define dbg_exit
  97. #define dbg_log_line(lvl, color_n, fmt, ...)
  98. #define dbg_raw(...)
  99. #endif /* DBG_ENABLE */
  100. #if (DBG_LEVEL >= DBG_LOG)
  101. #define LOG_D(fmt, ...) dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
  102. #else
  103. #define LOG_D(...)
  104. #endif
  105. #if (DBG_LEVEL >= DBG_INFO)
  106. #define LOG_I(fmt, ...) dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
  107. #else
  108. #define LOG_I(...)
  109. #endif
  110. #if (DBG_LEVEL >= DBG_WARNING)
  111. #define LOG_W(fmt, ...) dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
  112. #else
  113. #define LOG_W(...)
  114. #endif
  115. #if (DBG_LEVEL >= DBG_ERROR)
  116. #define LOG_E(fmt, ...) dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
  117. #else
  118. #define LOG_E(...)
  119. #endif
  120. #define LOG_RAW(...) dbg_raw(__VA_ARGS__)
  121. #endif /* __DBG_LOG_H */