act_log.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef __ACT_LOG_H
  2. #define __ACT_LOG_H
  3. #define MAX_MODULE_NAME_LEN (16)
  4. #define MAX_NANO_ARG_NUM (7)
  5. #define INVALID_NANO_ARG_NUM (15)
  6. extern const char * const nano_args;
  7. #define FUN_ARG_0 (&nano_args[0])
  8. #define FUN_ARG_1 (&nano_args[1])
  9. #define FUN_ARG_2 (&nano_args[2])
  10. #define FUN_ARG_3 (&nano_args[3])
  11. #define FUN_ARG_4 (&nano_args[4])
  12. #define FUN_ARG_5 (&nano_args[5])
  13. #define FUN_ARG_6 (&nano_args[6])
  14. #define FUN_ARG_7 (&nano_args[7])
  15. #define FUN_ARG_8 (&nano_args[8])
  16. #define FUN_ARG_9 (&nano_args[9])
  17. #define FUN_ARG_10 (&nano_args[10])
  18. #define FUN_ARG_11 (&nano_args[11])
  19. #define FUN_ARG_12 (&nano_args[12])
  20. #define FUN_ARG_I (&nano_args[13])
  21. #define FUN_ARG_EX(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N, ...) N
  22. #define FUN_ARG_EX_() FUN_ARG_12, FUN_ARG_11, FUN_ARG_10, FUN_ARG_9, \
  23. FUN_ARG_8, FUN_ARG_7, FUN_ARG_6, FUN_ARG_5, \
  24. FUN_ARG_4, FUN_ARG_3, FUN_ARG_2, FUN_ARG_1, FUN_ARG_0
  25. #define ARGS_CHECK(N) (true ? N : FUN_ARG_I)
  26. #define FUN_ARG_NUM_(...) FUN_ARG_EX(__VA_ARGS__)
  27. #define FUN_ARG_NUM(...) ARGS_CHECK(FUN_ARG_NUM_(FUN_ARG_I, ##__VA_ARGS__, FUN_ARG_EX_()))
  28. #define FUN_ARG_NUM_INTEGER(a) (((a) >= 0 && (a) <= MAX_NANO_ARG_NUM) ? (a) : INVALID_NANO_ARG_NUM)
  29. #define ACTLOG_LEVEL_NONE 0
  30. #define ACTLOG_LEVEL_ERROR 1
  31. #define ACTLOG_LEVEL_WARN 2
  32. #define ACTLOG_LEVEL_INFO 3
  33. #define ACTLOG_LEVEL_DEBUG 4
  34. #define ACTLOG_LEVEL_MAX 5
  35. #define ACTLOG_FILE_TYPE_LOG 0
  36. #define ACTLOG_FILE_TYPE_RUNTIME_LOG 1
  37. #define ACTLOG_FILE_TYPE_EVENT 2
  38. #define ACTLOG_FILE_TYPE_LOG_SAVE 3
  39. typedef enum{
  40. ACTLOG_OUTPUT_MODE_DIRECT = (1 << 0),
  41. ACTLOG_OUTPUT_MODE_FLOW = (1 << 1),
  42. ACTLOG_OUTPUT_MODE_BINARY = (1 << 2),
  43. ACTLOG_OUTPUT_MODE_TEXT = (1 << 3),
  44. ACTLOG_OUTPUT_MODE_USER = (1 << 4),
  45. }act_log_output_mode_e;
  46. #define ACTLOG_PACK(id, level, line, nums) \
  47. ((((line) & 0xffff) << 16) \
  48. | (((id) & 0xff) << 8) \
  49. | ((level & 0x7) << 4) \
  50. | (nums & 0xf))
  51. typedef union{
  52. uint32_t data;
  53. struct{
  54. uint8_t arg_num: 4;
  55. uint8_t level: 3;
  56. uint8_t reserved : 1;
  57. uint8_t id;
  58. uint16_t line;
  59. }bit_data;
  60. }act_log_pack_data;
  61. typedef void (* actlog_backend_callback_t)(const uint8_t *data, uint32_t len, void *user_data);
  62. int act_log_init(void);
  63. /*for bt transfer*/
  64. #define LOG_TYPE_COREDUMP 0
  65. #define LOG_TYPE_SYSLOG 1
  66. #define LOG_TYPE_RUN_LOG 2
  67. #define LOG_TYPE_RAMDUMP 3
  68. #ifdef CONFIG_ACTLOG
  69. extern int actlog_syslog_transfer(int log_type, int (*traverse_cb)(uint8_t *data, uint32_t max_len));
  70. #endif
  71. #ifdef CONFIG_DEBUG_COREDUMP_BACKEND_FLASH_DEV
  72. int coredump_transfer(int (*traverse_cb)(uint8_t *data, uint32_t max_len));
  73. #endif
  74. #ifdef CONFIG_DEBUG_RAMDUMP
  75. int ramdump_transfer(int (*traverse_cb)(uint8_t *data, uint32_t max_len));
  76. #endif
  77. static inline int actlog_bt_transfer(int log_type, int (*traverse_cb)(uint8_t *data, uint32_t max_len))
  78. {
  79. #ifdef CONFIG_ACTLOG
  80. if(log_type == LOG_TYPE_SYSLOG || log_type == LOG_TYPE_RUN_LOG)
  81. return actlog_syslog_transfer(log_type, traverse_cb);
  82. #endif
  83. #ifdef CONFIG_DEBUG_COREDUMP_BACKEND_FLASH_DEV
  84. if(log_type == LOG_TYPE_COREDUMP)
  85. return coredump_transfer(traverse_cb);
  86. #endif
  87. #ifdef CONFIG_DEBUG_RAMDUMP
  88. if(log_type == LOG_TYPE_RAMDUMP)
  89. return ramdump_transfer(traverse_cb);
  90. #endif
  91. return 0;
  92. }
  93. /**
  94. * @brief register output backend
  95. *
  96. * @param mode backend output mode, see @act_log_output_mode_e
  97. * @param callback callback function for output backend
  98. * @param user_data user-defined data
  99. *
  100. * @retval 0 on success else negative code.
  101. */
  102. int act_log_register_output_backend(uint8_t mode, actlog_backend_callback_t callback, void *user_data);
  103. /**
  104. * @brief unregister output backend
  105. *
  106. * @param mode backend output mode, see @act_log_output_mode_e
  107. *
  108. * @retval 0 on success else negative code.
  109. */
  110. int act_log_unregister_output_backend(uint8_t mode);
  111. /* The log process thread has the K_LOWEST_APPLICATION_THREAD_PRIO, adjust it
  112. * to a higher priority to increase the chances of being scheduled to handle
  113. * log message as soon as possible
  114. */
  115. void actlog_increase_log_thread_priority(void);
  116. /* The log process thread has the K_LOWEST_APPLICATION_THREAD_PRIO, recovery it
  117. * from a higher priority to normal priority
  118. */
  119. void actlog_decrease_log_thread_priority(void);
  120. #ifdef CONFIG_ACTLOG_SHOW_FUNCTION
  121. #define ACTLOG_FUNCTION __FUNCTION__
  122. #else
  123. #define ACTLOG_FUNCTION NULL
  124. #endif
  125. #ifdef CONFIG_ACTLOG_SHOW_FUNCTION
  126. void actlog_printk(uint32_t pack_data, const char *func, const char *fmt, ...);
  127. void actlog_printk_nano(uint32_t pack_data, const char *func, const char *fmt, ...);
  128. #define NORMAL_PRINTK(pack_data, fmt, ...) actlog_printk(pack_data, ACTLOG_FUNCTION, fmt, ##__VA_ARGS__)
  129. #define NANO_PRINTK(pack_data, fmt, ...) actlog_printk_nano(pack_data, ACTLOG_FUNCTION, fmt, ##__VA_ARGS__)
  130. #else
  131. void actlog_printk(uint32_t pack_data, const char *fmt, ...);
  132. void actlog_printk_nano(uint32_t pack_data, const char *fmt, ...);
  133. #define NORMAL_PRINTK(pack_data, fmt, ...) actlog_printk(pack_data, fmt, ##__VA_ARGS__)
  134. #define NANO_PRINTK(pack_data, fmt, ...) actlog_printk_nano(pack_data, fmt, ##__VA_ARGS__)
  135. #endif
  136. #ifndef CONFIG_ACTLOG_USE_NANOLOG
  137. #define ACT_LOG_PRINT(pack_data, fmt, ...) NORMAL_PRINTK(pack_data, fmt, ##__VA_ARGS__)
  138. #else
  139. #define ACT_LOG_PRINT(pack_data, fmt, ...) NANO_PRINTK(pack_data, fmt, ##__VA_ARGS__)
  140. #endif
  141. #define ACTLOG_PACK_NANO_DATA(id, level, fmt, ...) ACTLOG_PACK(id, level, __LINE__, (FUN_ARG_NUM_INTEGER(FUN_ARG_NUM(__VA_ARGS__) - nano_args)))
  142. #define ACTLOG_ID_GET() LOG_CONST_ID_GET(__log_current_const_data)
  143. #ifdef CONFIG_ACTLOG_COMPILE_MODUL_LEVEL
  144. #define ACTLOG_CONST_LEVEL_CHECK(_level) ((_level <= __log_level) && (_level <= CONFIG_ACTLOG_COMPILE_LEVEL))
  145. #else
  146. #define ACTLOG_CONST_LEVEL_CHECK(_level) ((_level <= CONFIG_ACTLOG_COMPILE_LEVEL))
  147. #endif
  148. #define ACT_LOG(_level, fmt, ...) do { \
  149. if (!ACTLOG_CONST_LEVEL_CHECK(_level)) { \
  150. break; \
  151. } \
  152. ACT_LOG_PRINT(ACTLOG_PACK_NANO_DATA(ACTLOG_ID_GET(), _level, fmt, __VA_ARGS__), fmt, ##__VA_ARGS__); \
  153. } while (false)
  154. #endif