cmb_flash_log.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-12-14 armink the first version
  9. */
  10. #define DBG_TAG "cmb_log"
  11. #define DBG_LVL DBG_LOG
  12. #include <rtdbg.h>
  13. #include <rtthread.h>
  14. #include <stdio.h>
  15. #include <cm_backtrace.h>
  16. //#define CMB_USING_FAL_FLASH_LOG
  17. //#define CMB_USING_FAL_BACKUP_LOG_TO_FILE
  18. #if defined(CMB_USING_FAL_FLASH_LOG)
  19. #if !defined(PKG_USING_FAL) || !defined(RT_USING_DFS)
  20. #error "please enable the FAL package and DFS component"
  21. #endif
  22. #include <fal.h>
  23. #include <dfs_posix.h>
  24. #ifndef CMB_FAL_FLASH_LOG_PART
  25. #define CMB_FAL_FLASH_LOG_PART "cmb_log"
  26. #endif
  27. #ifndef CMB_LOG_FILE_PATH
  28. #define CMB_LOG_FILE_PATH "/log/cmb.log"
  29. #endif
  30. /* cmb flash log partition write granularity, default: 8 bytes */
  31. #ifndef CMB_FLASH_LOG_PART_WG
  32. #define CMB_FLASH_LOG_PART_WG 8
  33. #endif
  34. /* the log length's size which saved in flash */
  35. #define CMB_LOG_LEN_SIZE MAX(sizeof(size_t), CMB_FLASH_LOG_PART_WG)
  36. #ifndef MIN
  37. #define MIN(a, b) (a < b ? a : b)
  38. #endif
  39. #ifndef MAX
  40. #define MAX(a, b) (a > b ? a : b)
  41. #endif
  42. static const struct fal_partition *cmb_log_part = NULL;
  43. /**
  44. * write cmb log to flash partition @see CMB_FLASH_LOG_PART
  45. *
  46. * @param log log buffer
  47. * @param len log length
  48. */
  49. void cmb_flash_log_write(const char *log, size_t len)
  50. {
  51. static uint32_t addr = 0;
  52. uint8_t len_buf[CMB_LOG_LEN_SIZE] = { 0 };
  53. static rt_bool_t first_write = RT_TRUE;
  54. if (first_write)
  55. {
  56. fal_partition_erase_all(cmb_log_part);
  57. first_write = RT_FALSE;
  58. }
  59. /* write log length */
  60. rt_memcpy(len_buf, (uint8_t *)&len, sizeof(size_t));
  61. fal_partition_write(cmb_log_part, addr, len_buf, sizeof(len_buf));
  62. addr += CMB_LOG_LEN_SIZE;
  63. /* write log content */
  64. fal_partition_write(cmb_log_part, addr, (uint8_t *)log, len);
  65. addr += RT_ALIGN(len, CMB_FLASH_LOG_PART_WG);
  66. }
  67. #if defined(RT_USING_ULOG)
  68. static void ulog_cmb_flash_log_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw,
  69. const char *log, size_t len)
  70. {
  71. if (!rt_strcmp(tag, CMB_LOG_TAG))
  72. {
  73. cmb_flash_log_write(log, len);
  74. }
  75. }
  76. int ulog_cmb_flash_log_backend_init(void)
  77. {
  78. static struct ulog_backend backend;
  79. cmb_log_part = fal_partition_find(CMB_FAL_FLASH_LOG_PART);
  80. RT_ASSERT(cmb_log_part != NULL);
  81. backend.init = RT_NULL;
  82. backend.output = ulog_cmb_flash_log_backend_output;
  83. ulog_backend_register(&backend, "cmb_flash_log", RT_FALSE);
  84. return 0;
  85. }
  86. INIT_APP_EXPORT(ulog_cmb_flash_log_backend_init);
  87. #else
  88. void cmb_flash_log_println(const char *fmt, ...)
  89. {
  90. va_list args;
  91. rt_size_t length;
  92. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  93. va_start(args, fmt);
  94. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  95. if (length > RT_CONSOLEBUF_SIZE - 1 - 2)
  96. length = RT_CONSOLEBUF_SIZE - 3;
  97. /* add CRLF */
  98. rt_log_buf[length++] = '\r';
  99. rt_log_buf[length++] = '\n';
  100. cmb_flash_log_write(rt_log_buf, length);
  101. va_end(args);
  102. }
  103. #endif /* RT_USING_ULOG */
  104. #ifdef CMB_USING_FAL_BACKUP_LOG_TO_FILE
  105. int cmb_backup_flash_log_to_file(void)
  106. {
  107. cmb_log_part = fal_partition_find(CMB_FAL_FLASH_LOG_PART);
  108. RT_ASSERT(cmb_log_part != NULL);
  109. size_t len;
  110. uint32_t addr = 0;
  111. rt_bool_t has_read_log = RT_FALSE;
  112. int log_fd = -1;
  113. while (1)
  114. {
  115. fal_partition_read(cmb_log_part, addr, (uint8_t *)&len, sizeof(size_t));
  116. if (len != 0xFFFFFFFF)
  117. {
  118. char log_buf[ULOG_LINE_BUF_SIZE];
  119. if (!has_read_log)
  120. {
  121. has_read_log = RT_TRUE;
  122. LOG_I("An CmBacktrace log was found on flash. Now will backup it to file ("CMB_LOG_FILE_PATH").");
  123. //TODO check the folder
  124. log_fd = open(CMB_LOG_FILE_PATH, O_WRONLY | O_CREAT | O_APPEND);
  125. if (log_fd < 0) {
  126. LOG_E("Open file ("CMB_LOG_FILE_PATH") failed.");
  127. break;
  128. }
  129. }
  130. addr += CMB_LOG_LEN_SIZE;
  131. /* read log content */
  132. fal_partition_read(cmb_log_part, addr, (uint8_t *)log_buf, MIN(ULOG_LINE_BUF_SIZE, len));
  133. addr += RT_ALIGN(len, CMB_FLASH_LOG_PART_WG);
  134. /* backup log to file */
  135. write(log_fd, log_buf, MIN(ULOG_LINE_BUF_SIZE, len));
  136. }
  137. else
  138. {
  139. break;
  140. }
  141. }
  142. if (has_read_log)
  143. {
  144. if (log_fd >= 0)
  145. {
  146. LOG_I("Backup the CmBacktrace flash log to file ("CMB_LOG_FILE_PATH") successful.");
  147. close(log_fd);
  148. fal_partition_erase_all(cmb_log_part);
  149. }
  150. }
  151. return 0;
  152. }
  153. INIT_APP_EXPORT(cmb_backup_flash_log_to_file);
  154. #endif /* CMB_USING_FAL_BACKUP_LOG_TO_FILE */
  155. #endif /* defined(CMB_USING_FLASH_LOG_BACKUP) */