ulog_easyflash_be.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <stdio.h>
  29. #include <string.h>
  30. #include <FreeRTOS.h>
  31. #include <easyflash.h>
  32. #include "board.h"
  33. #define LOG_TAG "easyflash"
  34. #include <ulog.h>
  35. #define ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
  36. #ifdef ULOG_EASYFLASH_BACKEND_ENABLE
  37. #if defined(ULOG_ASYNC_OUTPUT_BY_THREAD) && ULOG_ASYNC_OUTPUT_THREAD_STACK < 1024
  38. #error "The thread stack size must more than 1024 when using async output by thread (ULOG_ASYNC_OUTPUT_BY_THREAD)"
  39. #endif
  40. static struct ulog_backend flash_backend;
  41. static uint32_t log_saving_lvl = LOG_FILTER_LVL_ALL;
  42. /**
  43. * Read and output log to console.
  44. *
  45. * @param index index for saved log.
  46. * Minimum index is 0.
  47. * Maximum index is log used flash total size - 1.
  48. * @param size
  49. */
  50. void read_flash_log(uint8_t *logbuf, size_t index, size_t size)
  51. {
  52. /* 64 bytes buffer */
  53. uint32_t buf[512] = { 0 };
  54. size_t log_total_size = ef_log_get_used_size();
  55. size_t buf_size = sizeof(buf);
  56. size_t read_size = 0;
  57. /* word alignment for index and size */
  58. index = ALIGN_DOWN(index, 4);
  59. size = ALIGN_DOWN(size, 4);
  60. if (index + size > log_total_size)
  61. {
  62. printf("The output position and size is out of bound. The max size is %d.\n", log_total_size);
  63. return;
  64. }
  65. while (1)
  66. {
  67. if (read_size + buf_size < size)
  68. {
  69. ef_log_read(index + read_size, buf, buf_size);
  70. memcpy(logbuf + read_size, buf, buf_size);
  71. read_size += buf_size;
  72. }
  73. else
  74. {
  75. ef_log_read(index + read_size, buf, size - read_size);
  76. memcpy(logbuf + read_size, buf, size - read_size);
  77. break;
  78. }
  79. }
  80. }
  81. /**
  82. * Read and output recent log which saved in flash.
  83. *
  84. * @param size recent log size
  85. */
  86. size_t read_recent_flash_log(void *buf, size_t size)
  87. {
  88. size_t max_size = ef_log_get_used_size();
  89. if (size == 0)
  90. {
  91. return 0;
  92. }
  93. else if (size > max_size)
  94. {
  95. size = max_size;
  96. }
  97. read_flash_log(buf, max_size - size, size);
  98. return size;
  99. }
  100. /**
  101. * clean all log which in flash
  102. */
  103. void ulog_ef_log_clean(void)
  104. {
  105. EfErrCode clean_result = EF_NO_ERR;
  106. /* clean all log which in flash */
  107. clean_result = ef_log_clean();
  108. if (clean_result == EF_NO_ERR)
  109. {
  110. TRACE_INFO("All logs which in flash is clean OK.");
  111. }
  112. else
  113. {
  114. TRACE_ERROR("Clean logs which in flash has an error!");
  115. }
  116. }
  117. static void ulog_easyflash_backend_output(struct ulog_backend *backend, uint32_t level, const char *tag, int is_raw,
  118. const char *log, size_t len)
  119. {
  120. /* write some '\r' for word alignment */
  121. char write_overage_c[4] = { '\r', '\r', '\r', '\r' };
  122. size_t write_size_temp = 0;
  123. EfErrCode result = EF_NO_ERR;
  124. /* saving level filter for flash log */
  125. if (level <= log_saving_lvl)
  126. {
  127. /* calculate the word alignment write size */
  128. write_size_temp = ALIGN_DOWN(len, 4);
  129. result = ef_log_write((uint32_t *) log, write_size_temp);
  130. /* write last word alignment data */
  131. if ((result == EF_NO_ERR) && (write_size_temp != len))
  132. {
  133. memcpy(write_overage_c, log + write_size_temp, len - write_size_temp);
  134. ef_log_write((uint32_t *) write_overage_c, sizeof(write_overage_c));
  135. }
  136. }
  137. }
  138. /**
  139. * Set flash log saving level. The log which level less than setting will stop saving to flash.
  140. *
  141. * @param level setting level
  142. */
  143. void ulog_ef_log_lvl_set(uint32_t level)
  144. {
  145. log_saving_lvl = level;
  146. }
  147. int ulog_ef_backend_init(void)
  148. {
  149. flash_backend.output = ulog_easyflash_backend_output;
  150. ulog_backend_register(&flash_backend, "easyflash", pdTRUE);
  151. return 0;
  152. }
  153. #endif /* ULOG_EASYFLASH_BACKEND_ENABLE */