utils_log.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __UTILS_LOG_H__
  31. #define __UTILS_LOG_H__
  32. #include <stdio.h>
  33. #include <stdint.h>
  34. #include <stdbool.h>
  35. #include <string.h>
  36. // #include <FreeRTOS.h>
  37. // #include <task.h>
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. #define __utils_printf bl_printk
  42. /* cc */
  43. /* define compiler specific symbols */
  44. #if defined (__ICCARM__)
  45. #define BL_PACK_STRUCT_BEGIN
  46. #define BL_PACK_STRUCT_STRUCT
  47. #define BL_PACK_STRUCT_END
  48. #define BL_PACK_STRUCT_FIELD(x) x
  49. #define BL_PACK_STRUCT_USE_INCLUDES
  50. #elif defined (__CC_ARM)
  51. #define BL_PACK_STRUCT_BEGIN __packed
  52. #define BL_PACK_STRUCT_STRUCT
  53. #define BL_PACK_STRUCT_END
  54. #define BL_PACK_STRUCT_FIELD(x) x
  55. #elif defined (__GNUC__)
  56. #define BL_PACK_STRUCT_BEGIN //#pragma pack(push, 1)
  57. #define BL_PACK_STRUCT_STRUCT //__attribute__ ((__packed__))
  58. #define BL_PACK_STRUCT_END //#pragma pack(pop)
  59. #define BL_PACK_STRUCT_FIELD(x)
  60. #elif defined (__TASKING__)
  61. #define BL_PACK_STRUCT_BEGIN
  62. #define BL_PACK_STRUCT_STRUCT
  63. #define BL_PACK_STRUCT_END
  64. #define BL_PACK_STRUCT_FIELD(x) x
  65. #endif
  66. #if 0
  67. #define BL_ASSERT(condition) \
  68. do { \
  69. if (!(condition)) { \
  70. log_assert("ASSERT: %s:%d\r\n", __FILENAME__, __LINE__); \
  71. while (1) { \
  72. /*deap loop now*/ \
  73. } \
  74. } \
  75. } while (0)
  76. #endif
  77. #define BL_ASSERT(condition)
  78. #define LOG_USE_COLOR (0)
  79. typedef enum LOG_BUF_OUT_DATA_TYPE {
  80. LOG_BUF_OUT_DATA_TYPE_HEX,
  81. LOG_BUF_OUT_DATA_TYPE_INT8,
  82. LOG_BUF_OUT_DATA_TYPE_UNT8,
  83. } LOG_BUF_OUT_DATA_TYPE_T;
  84. //#define LOG_USE_LINE_FEED
  85. #define LOG_LOCK_LOCK /* reserved, must diffrent with buf lock */
  86. #define LOG_LOCK_UNLOCK /* reserved, must diffrent with buf lock */
  87. //#define SHORT_FILE __FILENAME__
  88. #define SHORT_FILE __FILE__
  89. #ifdef LOG_USE_LINE_FEED
  90. #define custom_log(N, M, ...) do { LOG_LOCK_LOCK;\
  91. __utils_printf("[%10u][%s: %s:%4d] " M "\r\n",\
  92. (xPortIsInsideInterrupt())?(xTaskGetTickCountFromISR()):(xTaskGetTickCount()),\
  93. N, SHORT_FILE, __LINE__,\
  94. ##__VA_ARGS__);\
  95. LOG_LOCK_UNLOCK;\
  96. } while(0==1)
  97. #define custom_buf_pri(file, line, N, M, ...) do { LOG_LOCK_LOCK;\
  98. __utils_printf("[%10u][%s: %s:%4d] " M, "\r\n",\
  99. (xPortIsInsideInterrupt())?(xTaskGetTickCountFromISR()):(xTaskGetTickCount()),\
  100. N, file, line,\
  101. ##__VA_ARGS__);\
  102. LOG_LOCK_UNLOCK;\
  103. } while(0==1)
  104. #else
  105. #define custom_log(N, M, ...) do { LOG_LOCK_LOCK;\
  106. __utils_printf("[%10u][%s: %s:%4d] " M,\
  107. (xPortIsInsideInterrupt())?(xTaskGetTickCountFromISR()):(xTaskGetTickCount()),\
  108. N, SHORT_FILE, __LINE__,\
  109. ##__VA_ARGS__);\
  110. LOG_LOCK_UNLOCK;\
  111. } while(0==1)
  112. #define custom_buf_pri(file, line, N, M, ...) do { LOG_LOCK_LOCK;\
  113. __utils_printf("[%10u][%s: %s:%4d] " M,\
  114. (xPortIsInsideInterrupt())?(xTaskGetTickCountFromISR()):(xTaskGetTickCount()),\
  115. N, file, line,\
  116. ##__VA_ARGS__);\
  117. LOG_LOCK_UNLOCK;\
  118. } while(0==1)
  119. #endif
  120. #if LOG_USE_COLOR
  121. #define log_buf_pri(file, line, M, ...) custom_buf_pri(file, line, "\x1b[36mBUF\x1b[0m", M, ##__VA_ARGS__)
  122. #else
  123. #define log_buf_pri(file, line, M, ...) custom_buf_pri(file, line, "BUF", M, ##__VA_ARGS__)
  124. #endif
  125. #if LOG_USE_COLOR
  126. #define log_trace(M, ...) custom_log("\x1b[94mTRACE \x1b[0m", M, ##__VA_ARGS__)
  127. #define log_debug(M, ...) custom_log("\x1b[36mDEBUG \x1b[0m", M, ##__VA_ARGS__)
  128. #define log_info(M, ...) custom_log("\x1b[32mINFO \x1b[0m", M, ##__VA_ARGS__)
  129. #define log_warn(M, ...) custom_log("\x1b[33mWARN \x1b[0m", M, ##__VA_ARGS__)
  130. #define log_error(M, ...) custom_log("\x1b[31mERROR \x1b[0m", M, ##__VA_ARGS__)
  131. #define log_assert(M, ...) custom_log("\x1b[35mASSERT\x1b[0m", M, ##__VA_ARGS__)
  132. #else
  133. #define log_trace(M, ...) custom_log("TRACE ", M, ##__VA_ARGS__)
  134. #define log_debug(M, ...) custom_log("DEBUG ", M, ##__VA_ARGS__)
  135. #define log_info(M, ...) custom_log("INFO ", M, ##__VA_ARGS__)
  136. #define log_warn(M, ...) custom_log("WARN ", M, ##__VA_ARGS__)
  137. #define log_error(M, ...) custom_log("ERROR ", M, ##__VA_ARGS__)
  138. #define log_assert(M, ...) custom_log("ASSERT", M, ##__VA_ARGS__)
  139. #endif
  140. #define log_buf(pbuf, len) log_buf_out(SHORT_FILE, __LINE__, pbuf, len, LOG_BUF_OUT_DATA_TYPE_HEX)
  141. #define log_buf_int8(pbuf, len) log_buf_out(SHORT_FILE, __LINE__, pbuf, len, LOG_BUF_OUT_DATA_TYPE_INT8)
  142. #define log_buf_unt8(pbuf, len) log_buf_out(SHORT_FILE, __LINE__, pbuf, len, LOG_BUF_OUT_DATA_TYPE_UNT8)
  143. #define log_compile_time() do {\
  144. log_info("compile date = %s\r\n", __DATE__);\
  145. log_info("compile time = %s\r\n", __TIME__);\
  146. } while (0 == 1);
  147. int log_buf_out(const char *file, int line, const void *inbuf, int len, LOG_BUF_OUT_DATA_TYPE_T type);
  148. void bl_printk(const char *format, ...);
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152. #endif