ulog_easyflash_be.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * This file is part of the EasyFlash Library.
  3. *
  4. * Copyright (c) 2014-2018, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * The ulog backend implementation for EasyFlash.
  26. * Created on: 2018-10-22
  27. */
  28. #include <rtthread.h>
  29. #include <easyflash.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #define LOG_TAG "easyflash"
  33. #include <ulog.h>
  34. #ifdef ULOG_EASYFLASH_BACKEND_ENABLE
  35. #if defined(ULOG_ASYNC_OUTPUT_BY_THREAD) && ULOG_ASYNC_OUTPUT_THREAD_STACK < 1024
  36. #error "The thread stack size must more than 1024 when using async output by thread (ULOG_ASYNC_OUTPUT_BY_THREAD)"
  37. #endif
  38. static struct ulog_backend flash_backend;
  39. static rt_uint32_t log_saving_lvl = LOG_FILTER_LVL_ALL;
  40. /**
  41. * Read and output log to console.
  42. *
  43. * @param index index for saved log.
  44. * Minimum index is 0.
  45. * Maximum index is log used flash total size - 1.
  46. * @param size
  47. */
  48. static void read_flash_log(size_t index, size_t size)
  49. {
  50. /* 64 bytes buffer */
  51. uint32_t buf[16] = { 0 };
  52. size_t log_total_size = ef_log_get_used_size();
  53. size_t buf_size = sizeof(buf);
  54. size_t read_size = 0;
  55. /* word alignment for index and size */
  56. index = RT_ALIGN_DOWN(index, 4);
  57. size = RT_ALIGN_DOWN(size, 4);
  58. if (index + size > log_total_size)
  59. {
  60. rt_kprintf("The output position and size is out of bound. The max size is %d.\n", log_total_size);
  61. return;
  62. }
  63. while (1)
  64. {
  65. if (read_size + buf_size < size)
  66. {
  67. ef_log_read(index + read_size, buf, buf_size);
  68. rt_kprintf("%.*s", buf_size, buf);
  69. read_size += buf_size;
  70. }
  71. else
  72. {
  73. ef_log_read(index + read_size, buf, size - read_size);
  74. rt_kprintf("%.*s", size - read_size, buf);
  75. /* output newline sign */
  76. rt_kprintf(ULOG_NEWLINE_SIGN);
  77. break;
  78. }
  79. }
  80. }
  81. /**
  82. * Read and output all log which saved in flash.
  83. */
  84. static void read_all_flash_log(void)
  85. {
  86. read_flash_log(0, ef_log_get_used_size());
  87. }
  88. /**
  89. * Read and output recent log which saved in flash.
  90. *
  91. * @param size recent log size
  92. */
  93. static void read_recent_flash_log(size_t size)
  94. {
  95. size_t max_size = ef_log_get_used_size();
  96. if (size == 0)
  97. {
  98. return;
  99. }
  100. if (size > max_size)
  101. {
  102. rt_kprintf("The output size is out of bound. The max size is %d.\n", max_size);
  103. }
  104. else
  105. {
  106. read_flash_log(max_size - size, size);
  107. }
  108. }
  109. /**
  110. * clean all log which in flash
  111. */
  112. void ulog_ef_log_clean(void)
  113. {
  114. EfErrCode clean_result = EF_NO_ERR;
  115. /* clean all log which in flash */
  116. clean_result = ef_log_clean();
  117. if (clean_result == EF_NO_ERR)
  118. {
  119. LOG_I("All logs which in flash is clean OK.");
  120. }
  121. else
  122. {
  123. LOG_E("Clean logs which in flash has an error!");
  124. }
  125. }
  126. static void ulog_easyflash_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw,
  127. const char *log, size_t len)
  128. {
  129. /* write some '\r' for word alignment */
  130. char write_overage_c[4] = { '\r', '\r', '\r', '\r' };
  131. size_t write_size_temp = 0;
  132. EfErrCode result = EF_NO_ERR;
  133. /* saving level filter for flash log */
  134. if (level <= log_saving_lvl)
  135. {
  136. /* calculate the word alignment write size */
  137. write_size_temp = RT_ALIGN_DOWN(len, 4);
  138. result = ef_log_write((uint32_t *) log, write_size_temp);
  139. /* write last word alignment data */
  140. if ((result == EF_NO_ERR) && (write_size_temp != len))
  141. {
  142. memcpy(write_overage_c, log + write_size_temp, len - write_size_temp);
  143. ef_log_write((uint32_t *) write_overage_c, sizeof(write_overage_c));
  144. }
  145. }
  146. }
  147. /**
  148. * Set flash log saving level. The log which level less than setting will stop saving to flash.
  149. *
  150. * @param level setting level
  151. */
  152. void ulog_ef_log_lvl_set(rt_uint32_t level)
  153. {
  154. log_saving_lvl = level;
  155. }
  156. int ulog_ef_backend_init(void)
  157. {
  158. flash_backend.output = ulog_easyflash_backend_output;
  159. ulog_backend_register(&flash_backend, "easyflash", RT_TRUE);
  160. return 0;
  161. }
  162. INIT_APP_EXPORT(ulog_ef_backend_init);
  163. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  164. #include <finsh.h>
  165. static void ulog_flash(uint8_t argc, char **argv)
  166. {
  167. if (argc >= 2)
  168. {
  169. if (!strcmp(argv[1], "read"))
  170. {
  171. if (argc >= 3)
  172. {
  173. read_recent_flash_log(atol(argv[2]));
  174. }
  175. else
  176. {
  177. read_all_flash_log();
  178. }
  179. }
  180. else if (!strcmp(argv[1], "clean"))
  181. {
  182. ulog_ef_log_clean();
  183. }
  184. else
  185. {
  186. rt_kprintf("Please input ulog_flash <read|clean>.\n");
  187. }
  188. }
  189. else
  190. {
  191. rt_kprintf("Please input ulog_flash <read|clean>.\n");
  192. }
  193. }
  194. MSH_CMD_EXPORT(ulog_flash, ulog <read|clean> flash log by EasyFlash backend);
  195. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
  196. #endif /* ULOG_EASYFLASH_BACKEND_ENABLE */